gulp-inject-modules

1.0.0 • Public • Published

gulp-inject-modules

NPM Version NPM Downloads

Loads JavaScript files on-demand from a Gulp stream into Node's module loader.

The motivation for this plugin is so you can write your tests using ES2015, transpile them using Babel, and run them using mocha without writing the transpiled code to disk first. The problem is mocha uses require() to load the tests. Writing the transpiled code to disk is inefficient and unnecessary. How do you get the transpiled tests into mocha so that it can run them without writing them to disk? That's where gulp-inject-modules comes in.

Installation

npm install gulp-inject-modules --save-dev

Usage

Here's an example using gulp-babel, gulp-mocha, and gulp-filter to transpile all your JavaScript source and unit tests, then inject the code into the module loader before calling Mocha to run the unit tests.

var babel = require('gulp-babel');
var filter = require('gulp-filter');
var injectModules = require('gulp-inject-modules');
var mocha = require('gulp-mocha');
 
gulp.task('test', [ /* tasks to lint code and clean directories */ ], function () {
    return gulp
        .src(['src/**/*.js', 'test/**/*.js']) // get all source and test files
        .pipe(babel()) // transpile the source and tests
        .pipe(injectModules()) // load the transpiled code into Node's module system
        .pipe(filter('test/**/*.js')) // mocha only likes js files
        .pipe(mocha()); // run the tests
});

License

(The MIT License)

Copyright (c) 2016 Chris Barber

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i gulp-inject-modules

Weekly Downloads

137

Version

1.0.0

License

MIT

Last publish

Collaborators

  • cb1kenobi