1+ var gutil = require ( 'gulp-util' ) ,
2+ requirejs = require ( 'requirejs' ) ,
3+ PluginError = gutil . PluginError ,
4+ File = gutil . File ,
5+ es = require ( 'event-stream' ) ;
6+
7+ // Consts
8+ const PLUGIN_NAME = 'gulp-requirejs' ;
9+
10+
11+ module . exports = function ( opts ) {
12+
13+ 'use strict' ;
14+
15+ if ( ! opts ) {
16+ throw PluginError ( PLUGIN_NAME , 'Missing options array!' ) ;
17+ }
18+
19+ if ( ! opts . out && typeof opts . out !== 'string' ) {
20+ throw PluginError ( PLUGIN_NAME , 'Only single file outputs are supported right now, please pass a valid output file name!' ) ;
21+ }
22+
23+ if ( ! opts . baseUrl ) {
24+ throw PluginError ( PLUGIN_NAME , 'Piping dirs/files is not supported right now, please specify the base path for your script.' ) ;
25+ }
26+
27+ // create the stream and save the file name (opts.out will be replaced by a callback function later)
28+ var _s = es . pause ( ) ,
29+ _fName = opts . out ;
30+
31+ // just a small wrapper around the r.js optimizer, we write a new gutil.File (vinyl) to the Stream, mocking a file, which can be handled
32+ // regular gulp plugins (i hope...).
33+ optimize ( opts , function ( text ) {
34+ _s . write ( new File ( {
35+ path : _fName ,
36+ contents : new Buffer ( text )
37+ } ) ) ;
38+ } ) ;
39+
40+ // return the stream for chain .pipe()ing
41+ return _s ;
42+ }
43+
44+ // a small wrapper around the r.js optimizer
45+ function optimize ( opts , cb ) {
46+ opts . out = cb ;
47+ opts . optimize = 'none' ;
48+ requirejs . optimize ( opts ) ;
49+ }
0 commit comments