New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paths fallback not supported in optimizer? #791

Closed
farmersmc opened this Issue Jun 25, 2013 · 13 comments

Comments

Projects
None yet
9 participants
@farmersmc

I am running the Yeoman Beta 6, having installed require.js using the Webapp scaffolding. I am currently having build issues trying to build using Grunt.

I have updated the require.config function:

    paths: {
        jquery: [
            // jQuery Fallback - http://requirejs.org/docs/api.html#pathsfallbacks
            '//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min',
            // If the CDN location fails, load from this location
            '../bower_components/jquery/jquery'
        ],
        bootstrap: 'vendor/bootstrap'
    },
    shim: {
        bootstrap: {
            deps: ['jquery'],
            exports: 'jquery'
        }
    }
});

When I am in the directory and run "grunt" in the Terminal, when it gets to the requirejs:dist step, I receive the following error:

[Error: Error: paths fallback not supported in optimizer. Please provide a build config path override for jquery
    at /Users/myaccount/Sites/test/node_modules/grunt-contrib-requirejs/node_modules/requirejs/bin/r.js:24325:23
]

Is there an error in the way I've formatted the path fallback?

@jrburke

This comment has been minimized.

Show comment
Hide comment
@jrburke

jrburke Jun 26, 2013

Member

How are you calling the optimizer with grunt? I expect you are passing it mainConfigFile, what you can do in that grunt r.js config is just set paths: for jquery to the place to find it on disk for the build.

Closing as the build override should work, but feel free to continue discussion here.

Member

jrburke commented Jun 26, 2013

How are you calling the optimizer with grunt? I expect you are passing it mainConfigFile, what you can do in that grunt r.js config is just set paths: for jquery to the place to find it on disk for the build.

Closing as the build override should work, but feel free to continue discussion here.

@jrburke jrburke closed this Jun 26, 2013

@farmersmc

This comment has been minimized.

Show comment
Hide comment
@farmersmc

farmersmc Jun 27, 2013

Ultimately, it's being called by the grunt.initConfig function as a task:

        requirejs: {
            dist: {
                // Options: https://github.com/jrburke/r.js/blob/master/build/example.build.js
                options: {
                    // `name` and `out` is set by grunt-usemin
                    baseUrl: yeomanConfig.app + '/scripts',
                    optimize: 'none',
                    // TODO: Figure out how to make sourcemaps work with grunt-usemin
                    // https://github.com/yeoman/grunt-usemin/issues/30
                    //generateSourceMaps: true,
                    // required to support SourceMaps
                    // http://requirejs.org/docs/errors.html#sourcemapcomments
                    preserveLicenseComments: false,
                    useStrict: true,
                    wrap: true
                    //uglify2: {} // https://github.com/mishoo/UglifyJS2
                }
            }
        },

This is where the build script hangs every time and throws the error. I can't tell where it's passing it mainConfigFile to be honest. Is there an argument in this task I need to add for this to work?

Ultimately, it's being called by the grunt.initConfig function as a task:

        requirejs: {
            dist: {
                // Options: https://github.com/jrburke/r.js/blob/master/build/example.build.js
                options: {
                    // `name` and `out` is set by grunt-usemin
                    baseUrl: yeomanConfig.app + '/scripts',
                    optimize: 'none',
                    // TODO: Figure out how to make sourcemaps work with grunt-usemin
                    // https://github.com/yeoman/grunt-usemin/issues/30
                    //generateSourceMaps: true,
                    // required to support SourceMaps
                    // http://requirejs.org/docs/errors.html#sourcemapcomments
                    preserveLicenseComments: false,
                    useStrict: true,
                    wrap: true
                    //uglify2: {} // https://github.com/mishoo/UglifyJS2
                }
            }
        },

This is where the build script hangs every time and throws the error. I can't tell where it's passing it mainConfigFile to be honest. Is there an argument in this task I need to add for this to work?

@jrburke

This comment has been minimized.

Show comment
Hide comment
@jrburke

jrburke Jun 27, 2013

Member

I do not know how the grunt task for requirejs works, so I am not sure either. My guess is if you add this to that dist.options object literal, it may work:

  paths: {
       jquery: '../bower_components/jquery/jquery'
  }

assuming that is actually where jquery is located, relative to the baseUrl used in that call.

Member

jrburke commented Jun 27, 2013

I do not know how the grunt task for requirejs works, so I am not sure either. My guess is if you add this to that dist.options object literal, it may work:

  paths: {
       jquery: '../bower_components/jquery/jquery'
  }

assuming that is actually where jquery is located, relative to the baseUrl used in that call.

@asuh

This comment has been minimized.

Show comment
Hide comment
@asuh

asuh Jul 12, 2013

@jrburke Hey, I'm kinda getting the runaround that this issue is on your side. If you look above at Issue #26 from the gruntjs repo, they're referring back to the problem being on https://github.com/jrburke/r.js

I'm not sure where to go as both projects are saying it's the other side. Thanks.

asuh commented Jul 12, 2013

