gulp-wrap-commonjs
Wrap files into a CommonJS module definition compatible with the Node.js require() API.
Last updated 4 years ago by efacilitation .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install gulp-wrap-commonjs 
SYNC missed versions from official npm registry.

Information

Package gulp-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/)

Current Tags

  • 0.1.14                                ...           latest (4 years ago)

4 Versions

  • 0.1.14                                ...           4 years ago
  • 0.1.13                                ...           6 years ago
  • 0.1.12                                ...           6 years ago
  • 0.1.11                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (5)
Dev Dependencies (3)
Dependents (2)

Copyright 2014 - 2016 © taobao.org |