0
votes

I have a project, that consists of multiple apps with the following structure:

project
  |-node_modules
  |-common
     |-public
       |-js
         |-vendor
           |-almond.js
           |-require.js
           |-underscore.js
           |-backbone.js
  |-app1
     |-public
       |-js
         |-requireConfig.js
         |-main.js
         |-views
           |- ...
         |-models
           |- ...
  |-app2
  Gruntfile.js

I want to have my requirejs grunt task compile the js src into one output file for each app. I have this working for app2 but it doesn't use the shared code...yet. I've tried a bunch of different things for my grunt config but I get different errors each time.

Here's my gruntfile so far, for requirejs:

requirejs: {
  app1: {
    options: {
      baseUrl: './',
      mainConfigFile: 'app1/public/js/requireConfig.js',
      include: ['app1/public/js/main'],
      out: 'app1/public/release/main.js',
      name: 'common/public/js/vendor/almond',
      optimize: 'none'
    }
  },
  app2: {...}
},

Here's my requireConfig.js:

require.config({
  paths: {
    jquery: '/common/public/js/vendor/jquery',
    underscore: '/common/public/js/vendor/underscore',
    etc
  }
});

Now when I try to run that task I get an error:

Error: ENOENT, no such file or directory
'/common/public/js/vendor/backbone.js'
In module tree:
   app1/public/js/main

Not sure how to solve this. I've tried setting a baseUrl in my requireConfig but no real luck.

I find the r.js example build file is kind of confusing and I'm guessing maybe I'm mixing up the config in the gruntfile somehow.

1

1 Answers

0
votes

I got this to work by doing some slightly undesirable changes. I have my server set up so that /common/public servers that folder, however this affects my requirejs build. It seems that the routes in my requireConfig have to be relative to the directory it sits in. I'd then have to prefix with:

'../../../common/public'

Which I don't really like, also I had to change the grunt config:

requirejs: {
  app1: {
    options: {
      mainConfigFile: 'app1/public/js/requireConfig.js',
      include: ['main'],
      out: 'app1/public/release/main.js',
      name: '../../../common/public/js/vendor/almond',
      optimize: 'none'
    }
  },
  app2: {...}
},