@jrburke Hey, I'm kinda getting the runaround that this issue is on your side. If you look above at Issue #26 from the gruntjs repo, they're referring back to the problem being on https://github.com/jrburke/r.js

I'm not sure where to go as both projects are saying it's the other side. Thanks.

@jrburke

This comment has been minimized.

Show comment
Hide comment
@jrburke

jrburke Jul 17, 2013

Member

@asuh re-reading my previous response, I think it did not include enough detail: paths config is not supported by the optimizer, but what I thought should work is if you put the paths config override in the dist.options that it may work. Anything in the explicit build config should override paths config values. Did you try the dis.options override mentioned in #791 (comment) or you get the same result?

Member

jrburke commented Jul 17, 2013

@asuh re-reading my previous response, I think it did not include enough detail: paths config is not supported by the optimizer, but what I thought should work is if you put the paths config override in the dist.options that it may work. Anything in the explicit build config should override paths config values. Did you try the dis.options override mentioned in #791 (comment) or you get the same result?

@asuh

This comment has been minimized.

Show comment
Hide comment
@asuh

asuh Jul 17, 2013

I can verify that on both Windows and OS X this appears to have solved the problem. I suppose I need to submit a pull request with comments about this. Thank you for your attention and resolution!

asuh commented Jul 17, 2013

I can verify that on both Windows and OS X this appears to have solved the problem. I suppose I need to submit a pull request with comments about this. Thank you for your attention and resolution!

@raldred

This comment has been minimized.

Show comment
Hide comment
@raldred

raldred Dec 9, 2013

This currently leaves a difficult choice between having fallbacks or an optimised build.

Could some one explain why this can't be made to work properly?

I would save time for everyone if there were a definitive answer. Reducing the need for people opening duplicate issues here & questions on stackoverflow.
Surely the createConfig method could be made to correctly parse fallbacks
The method explicitly checks if paths is an array on line 25066 and throws an error if there is.

