Skip to content

Commit b56338a

Browse files
Georg Kallidisjorrit
authored andcommitted
Add option to build a directory
1 parent 6a921c9 commit b56338a

File tree

8 files changed

+163
-9
lines changed

8 files changed

+163
-9
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
test/expected/*.js
2+
test/expected/*/*.js
3+
test/tmp/*

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
npm-debug.log
44
coverage
55
.idea
6+
test/tmp

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ gulp.task('requirejsBuild', function() {
5959
});
6060
```
6161

62+
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:
63+
```javascript
64+
...
65+
const rjs = require('gulp-requirejs');
66+
67+
async function requirejsBuild(cb) {
68+
return rjs({
69+
dir: 'deploy',
70+
mainConfigFile: 'config.js',
71+
path: {
72+
'config': '../config_init'
73+
},
74+
modules: [{
75+
name: 'FILENAME_TO_BE_OUTPUTTED', // no extension
76+
include : [ .. ]
77+
...
78+
}]
79+
}) ...
80+
};
81+
82+
exports.requirejsBuild = requirejsBuild;
83+
84+
6285
Note: In order to let gulp know that the optimization completes, return the rjs stream.
6386

6487
See [requirejs.org](https://requirejs.org/docs/optimization.html) for more information about the supported parameters.

index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ function validateOptions(opts) {
1111
throw new PluginError(PLUGIN_NAME, 'Missing options object.');
1212
}
1313

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

5657
// Set .out to a function to catch result text and sourcemap.
57-
opts.out = function(text, sourceMap) {
58-
output = text;
59-
sourceMapOutput = sourceMap;
60-
};
58+
if (opts.out) {
59+
opts.out = function(text, sourceMap) {
60+
output = text;
61+
sourceMapOutput = sourceMap;
62+
};
63+
}
6164

6265
var success = function(buildResponse) {
6366
stream.write(createFile(filename, output, buildResponse, sourceMapOutput));
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
define('simple_amd_file',[],function() {
2+
3+
var Mult = function(a, b) {
4+
return a * b;
5+
};
6+
7+
return Mult;
8+
9+
});
10+
11+
// src for the wrapper: https://github.com/umdjs/umd/blob/master/amdWeb.js
12+
(function (root, factory) {
13+
if (typeof define === 'function' && define.amd) {
14+
// AMD. Register as an anonymous module.
15+
define('umd_file',factory);
16+
} else {
17+
// Browser globals
18+
root.amdWeb = factory(root.b);
19+
}
20+
}(this, function() {
21+
//use b in some fashion.
22+
23+
// Just return a value to define the module export.
24+
// This example returns an object, but the module
25+
// can return a function as the exported value.
26+
return {
27+
test: function() {
28+
console.log('Test Log from the UMD file');
29+
}
30+
};
31+
}));
32+
33+
(function(root) {
34+
35+
root.myLib = {};
36+
37+
root.myLib.sum = function(a, b) {
38+
return a + b;
39+
}; // END PROTYPE OF sum
40+
41+
})(this);
42+
43+
define("non_md_file", (function (global) {
44+
return function () {
45+
var ret, fn;
46+
return ret || global.myLib;
47+
};
48+
}(this)));
49+
50+
define('complex_amd_file',['non_md_file', 'simple_amd_file'], function(MyLib, mult) {
51+
52+
var SumMulti = function(a, b) {
53+
return mult(MyLib.sum(a, b), b);
54+
};
55+
56+
return SumMulti;
57+
58+
});
59+
60+
requirejs(['simple_amd_file', 'umd_file', 'complex_amd_file'],
61+
function(mult, UMDLib, sumMulti) {
62+
console.log('executing the COMPLEX init file');
63+
console.log(mult(3, 5), '<= this should be 15');
64+
UMDLib.test(); // should log 'Test Log from the UMD file'
65+
console.log(sumMulti(5, 8), '<= this should be 104');
66+
}
67+
);
68+
69+
define("complex_init_dir", function(){});
70+

test/fixtures/complex_init_dir.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
requirejs(['simple_amd_file', 'umd_file', 'complex_amd_file'],
2+
function(mult, UMDLib, sumMulti) {
3+
console.log('executing the COMPLEX init file');
4+
console.log(mult(3, 5), '<= this should be 15');
5+
UMDLib.test(); // should log 'Test Log from the UMD file'
6+
console.log(sumMulti(5, 8), '<= this should be 104');
7+
}
8+
);

test/fixtures/config_init_dir.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//The build will inline common dependencies into this file.
2+
requirejs.config({
3+
paths: {
4+
'complex_init_dir': '../complex_init_dir',
5+
},
6+
shim: {
7+
'non_md_file': {
8+
exports: 'myLib'
9+
}
10+
}
11+
});

test/main.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,42 @@ describe('gulp-requirejs', function() {
171171
});
172172
});
173173

174+
describe('amd dir with shim', function() {
175+
var pathname = 'test/tmp/';
176+
var moduleName = 'complex_init_dir';
177+
it('should concat the files in the correct order into modules.name, and build wrappers for the shimmed files', function(done) {
178+
grjs({
179+
mainConfigFile: 'test/fixtures/config_init_dir.js',
180+
dir: pathname,
181+
path: {
182+
'config': '../config_init_dir'
183+
},
184+
modules: [{
185+
name: moduleName, // no extension
186+
includes: [
187+
'simple_amd_file',
188+
'umd_file',
189+
'complex_amd_file',
190+
'non_md_file'
191+
]
192+
}],
193+
enforceDefine: true,
194+
baseUrl: 'test/fixtures/vendor',
195+
optimize: 'none',
196+
findNestedDependencies: true
197+
});
198+
var contents = fs.readFileSync( 'test/expected/complex/' + moduleName + '.js', 'utf8');
199+
var length = contents.length;
200+
// wait, as require.js deletes and copies files
201+
setTimeout(function(){
202+
var test = fs.readFileSync( pathname + moduleName + '.js', 'utf8');
203+
test.length.should.equal(length);
204+
test.should.equal(contents);
205+
done();
206+
}, 1);
207+
});
208+
});
209+
174210
describe('ERRORS: ', function() {
175211

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

196232

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

199235
(function() {
200236
grjs({
201237
baseUrl: 'test/dir'
202238
});
203-
}).should.throwError(/^Only.*/);
239+
}).should.throwError(/^Either.*/);
204240

205241
done();
206242
});

0 commit comments

Comments
 (0)