gulp-wrap-commonjs-sourcemaps
v0.1.14
Published
Wrap files into a CommonJS module definition compatible with the Node.js require() API.
Downloads
2
Maintainers
Readme
Information
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.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/)