[Error: Error: paths fallback not supported in optimizer. Please provide a build config path override for jquery
at /Users/robaldred/Sites/nominet-infographic/node_modules/requirejs/bin/r.js:25066:23

raldred commented Dec 9, 2013

This currently leaves a difficult choice between having fallbacks or an optimised build.

Could some one explain why this can't be made to work properly?

I would save time for everyone if there were a definitive answer. Reducing the need for people opening duplicate issues here & questions on stackoverflow.
Surely the createConfig method could be made to correctly parse fallbacks
The method explicitly checks if paths is an array on line 25066 and throws an error if there is.

[Error: Error: paths fallback not supported in optimizer. Please provide a build config path override for jquery
at /Users/robaldred/Sites/nominet-infographic/node_modules/requirejs/bin/r.js:25066:23
@jrburke

This comment has been minimized.

Show comment
Hide comment
@jrburke

jrburke Jan 1, 2014

Member

@raldred: optimization is possible, just specify the paths that should work for the build in the build options, see more details above.

The problem with supporting it by default is dealing with network URLs, and the optimizer does not handle network URLs by default. In addition, a paths config fallback is somewhat at odds with optimization wishes: if the idea is to use a CDN location for the file, then it is hard to know in the build what should be done: does the user actually want to use an "empty:" build config to exclude that dependency and still load it from CDN after a build, or to truly inline the dependency from a local file?

By asking for the explicit indication of the desire via the build config, it makes it clear what the developer wants to happen.

Member

jrburke commented Jan 1, 2014

@raldred: optimization is possible, just specify the paths that should work for the build in the build options, see more details above.

The problem with supporting it by default is dealing with network URLs, and the optimizer does not handle network URLs by default. In addition, a paths config fallback is somewhat at odds with optimization wishes: if the idea is to use a CDN location for the file, then it is hard to know in the build what should be done: does the user actually want to use an "empty:" build config to exclude that dependency and still load it from CDN after a build, or to truly inline the dependency from a local file?

By asking for the explicit indication of the desire via the build config, it makes it clear what the developer wants to happen.

@dak

This comment has been minimized.

Show comment
Hide comment
@dak

dak Jun 27, 2014

@jrburke The solution mentioned works fine except for overriding paths in the main config file for modules that are lazy-loaded. As best I can tell, those paths remain completely unmodified when optimized.

dak commented Jun 27, 2014

@jrburke The solution mentioned works fine except for overriding paths in the main config file for modules that are lazy-loaded. As best I can tell, those paths remain completely unmodified when optimized.

@vdininski

This comment has been minimized.

Show comment
Hide comment
@vdininski

vdininski Aug 12, 2014

I am struggling to add cdn versions of some dependencies while using mainConfigFile and grunt-contrib-requirejs. An example would be:

module.exports = {
    compile: {
        options: {
           out: 'app/dist/app.min.js',
            name: 'main',
            mainConfigFile: "app/main.js",
            wrapShim: true,
            paths: {
                app: 'temp/app',
                templates: 'temp/templates',
                kendo: '//da7xgjtj801h2.cloudfront.net/2014.2.716/js/kendo.all.min',
                angular: '//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular'
            },
            exclude: ['config']
        }
    }
};

The dependencies for kendo and angular are not being replaced by their cdn versions. Not sure if this is supported or if I am doing something wrong. If I however swap the path in the mainConfigFile to a cdn it works like a charm.

I am struggling to add cdn versions of some dependencies while using mainConfigFile and grunt-contrib-requirejs. An example would be:

module.exports = {
    compile: {
        options: {
           out: 'app/dist/app.min.js',
            name: 'main',
            mainConfigFile: "app/main.js",
            wrapShim: true,
            paths: {
                app: 'temp/app',
                templates: 'temp/templates',
                kendo: '//da7xgjtj801h2.cloudfront.net/2014.2.716/js/kendo.all.min',
                angular: '//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular'
            },
            exclude: ['config']
        }
    }
};

The dependencies for kendo and angular are not being replaced by their cdn versions. Not sure if this is supported or if I am doing something wrong. If I however swap the path in the mainConfigFile to a cdn it works like a charm.

@jbraithwaite

This comment has been minimized.

Show comment
Hide comment
@jbraithwaite

jbraithwaite Oct 23, 2014

@vdininski Were you able to figure this out? I'm having the same issue.

@vdininski Were you able to figure this out? I'm having the same issue.

@grayghostvisuals

This comment has been minimized.

Show comment
Hide comment
@grayghostvisuals

grayghostvisuals Nov 2, 2014

For anyone looking for a clean and short answer here's my code and a screenshot of my directory structure. The code below has passed and verified to be working. This is also documented by requireJS

build.js

({
  mainConfigFile : "common.js",
  baseUrl: ".",
  removeCombined: true,
  findNestedDependencies: true,
  dir: "build",
  paths: {
    // https://github.com/jrburke/requirejs/issues/791
    // http://www.anthb.com/2014/07/04/optimising-requirejs-with-cdn-fallback
    'jquery': 'empty:'
  },
  modules: [
    {
      name: "main",
      exclude: ['jquery']
    }
  ]
})

common.js

requirejs.config({
  baseUrl: 'js/src',
  paths : {
    'jquery': [ '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min', '../lib/jquery-2.1.1.min' ]
  }
});

output

grayghostvisuals ✭ grayghostvisualsmacbookair.rochester.rr.com in ~/Sites/lemanz/js/src on reqjs*
⚡ node ../../node_modules/requirejs/bin/r.js -o build.js

Tracing dependencies for: main

Tracing dependencies for: jquery
Uglifying file: /Users/grayghostvisuals/Sites/lemanz/js/src/build/build.js
Uglifying file: /Users/grayghostvisuals/Sites/lemanz/js/src/build/lemanzanchorscroll.js
Uglifying file: /Users/grayghostvisuals/Sites/lemanz/js/src/build/main.js
Uglifying file: /Users/grayghostvisuals/Sites/lemanz/js/src/build/require.js
Uglifying file: /Users/grayghostvisuals/Sites/lemanz/js/src/build/utils.js

main.js
----------------
common.js
jqplugins.js
disablehover.js
globals.js
script.js
main.js

For anyone looking for a clean and short answer here's my code and a screenshot of my directory structure. The code below has passed and verified to be working. This is also documented by requireJS

build.js

({
  mainConfigFile : "common.js",
  baseUrl: ".",
  removeCombined: true,
  findNestedDependencies: true,
  dir: "build",
  paths: {
    // https://github.com/jrburke/requirejs/issues/791
    // http://www.anthb.com/2014/07/04/optimising-requirejs-with-cdn-fallback
    'jquery': 'empty:'
  },
  modules: [
    {
      name: "main",
      exclude: ['jquery']
    }
  ]
})

common.js

requirejs.config({
  baseUrl: 'js/src',
  paths : {
    'jquery': [ '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min', '../lib/jquery-2.1.1.min' ]
  }
});

output

grayghostvisuals ✭ grayghostvisualsmacbookair.rochester.rr.com in ~/Sites/lemanz/js/src on reqjs*
⚡ node ../../node_modules/requirejs/bin/r.js -o build.js

Tracing dependencies for: main

Tracing dependencies for: jquery
Uglifying file: /Users/grayghostvisuals/Sites/lemanz/js/src/build/build.js
Uglifying file: /Users/grayghostvisuals/Sites/lemanz/js/src/build/lemanzanchorscroll.js
Uglifying file: /Users/grayghostvisuals/Sites/lemanz/js/src/build/main.js
Uglifying file: /Users/grayghostvisuals/Sites/lemanz/js/src/build/require.js
Uglifying file: /Users/grayghostvisuals/Sites/lemanz/js/src/build/utils.js

main.js
----------------
common.js
jqplugins.js
disablehover.js
globals.js
script.js
main.js
@safakgur

This comment has been minimized.

Show comment
Hide comment
@safakgur

safakgur Mar 1, 2017

I've recently failed in all the above and asked this question on SO.

safakgur commented Mar 1, 2017

I've recently failed in all the above and asked this question on SO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment