-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
70 lines (63 loc) · 2.49 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const gulp = require( 'gulp' ),
ClosureCompiler = require( 'google-closure-compiler').gulp();
PluginError = require( 'plugin-error' ),
pluginName = 'gulp-es2-to-es3';
/* -------------------------------------------------------
* gulp prepublish
*/
const TASK_LIST = [];
const POLYFILL_LIST = [
'Array.prototype.indexOf' , '',
'Array.prototype.pop' , '',
'Array.prototype.push' , '',
'Array.prototype.shift' , '',
'Array.prototype.splice' , '',
'Array.prototype.unshift' , '',
'decodeURIComponent' , 'ADVANCED',
'encodeURIComponent' , 'ADVANCED',
'Function.prototype.apply', 'WHITESPACE_ONLY',
'Function.prototype.call' , ''
];
const POLYFILL_CODES = {};
for( let i = 0, l = POLYFILL_LIST.length; i < l; i += 2 ){
TASK_LIST.push( createTask( POLYFILL_LIST[ i ], POLYFILL_LIST[ i + 1 ] ) );
};
TASK_LIST.push(
function( callback ){
require( 'fs' ).writeFile( './polyfills.json', JSON.stringify( POLYFILL_CODES, null, ' ' ), callback );
}
);
function createTask( builtInName, compilationLevel ){
return function(){
return gulp.src(
[ './src/' + builtInName + '.js' ]
).pipe(
ClosureCompiler(
{
compilation_level : compilationLevel || 'SIMPLE',
warning_level : 'VERBOSE',
language_in : 'ECMASCRIPT3',
language_out : 'ECMASCRIPT3',
js_output_file : 'polyfill.js'
}
)
).pipe(
require( 'through2' ).obj(
function( file, encoding, callback ){
if( file.isNull() ) return callback();
if( file.isStream() ){
this.emit( 'error', new PluginError( pluginName, 'Streaming not supported' ) );
return callback();
};
try {
POLYFILL_CODES[ builtInName ] = file.contents.toString( encoding ).replace( /\s+$/, '' );
} catch(O_o){
this.emit( 'error', new PluginError( pluginName, O_o ) );
};
callback();
}
)
);
};
};
gulp.task( 'prepublish', gulp.series.apply( gulp, TASK_LIST ) );