forked from josdejong/workerpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
184 lines (147 loc) · 4.58 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var fs = require('fs');
var path = require('path');
var gulp = require('gulp');
var webpack = require('webpack');
var uglify = require('uglify-js');
var log = require('fancy-log');
var format = require('date-format');
var del = require('del');
var bundleFileName = 'workerpool.js'
var minifiedFileName = 'workerpool.min.js'
// generate banner with today's date and correct version
function createBanner() {
var today = format.asString('yyyy-MM-dd', new Date()); // today, formatted as yyyy-MM-dd
var version = require('./package.json').version; // module version
return String(fs.readFileSync('./src/header.js'))
.replace('@@date', today)
.replace('@@version', version);
}
var bannerPlugin = new webpack.BannerPlugin({
banner: createBanner(),
entryOnly: true,
raw: true
});
var webpackConfig = {
entry: './src/index.js',
mode: 'production',
output: {
library: 'workerpool',
libraryTarget: 'umd',
path: path.join(__dirname, 'dist'),
filename: bundleFileName,
umdNamedDefine: true,
globalObject: '(typeof self !== \'undefined\' ? self : this)'
},
plugins: [
bannerPlugin
],
node: false, // do not include node poly fills for process, __dirname, etc
optimization: {
// We no not want to minimize our code.
minimize: false
},
devtool: 'source-map'
};
var webpackMinifyConfig = {
...webpackConfig,
optimization: {
minimize: true
},
output: {
...webpackConfig.output,
filename: minifiedFileName
}
}
var webpackWorkerConfig = {
...webpackConfig,
entry: './src/worker.js',
output: {
path: path.join(__dirname, 'dist'),
filename: 'worker.js'
}
};
// create a single instance of the compiler to allow caching
var compiler = webpack(webpackConfig);
var compilerMinify = webpack(webpackMinifyConfig);
gulp.task('clean', function () {
return del([
'dist/**/*'
])
});
gulp.task('bundle-worker', function (done) {
webpack(webpackWorkerConfig).run(function (err, stats) {
if (err) {
log(err);
}
var info = stats.toJson();
if (stats.hasWarnings()) {
log('Webpack warnings:\n' + info.warnings.join('\n'));
}
if (stats.hasErrors()) {
log('Webpack errors:\n' + info.errors.join('\n'));
done(new Error('Compile failed'));
}
log('bundled worker ./dist/worker.js');
var result = uglify.minify(String(fs.readFileSync('./dist/worker.js')));
if (result.error) {
throw result.error;
}
// 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('./src/generated/embeddedWorker.js', embedded);
log('generated embedded worker ./src/generated/embeddedWorker.js');
done();
});
});
gulp.task('bundle-workerpool', function (done) {
// 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) {
log(err);
}
var info = stats.toJson();
if (stats.hasWarnings()) {
log('Webpack warnings:\n' + info.warnings.join('\n'));
}
if (stats.hasErrors()) {
log('Webpack errors:\n' + info.errors.join('\n'));
done(new Error('Compile failed'));
}
log(`bundled ./dist/${bundleFileName}`);
done();
});
});
gulp.task('minify-workerpool', function (done) {
// update the banner contents (has a date in it which should stay up to date)
bannerPlugin.banner = createBanner();
compilerMinify.run(function (err, stats) {
if (err) {
log(err);
}
var info = stats.toJson();
if (stats.hasWarnings()) {
log('Webpack warnings:\n' + info.warnings.join('\n'));
}
if (stats.hasErrors()) {
log('Webpack errors:\n' + info.errors.join('\n'));
done(new Error('Compile failed'));
}
log(`bundled ./dist/${bundleFileName}`);
done();
});
});
var tasks = gulp.series('clean', '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', gulp.series(tasks, function () {
gulp.watch(['index.js', 'src/**/*.js', '!src/generated/**'], tasks);
}));