forked from josdejong/workerpool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
146 lines (118 loc) · 3.93 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var fs = require('fs');
var gulp = require('gulp');
var gutil = require('gulp-util');
var webpack = require('webpack');
var uglify = require('uglify-js');
// generate banner with today's date and correct version
function createBanner() {
var today = gutil.date(new Date(), 'yyyy-mm-dd'); // today, formatted as yyyy-mm-dd
var version = require('./package.json').version; // module version
return String(fs.readFileSync('./lib/header.js'))
.replace('@@date', today)
.replace('@@version', version);
}
var bannerPlugin = new webpack.BannerPlugin(createBanner(), {
entryOnly: true,
raw: true
});
// TODO: webpack is quite compilicated to configure. Switch to browserify?
var FunctionModulePlugin = require('webpack/lib/FunctionModulePlugin');
var NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
var NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
var LoaderTargetPlugin = require('webpack/lib/LoaderTargetPlugin');
var webpackOutput = {
library: 'workerpool',
libraryTarget: 'umd',
path: './dist',
filename: 'workerpool.js'
};
var webpackNode = {
// do not include poly fills...
console: false,
process: false,
global: false,
buffer: false,
__filename: false,
__dirname: false
};
var webpackConfig = {
entry: './index.js',
target: function(compiler) {
compiler.apply(
new FunctionModulePlugin(webpackOutput),
new NodeTemplatePlugin(webpackOutput),
new NodeTargetPlugin(webpackNode),
new LoaderTargetPlugin('web')
);
},
output: webpackOutput,
node: webpackNode,
plugins: [
bannerPlugin
],
cache: true
};
var webpackWorkerConfig = {
entry: './lib/worker.js',
output: {
path: './dist',
filename: 'worker.js'
},
node: webpackNode,
plugins: []
};
var uglifyConfig = {
outSourceMap: 'workerpool.map',
output: {
comments: /@license/
}
};
// create a single instance of the compiler to allow caching
var compiler = webpack(webpackConfig);
gulp.task('bundle-worker', function (cb) {
webpack(webpackWorkerConfig).run(function (err, stats) {
if (err) {
gutil.log(err);
}
gutil.log('bundled worker ./dist/worker.js');
var result = uglify.minify(['./dist/worker.js']);
// create embeddedWorker.js
var embedded = '/**\n' +
' * embeddedWorker.js contains an embedded version of worker.js.\n' +
' * This file is automatically generated,\n' +
' * changes made in this file will be overwritten.\n' +
' */\n' +
'module.exports = ' + JSON.stringify(result.code) + ';\n';
fs.writeFileSync('./lib/generated/embeddedWorker.js', embedded);
gutil.log('generated embedded worker ./lib/generated/embeddedWorker.js');
cb();
});
});
gulp.task('bundle-workerpool', ['bundle-worker'], function (cb) {
// update the banner contents (has a date in it which should stay up to date)
bannerPlugin.banner = createBanner();
// note in browserify we would do something like:
// browserify ./index.js -o dist/workerpool.js -s workerpool --no-builtins --insert-global-vars none
compiler.run(function (err, stats) {
if (err) {
gutil.log(err);
}
gutil.log('bundled ./dist/workerpool.js');
cb();
});
});
gulp.task('minify-workerpool', ['bundle-workerpool'], function () {
var result = uglify.minify(['./dist/workerpool.js'], uglifyConfig);
var fileMap = 'workerpool.map';
fs.writeFileSync('./dist/workerpool.min.js', result.code);
fs.writeFileSync('./dist/workerpool.map', result.map);
gutil.log('Minified ' + './dist/workerpool.min.js');
gutil.log('Mapped ' + './dist/workerpool.map');
});
var tasks = ['bundle-worker', 'bundle-workerpool', 'minify-workerpool'];
// The default task (called when you run `gulp`)
gulp.task('default', tasks);
// The watch task (to automatically rebuild when the source code changes)
gulp.task('watch', tasks, function () {
gulp.watch(['index.js', 'lib/**/*.js', '!lib/generated/**'], tasks);
});