node package manager
Painless code sharing. npm Orgs help your team discover, share, and reuse code. Create a free org »



A require-bundler plugin for gulp.

Apart from normal bundling, this plugin also adds a require.config call to the final optimized script in which all paths are re-added that where removed from the build by using "empty:" during optimization.

Thanks to @SteveSanderson for the original gulp-requirejs-bundler and all his other great work!


First, install gulp-requirejs-cdnbundler as a development dependency:

npm install --save-dev gulp-requirejs-cdnbundler

Then, add it to your gulpfile.js (example):

var require-bundler = require("gulp-requirejs-cdnbundler");
gulp.task("requirejs", function() {
    var requireJsOptimizerConfig, requireJsRuntimeConfig;
    // get the "normal" config that is used during development. You can also just use some JS object like 
    // { 
    //     paths: { 
    //         foo: "path/to/foo" 
    //     }, 
    //     shim: { 
    //         someShimedModule: { 
    //             deps: ["foo"] 
    //         } 
    //     } 
    // } 
    requireJsRuntimeConfig = vm.runInNewContext(fs.readFileSync("src/app/require.config.js") + "; require;");
    // config that extends or overwrites the original config 
    requireJsOptimizerConfig = {
      out: "scripts.js",
      baseUrl: "./src",
      name: "app/startup",
      paths: {
        requireLib: "bower_modules/requirejs/require",
        socketio: "empty:"
      include: ["requireLib", "some/component"],
      insertRequire: ["app/startup"],
      bundles: {},
      optimize: "none"
    // call/return rjs with the two configurations. 
    return rjs(requireJsRuntimeConfig, requireJsOptimizerConfig)


require-bundler(requireJsRuntimeConfig, requireJsOptimizerConfig)

Takes two arguments:

  • the requirejs config
  • the r.js optimizer config

See requirejs config docs and requirejs optimization docs for possible options.


MIT License