gulp-wrap-commonjs

Wrap files into a CommonJS module definition compatible with the Node.js require() API.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
gulp-wrap-commonjs
500.1.148 years ago10 years agoMinified + gzip package size for gulp-wrap-commonjs in KB

Readme

Information

Packagegulp-wrap-commonjs
Description Wrap files into a CommonJS module definition compatible with the Node.js require() API.

Usage

First, install gulp-wrap-commonjs as a development dependency:
npm install --save-dev gulp-wrap-commonjs

Then, add it to your gulpfile.js:
var wrapCommonjs = require('gulp-wrap-commonjs');

gulp.task('commonjs', function(){
  gulp.src(['lib/*.js'])
    .pipe(wrapCommonjs())
    .pipe(gulp.dest('build/'));
});

Works with JavaScript- and CoffeeScript-Files.
Supports sourcemaps. Just add a proper pipe in a sourcemaps.init() stream from gulp-sourcemaps.

CommonJS Require

You need a require.register function in the scope where you add the wrapped files. It's recommended to use commonjs-require for this purpose.

API

commonjsWrap(options)

options.autoRequire

Type: Boolean Default: false
Whether to append a require() on the filepath directly after the wrap.
Example:
var commonjsWrap = require('gulp-wrap-commonjs');

gulp.task('commonjs', function(){
  gulp.src(['lib/*.js'])
    .pipe(commonjsWrap({
      autoRequire: true
    }))
    .pipe(gulp.dest('build/'));
});

options.relativePath

Type: String Default: false
Allows you to set a base directory, which will allow modules to use relative paths.
Example:
var commonjsWrap = require('gulp-wrap-commonjs');

gulp.task('commonjs', function(){
  gulp.src(['lib/*.js'])
    .pipe(commonjsWrap({
      relativePath: 'lib'
    }))
    .pipe(gulp.dest('build/'));
});

produces modules that look like:
require.register("module.js", function(exports, require, module){

instead of
require.register("/path/to/project/lib/module.js", function(exports, require, module){

options.pathModifier

Type: Function Default: false
Allows you to set a function in which you can modify the filepath.
Example:
var commonjsWrap = require('gulp-wrap-commonjs');

gulp.task('commonjs', function(){
  gulp.src(['lib/*.js'])
    .pipe(commonjsWrap({
      pathModifier: function (path) {
        path = path.replace /.js$/, ''
        return path
      }
    }))
    .pipe(gulp.dest('build/'));
});

options.moduleExports

Type: Function or String Default: null
Allows you to set a module.exports at the end of the content
Example using Jade:
var wrapCommonjs = require('gulp-wrap-commonjs');

gulp.task('commonjs', function(){
  gulp.src(['lib/*.jade'])
    .pipe(wrapCommonjs({moduleExports: "template"}))
    .pipe(gulp.dest('build/'));
});

When passed in a function the value of module.exports can be determined dynamically. When used the path of each processed file is passed as argument to the function. If the function returns null or undefined no module.exports will be set.
var wrapCommonjs = require('gulp-wrap-commonjs');

gulp.task('commonjs', function(){
  gulp.src(['lib/*.jade'])
    .pipe(wrapCommonjs({
      moduleExports: function(path) {
        if (path.indexOf('some-module') > 0) {
          return 'someExports';
        }
      }
    }))
    .pipe(gulp.dest('build/'));
});

options.coffee

Type: Boolean Default: false
Force wrapping into CoffeeScript. By default this will get set by detecting the file-extension .coffee.
Example:
var wrapCommonjs = require('gulp-wrap-commonjs');

gulp.task('commonjs', function(){
  gulp.src(['lib/*.txt'])
    .pipe(wrapCommonjs({coffee: true}))
    .pipe(gulp.dest('build/'));
});

License

MIT
Copyright (c) 2014 efa GmbH (http://efa-gmbh.com/)