1

I'm working on a project using requirejs and I'm trying to optimize my javascript into two files: vendor libraries and app code. Unfortunately, I can't seem to get it to work.

Folder structure:

  • backbone_components/
  • app/
    • less/
    • js/
      • component1/
      • component2/
      • layouts/
        • dashboard/
        • about/
      • lib/
      • config.js
      • main.js
    • index.html
  • dist/
  • Gruntfile.js

config.js:

require.config({
  baseUrl: '../js/',
  paths: {
    jquery:     '../../bower_components/jquery/dist/jquery',
    underscore: '../../bower_components/lodash/dist/lodash',
    backbone:   '../../bower_components/backbone/backbone',
    text:       '../../bower_components/requirejs-text/text'
  },
  enforeceDefine: true
});

define(['backbone', 'main'], function(Backbone, app) {
});

Gruntfile.js:

requirejs: {
  options: {
    dir: 'dist/js/',
    mainConfigFile: 'app/js/config.js',
    optimize: 'none', 
    normalizeDirDefines: 'all',
    skipDirOptimize: true
  },
  dist: {
    options: {
      modules: [
        {
          name: 'vendor',
          include: ['jquery', 'underscore', 'backbone', 'text'],
        },
        {
          name: 'app',
          exclude: ['vendor']
        }
      ]
    }
  }
});

When I run grunt, I get the following error: "Error: Error: ERROR: module path does not exist: /path/to/project/app/js/app/js/vendor.js for module named: vendor."

Why is it looking for "vendor" when it doesn't exist yet?

1 Answer 1

2

I take it that you have no module named vendor in the source you want to optimize. If this is correct, then the error you are getting is because r.js is looking for a module named vendor. You have to tell it to create it with the create option:

modules: [
    {
      name: 'vendor',
      create: true,            // <--- Add this option!
      include: ['jquery', 'underscore', 'backbone', 'text']
    },
    {
      name: 'app',
      exclude: ['vendor']
    }
]

Without the option r.js understands you to be effectively saying "take the vendor module and include with it jquery, etc." With the option, you are telling it to create vendor from scratch.

The create option is documented in this file.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Louis. I was just reading about "create" when your comment came in. And that made it work.
Out of curiosity, all javascript files are being copied into "dist/js". I expected only vendor.js and main.js to be there. Did I miss something else?
removeCombined: true I would think. You can find it documented in the same file as create.
Yup, that did it. Would of expected removeCombined's default to be true. Feeling better about this. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.