requirejs / r.js

Runs RequireJS in Node and Rhino, and used to run the RequireJS optimizer
Other
2.57k stars 671 forks source link

Requirejs build not finding dependency in modules #690

Closed rossthedevigner closed 10 years ago

rossthedevigner commented 10 years ago

I'm using Grunt along with grunt-requirejs, usemin, yeoman for a multipage project, but when I try to use modules as an option in my requirejs:dist task my dependencies cannot be found. I'm also wondering if this is the best way to output multiple optimized files.

config.js

'use strict';

require.config({
  baseUrl: '/js',
  map: {
    // '*' means all modules will get 'jquery-private'
    // for their 'jquery' dependency.
    '*': {
      'jquery': 'jquery-noconflict'
    },

    // 'jquery-private' wants the real jQuery module
    // though. If this line was not here, there would
    // be an unresolvable cyclic dependency.
    'jquery-noconflict': {
      'jquery': 'jquery'
    }
  },
  paths: {
    'jquery-xdomain': 'vendor/jquery.xdomainrequest.min',
    'knockout': '../bower_components/knockout.js/knockout',
    'lodash': '../bower_components/lodash/lodash',
    'moment': '../bower_components/moment/moment',
    'momentTwitter': 'vendor/moment.twitter',
    'text': '../bower_components/requirejs-plugins/lib/text',
    'json': '../bower_components/requirejs-plugins/src/json'
  },
  shim: {
    'json': {
      deps: ['text']
    },
    'jquery-xdomain': {
      deps: ['jquery']
    },
    'lodash': {
      exports: '_'
    },
    'momentTwitter': {
      deps: ['moment']
    },
    'knockout': {
      exports: 'ko'
    },

  }
});

jquery-noconflict.js

'use strict';

define(['app/js/bower_components/jquery/jquery'], function() {
  return $.noConflict();
});

index-main.js

'use strict';
require(['jquery', 'index-app'], function($, app) {
    console.log('index-main');
});

Gruntfile.js

requirejs: {
      dist: {
        options: {
          appDir: '<%= yeoman.app %>/js',
          baseUrl: '.',
          mainConfigFile: '<%= yeoman.app %>/js/config.js',
          dir: '<%= yeoman.dist %>/js',
          removeCombined: true,
          optimize: 'none',
          preserveLicenseComments: false,
          useStrict: true,
          wrap: true,
          mangle: false,
          paths: {
            'requirejs': 'bower_components/requirejs/require'
          },
          include: 'requirejs',
          modules: [{
            name: 'index-main'
          }]
        }
      }
    }

index.html

{% block scripts %}
  <!-- build:js js/index-main.js -->
    <script src="bower_components/requirejs/require.js"></script>
    <script src="js/config.js"></script>
    <script src="js/index-main.js"></script>
  <!-- endbuild -->
{% endblock %}

The code runs fine on a local server, but when I do the build, it fails on the modules. I'm not entirely sure why.

Let me know if this issue belongs in a different project.

RealFlow commented 10 years ago

Same here. When I do the build, it cannot find my modules.

The code runs well on local server.

jrburke commented 10 years ago

My guess is that you likely do not need appDir if baseUrl is set to the same value. Instead set the baseUrl to that value. Not sure though just by looking at the config. Hopefully an error would be printed if a file was not found. If this was with r.js 2.1.14, then there was a module parsing problem that will be fixed in 2.1.15, see #704 for more details.

Closing for now as a discussion ticket. Feel free to continue discussion here though.