gulp-requirejs-cdnbundler

1.0.1 • Public • Published

gulp-requirejs-cdnbundler

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!

Usage

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)
    .pipe(gulp.dest("./dist/"));
  });

API

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.

License

MIT License

Readme

Keywords

Package Sidebar

Install

npm i gulp-requirejs-cdnbundler

Weekly Downloads

2

Version

1.0.1

License

MIT

Last publish

Collaborators

  • docnoe