简体   繁体   中英

RequireJS: optimizer generates name for define module

I'm using gulp.js and an optimization tool for requirejs (gulp-requirejs) it combines all scritps into one file. I have one define module with no name but it generates a name for it. The problem is I don't know how to call that module from another file.

For example in page1.js:

...
define("map",["jquery"],function($){
   ...
});
define("lib", ["jquery"],function($){
   ...
});

and in page2.js I would like to call page1.js lib module but I am not sure how to do it? I would prefer if the optimization tool did not set a name then my code works but this way I have no idea how to make it work. Any ideas?

It is not possible to use multiple modules in a single file, unless these modules are named. Otherwise RequireJS won't know how to relate a request for a module with the actual code that defines it.

The typical scenario when optimizing all modules into a single bundle is that there is going to be one specific module in the bundle which serves as the entry point of your application, and then you can just put that module name in paths :

require.config({
  paths: {
    lib: 'path/to/page1',
  }
});

If you do not have a single entry point but may in fact also have code outside the bundle that will initiate loading modules that are in the bundle , then you need to list those modules in bundles :

require.config({
  paths: {
    lib: 'path/to/page1',
  },
  bundles: {
    lib: ['map', ...],
  }
});

The bundles setting I have shown above says essentially "when you look for the module named map , fetch the module lib , and you will have the definition of map ."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM