javascriptnode.jsrequirejsrequirejs-optimizer

r.js evaluates 'text' plugin despite on 'stubModules' param


some code at first:

My r.js boot file, which i run by r.js.cmd -o static/js/boot.js

({
    baseUrl: './',
    preserveLicenseComments: false,
    name: 'boot',
    stubModules: ['text'],
    mainConfigFile: './requirejs/config.js',
    out: 'build.min.js',
    //paths: {
    //    'text': 'plugins/requirejs.text'
    //},
})

then plugin throws exception in console:

Error: Error: Loader plugin did not call the load callback in the build:
text:
    text!langJSON: Error: ENOENT, no such file or directory 'C:\web\lang\main'
    text!/web/downloads/links.json: Error: ENOENT, no such file or directory 'C:\web\downloads\links.json'

Could someone answer to me why r.js evaluates 'text' plugin despite on 'stubModules' param in build profile properties?

I've read this articles before:

  1. How can I prevent the Require.js optimizer from including the text plugin in optimized files?
  2. Inlining require.js text! using Grunt

Thanks in advance.


Solution

  • stubModules does not tell the optimizer to skip everything that loads through the text plugin. It merely tells the optimizer to replace the plugin with this code in the final bundle:

    define('text',{load: function(id){throw new Error("Dynamic load not allowed: " + id);}});
    

    This is useful if you are creating a bundle that will contain every single module that your code needs. If this is the case, then all modules loaded through text will already be in the bundle, and thus including the code of the text plugin inside the bundle is pointless because it won't be used. (In this case the plugin is used at build time but not at run time.)

    The errors you get are because the optimizer is still trying to include in your bundle those modules loaded through text, but it is not finding the files.

    If what you want to do is exclude from the bundle everything that loads through text, one thing you could do is add a paths to your build configuration that lists these modules as empty:. For instance:

    paths: {
        'web/downloads/links.json': 'empty:',
        ...
    }
    

    but then you will need the text plugin at run time to load the text, so you should remove the stubModules.