-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
176 lines (164 loc) · 4.47 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
/**
* Build scripts
*
* Copyright (c) 2017-2025 Alex Grant (@localnerve), LocalNerve LLC
* Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
*/
const fs = require('fs');
const path = require('path');
const util = require('util');
const { spawn } = require('child_process');
const gulp = require('gulp');
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const { getSourceDirs, srcRoot } = require('./src/utils/dirs');
const jsBundle = 'bundle.js';
/**
* Returns an array of webpack config objects that assume these constraints:
* 1. Produces one .js only bundle named `jsBundle`.
* 2. Entry point is index.js
*
* @param {String} env - The target env, 'production' for production.
* @param {Array} dirs - toplevel directories under srcRoot.
* @returns {Array} Webpack config objects.
*/
async function getWebpackConfig (env, dirs) {
const prod = env === 'production';
const definitions = {
DEBUG: !prod,
'process.env': {
NODE_ENV: JSON.stringify(env)
}
};
const plugins = [
new webpack.DefinePlugin(definitions)
];
const optimization = {};
if (prod) {
optimization.minimizer = [
new TerserPlugin({
terserOptions: {
compress: {
warnings: false
},
output: {
comments: false
}
}
})
];
}
// special case for jump-scroll, revisit...
const dir = dirs.find(dir => dir.includes('jump-scroll'));
if (dir) {
const { spawn } = require('child_process');
const cp = spawn('npm', ['run', 'build'], {
stdio: 'inherit',
cwd: dir
});
const result = new Promise((resolve, reject) => {
cp.on('close', code => {
if (code === 0) {
resolve();
} else {
reject();
}
});
});
await result;
fs.copyFileSync(
path.join(path.resolve(dir), 'dist/jump-scroll.js'),
path.join(path.resolve(dir), `${jsBundle}`)
);
}
const webpackDirs = dirs.filter(dir => !dir.includes('jump-scroll'));
return webpackDirs.map(dir => ({
mode: prod ? 'production' : 'development',
entry: `${path.join(dir, 'index.js')}`,
output: {
path: path.resolve(dir),
filename: `${jsBundle}`
},
module: {
rules: [{
test: /\.js$/,
exclude: /^\/node_modules/,
loader: 'babel-loader'
}]
},
plugins,
optimization
}));
}
/**
* Creates js bundles for all top level directories under `srcRoot`.
*
* @param {String} env - "production" for production.
* @returns {Promise} Resolves to undefined on completion.
*/
function createBundle (env) {
return getSourceDirs()
.then(getWebpackConfig.bind(null, env))
.then(webpackConfigs => util.promisify(webpack)(webpackConfigs))
.then(stats => {
if (stats.hasErrors()) {
throw stats.toJson().errors;
}
});
}
/**
* Run `npm install` for any src directory that needs it.
*
* @returns {Promise} That resolves if all npm installs succeed, rejects otherwise.
*/
function runPackageInstalls () {
return getSourceDirs()
.then(dirs => {
const pkgDirs = dirs.filter(dir => {
let exists = false;
try {
fs.accessSync(path.join(dir, 'package.json'));
exists = true;
} catch (e) {
exists = false;
}
return exists;
});
return Promise.all(pkgDirs.map(pkgDir => {
const cp = spawn('npm', ['install'], {
cwd: pkgDir
});
return new Promise((resolve, reject) => {
cp.on('close', code => {
if (code !== 0) {
return reject();
}
return resolve();
});
cp.on('error', reject);
});
}));
});
}
// Define the build tasks:
gulp.task(
'copy', () => gulp.src([
`${srcRoot}/**/*.html`,
`${srcRoot}/**/*.css`,
`${srcRoot}/**/*.jpg`,
`${srcRoot}/**/${jsBundle}`,
`${srcRoot}/**/*worker.js`,
`${srcRoot}/**/node_modules/**`,
`!${srcRoot}/horizontal-pager/node_modules`,
`!${srcRoot}/horizontal-pager/node_modules/**`,
`!${srcRoot}/jump-scroll/node_modules`,
`!${srcRoot}/jump-scroll/node_modules/**`,
`!${srcRoot}/**/test`,
`!${srcRoot}/**/test/**`,
], {
encoding: false
}).pipe(gulp.dest('dist'))
);
gulp.task('webpack', () => createBundle('production'));
gulp.task('webpack-dev', () => createBundle('development'));
gulp.task('installs', runPackageInstalls);