Skip to content

Commit

Permalink
Add option to build a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Georg Kallidis authored and jorrit committed May 20, 2019
1 parent 6a921c9 commit b56338a
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
test/expected/*.js
test/expected/*/*.js
test/tmp/*
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
npm-debug.log
coverage
.idea
test/tmp
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ gulp.task('requirejsBuild', function() {
});
```

If you use instead of out the dir option, you do not need the pipe at all, see this example in Gulp 4 syntax and mocha test:
```javascript
...
const rjs = require('gulp-requirejs');

async function requirejsBuild(cb) {
return rjs({
dir: 'deploy',
mainConfigFile: 'config.js',
path: {
'config': '../config_init'
},
modules: [{
name: 'FILENAME_TO_BE_OUTPUTTED', // no extension
include : [ .. ]
...
}]
}) ...
};

exports.requirejsBuild = requirejsBuild;


Note: In order to let gulp know that the optimization completes, return the rjs stream.

See [requirejs.org](https://requirejs.org/docs/optimization.html) for more information about the supported parameters.
Expand Down
17 changes: 10 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ function validateOptions(opts) {
throw new PluginError(PLUGIN_NAME, 'Missing options object.');
}

if (!opts.out && typeof opts.out !== 'string') {
throw new PluginError(PLUGIN_NAME, 'Only single file outputs are ' +
if ( !opts.out && typeof opts.out !== 'string'
&& !opts.dir && typeof opts.dir !== 'string' ) {
throw new PluginError(PLUGIN_NAME, 'Either single file outputs are ' +
'supported right now, please pass a valid output file name as the out ' +
'option.');
}
Expand Down Expand Up @@ -49,15 +50,17 @@ module.exports = function(opts) {
// create the stream and save the file name
// (opts.out will be replaced by a callback function later)
var stream = es.pause();
var filename = opts.out;
var filename = opts.out || opts.dir;
var output = null;
var sourceMapOutput = null;

// Set .out to a function to catch result text and sourcemap.
opts.out = function(text, sourceMap) {
output = text;
sourceMapOutput = sourceMap;
};
if (opts.out) {
opts.out = function(text, sourceMap) {
output = text;
sourceMapOutput = sourceMap;
};
}

var success = function(buildResponse) {
stream.write(createFile(filename, output, buildResponse, sourceMapOutput));
Expand Down
70 changes: 70 additions & 0 deletions test/expected/complex/complex_init_dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
define('simple_amd_file',[],function() {

var Mult = function(a, b) {
return a * b;
};

return Mult;

});

// src for the wrapper: https://github.com/umdjs/umd/blob/master/amdWeb.js
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define('umd_file',factory);
} else {
// Browser globals
root.amdWeb = factory(root.b);
}
}(this, function() {
//use b in some fashion.

// Just return a value to define the module export.
// This example returns an object, but the module
// can return a function as the exported value.
return {
test: function() {
console.log('Test Log from the UMD file');
}
};
}));

(function(root) {

root.myLib = {};

root.myLib.sum = function(a, b) {
return a + b;
}; // END PROTYPE OF sum

})(this);

define("non_md_file", (function (global) {
return function () {
var ret, fn;
return ret || global.myLib;
};
}(this)));

define('complex_amd_file',['non_md_file', 'simple_amd_file'], function(MyLib, mult) {

var SumMulti = function(a, b) {
return mult(MyLib.sum(a, b), b);
};

return SumMulti;

});

requirejs(['simple_amd_file', 'umd_file', 'complex_amd_file'],
function(mult, UMDLib, sumMulti) {
console.log('executing the COMPLEX init file');
console.log(mult(3, 5), '<= this should be 15');
UMDLib.test(); // should log 'Test Log from the UMD file'
console.log(sumMulti(5, 8), '<= this should be 104');
}
);

define("complex_init_dir", function(){});

8 changes: 8 additions & 0 deletions test/fixtures/complex_init_dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
requirejs(['simple_amd_file', 'umd_file', 'complex_amd_file'],
function(mult, UMDLib, sumMulti) {
console.log('executing the COMPLEX init file');
console.log(mult(3, 5), '<= this should be 15');
UMDLib.test(); // should log 'Test Log from the UMD file'
console.log(sumMulti(5, 8), '<= this should be 104');
}
);
11 changes: 11 additions & 0 deletions test/fixtures/config_init_dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//The build will inline common dependencies into this file.
requirejs.config({
paths: {
'complex_init_dir': '../complex_init_dir',
},
shim: {
'non_md_file': {
exports: 'myLib'
}
}
});
40 changes: 38 additions & 2 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,42 @@ describe('gulp-requirejs', function() {
});
});

describe('amd dir with shim', function() {
var pathname = 'test/tmp/';
var moduleName = 'complex_init_dir';
it('should concat the files in the correct order into modules.name, and build wrappers for the shimmed files', function(done) {
grjs({
mainConfigFile: 'test/fixtures/config_init_dir.js',
dir: pathname,
path: {
'config': '../config_init_dir'
},
modules: [{
name: moduleName, // no extension
includes: [
'simple_amd_file',
'umd_file',
'complex_amd_file',
'non_md_file'
]
}],
enforceDefine: true,
baseUrl: 'test/fixtures/vendor',
optimize: 'none',
findNestedDependencies: true
});
var contents = fs.readFileSync( 'test/expected/complex/' + moduleName + '.js', 'utf8');
var length = contents.length;
// wait, as require.js deletes and copies files
setTimeout(function(){
var test = fs.readFileSync( pathname + moduleName + '.js', 'utf8');
test.length.should.equal(length);
test.should.equal(contents);
done();
}, 1);
});
});

describe('ERRORS: ', function() {

it('should throw an error if we forget to pass in an options object', function(done) {
Expand All @@ -194,13 +230,13 @@ describe('gulp-requirejs', function() {
});


it('should throw an error if we forget to set the output', function(done) {
it('should throw an error if we forget to set the output (out or dir)', function(done) {

(function() {
grjs({
baseUrl: 'test/dir'
});
}).should.throwError(/^Only.*/);
}).should.throwError(/^Either.*/);

done();
});
Expand Down

0 comments on commit b56338a

Please sign in to comment.