jbarreiros
jbarreiros

Reputation: 1103

Using requirejs and trying to optimize javascript into a vendor file and an app file

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:

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?

Upvotes: 1

Views: 831

Answers (1)

Louis
Louis

Reputation: 151380

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.

Upvotes: 2

Related Questions