6

I have structure like:

  • app
    • scripts
      • libs
        • jquery.js
        • bootstrap.js
        • moment.js
      • app.js
      • vendor.js
      • common.js
      • app.config.js
      • main.js #require config

Note vendor.js is just a file that includes all files under libs. Ex

//vendor.js define(['jquery','bootstrap', 'moment'], function(){});

Just to list out the dependencies:

  • app.js depends on common.js and app.config.js
  • common.js depends on vendor.js
  • app.config.js depends on moment.js

What I'm trying to do is run the grunt requirejs command to create a vendor.js file with all files under libs/ and a app.js with the rest of the files not included in vendor.js.

Here is what my requirejs options looks like:

module.exports = function (grunt) {
    'use strict';
    var config = {
        dist: {
            options: {
                appDir: 'app/',
                baseUrl: './scripts',
                mainConfigFile: 'app/scripts/main.js',
                dir: 'dist/scripts/',
                modules: [
                    { name: 'vendor'},
                    { name: 'app', exclude: ['vendor'] }
                ]
            }
        }
    };
    grunt.config('requirejs', config);
};

What I get from running the above is the following build.txt

scripts/vendor.js
----------------
scripts/libs/jquery.js
scripts/libs/bootstrap.js
scripts/libs/moment.js
scripts/app.js
scripts/vendor.js
scripts/common.js
scripts/app.config.js

scripts/app.js
----------------

As you can see all the files are just appended to vendor.js and not app.js. I would expect vendor.js to include vendor.js and its dependencies. And app.js to include the rest since vendor is excluded.

I've tried numerous combination and still a nogo here.

Cœur
  • 37,241
  • 25
  • 195
  • 267
dchhetri
  • 6,926
  • 4
  • 43
  • 56
  • Just wondering, why are you using grunt instead of gulp or webpack? Also, why aren't you following more commonly used structures for grunt files? – Pytth Mar 31 '17 at 22:09
  • @Pytth pre-existing code, the structure I might be able to alter but I'd rather get this to work with minimal change before I update anything major. Appreciate the comment man. – dchhetri Mar 31 '17 at 23:06
  • Np man! is that your whole grunt file? – Pytth Mar 31 '17 at 23:13
  • @Pytth For that task that's pretty much it. I was thinking maybe it was some shim dependencies causing issue but after checking it I don't think thats the cause – dchhetri Mar 31 '17 at 23:21
  • Just wondering, what happens if you switch all `app` and `vendor` strings in that config? Do you have a similar `app.js` file similar to the vendor one with: `define(['jquery','bootstrap', 'moment'], function(){});` – Pytth Mar 31 '17 at 23:26
  • @Pytth Yea the first entry seems to be taking all the files. Even if I just have `{ name: 'foo', create: true, include: ['lodash']},` it seems to just concat all the files – dchhetri Mar 31 '17 at 23:33
  • Honestly man, I have looked up the various grunt `requirejs` libs, and none seem to share that structure for their config files. Seems like you are fighting a very real up hill battle with this one by having to adhere to that structure. – Pytth Mar 31 '17 at 23:42
  • @Pytth I agree. I think I found the issue( or a lead!). In the require config(not the grunt config above), there is a `deps: ['app']`. That seems to be the problem because the optimizer reads the require.config and sees app is dependencies, which just loads all files....Going from here, and if resolved, will update. Thanks for the chat, it helped a lot. – dchhetri Mar 31 '17 at 23:51

1 Answers1

1

Found the issue! The problem was with my require config file thats feed to the grunt requirejs task. The require config file had deps: ['app'] code, which conflicted when trying to split up into modules because as soon as the require config was feed to the grunt requirejs task, it seen app as necessary dependency, which meant app and all its files were marked as dependent and thus included in the first module file listed.

dchhetri
  • 6,926
  • 4
  • 43
  • 56