-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
gulpfile.js
343 lines (318 loc) · 10.6 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const gulp = require('gulp')
const webpack = require('webpack')
const webpackst = require('webpack-stream')
// const eslint = require('gulp-eslint')
const sourcemaps = require('gulp-sourcemaps')
const rename = require('gulp-rename')
const path = require('path')
const { spawn, fork } = require('child_process')
const sass = require('gulp-sass')(require('sass'))
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const del = require('del')
const replace = require('gulp-replace')
const filter = require('gulp-filter')
const argv = require('minimist')(process.argv.slice(2));
const browserSync = require('browser-sync').create()
let gulp_bin = path.resolve(__dirname, "./node_modules/.bin/gulp")
let wp_target = ['web']
if (argv['target'] == 'es5') {
wp_target = ['web', 'es5']
}
let wpbuilds = []
let SRC_DIR = path.join(__dirname, 'src')
let NODE_MODULES_DIR = path.join(__dirname, 'node_modules')
wpbuilds.push({ name: 'main', entry: './src/main.js', dest: './html/webpack' })
class BuildCompilerDoneListenerPlugin {
apply (compiler) {
compiler.hooks.done.tap({ name: 'dispatch-compiler-done' }, () => {
if (BuildCompilerDoneListenerPlugin._listeners) {
for (let listener of BuildCompilerDoneListenerPlugin._listeners) {
listener(this, compiler)
}
}
})
}
static addListener (listener) {
if (!BuildCompilerDoneListenerPlugin._listeners) {
BuildCompilerDoneListenerPlugin._listeners = []
}
BuildCompilerDoneListenerPlugin._listeners.push(listener)
}
}
function wpdefine_options(is_production) {
return {
'process.env.IS_PRODUCTION': JSON.stringify(is_production),
'process.env.PASCO_BACKEND_URL': JSON.stringify(process.env.PASCO_BACKEND_URL || 'https://backend.pasco.chat'),
'process.env.DROPBOX_APP_KEY': JSON.stringify(process.env.DROPBOX_APP_KEY || ''),
}
}
gulp.task('script-lint', function () {
return gulp.src(['src/**/*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError())
})
function clean_webpack () {
return del([
`./html/webpack/**/*`,
`!./html/webpack/.gitignore`,
])
}
function make_build_webpack ({ build_name, env, watch }) {
let wpbuild = wpbuilds.find((a) => a.name == build_name)
let is_prod = env == 'production'
let webpack_build = () => {
return gulp.src('.')
.pipe(webpackst({
entry: wpbuild.entry,
target: wp_target,
watch,
output: {
filename: `${build_name}.js`,
assetModuleFilename: '[name]-[hash][ext]',
chunkFormat: 'array-push',
},
mode: env,
devtool: 'source-map',
performance: { hints: false },
resolve: {
modules: [ 'node_modules' ],
alias: {
},
fallback: {
'path': require.resolve('path-browserify'),
'stream': require.resolve('stream-browserify'),
},
},
module: {
rules: [
{
test: /\.js$/,
include: [
SRC_DIR,
...[
// empty
].map((a) => path.resolve(NODE_MODULES_DIR, a)),
],
use: {
loader: 'babel-loader',
options: {
presets: [['@babel/preset-env', {
useBuiltIns: 'usage',
corejs: '3.8',
}]],
plugins: ['@babel/plugin-transform-runtime'],
},
},
},
// execeptions in modules that need babel-loader run
{
test: /\.js$/,
include: [
[
'delay',
'rangeslider-js',
'sha256-uint8array',
'sanitize-html',
].map((a) => path.resolve(NODE_MODULES_DIR, a))
],
use: {
loader: 'babel-loader',
options: {
presets: [['@babel/preset-env', { modules: 'commonjs' }]],
plugins: ['@babel/plugin-transform-runtime'],
},
},
},
{ // rules for sass/css files
test: /\.s[ac]ss$/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader',
],
},
],
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
}),
new MiniCssExtractPlugin({
filename: `${build_name}.css`,
}),
new webpack.DefinePlugin(wpdefine_options(is_prod)),
new BuildCompilerDoneListenerPlugin(),
],
}, webpack))
.pipe(gulp.dest(wpbuild.dest))
}
return webpack_build
}
gulp.task('copy-webpack-static', () => {
return gulp.src('static/**/*')
.pipe(gulp.dest('html/webpack/static/'))
})
gulp.task('clean-webpack', clean_webpack)
{
let wpbuilders = wpbuilds.map((a) => make_build_webpack({ build_name: a.name, env: 'production' }))
gulp.task('build-script-prod', gulp.series('clean-webpack', 'copy-webpack-static', ...wpbuilders))
}
{
let wpbuilders = wpbuilds.map((a) => make_build_webpack({ build_name: a.name, env: 'development' }))
gulp.task('build-script-dev', gulp.series('clean-webpack', 'copy-webpack-static', ...wpbuilders))
}
{
let wpbuilders = wpbuilds.map((a) => make_build_webpack({ build_name: a.name, env: 'development', watch: true }))
gulp.task('build-script-dev:watch', gulp.series('clean-webpack', 'copy-webpack-static', ...wpbuilders))
}
gulp.task('dev', () => {
let initialBuildCalled = false
let initialBuild = () => {
browserSync.init({
files: [
'./html/**/*',
'!./html/webpack/**/*'
],
server: {
baseDir: "./html"
},
})
}
BuildCompilerDoneListenerPlugin.addListener((plugin, compiler) => {
if (!initialBuildCalled) {
initialBuildCalled = true
initialBuild()
} else {
browserSync.reload()
}
})
return gulp.series('build-script-dev:watch')()
})
gulp.task('build-prod', gulp.series(/*'script-lint', */'build-script-prod'))
gulp.task('build-dev', gulp.series(/*'script-lint', */'build-script-dev'))
function get_cordova_build_args () {
return [ argv['build-name'] ? argv['build-name'] : 'main-build' ]
}
gulp.task('clean-cordova-build', () => {
let [ build_name ] = get_cordova_build_args()
return del(`./builds/cordova-${build_name}`)
})
gulp.task('init-cordova-build', () => {
let [ build_name ] = get_cordova_build_args()
return gulp.src([
`./cordova-template/main-build/**/*`,
build_name != 'main-build' ? `./cordova-template/${build_name}/**/*` : null,
].filter((a) => !!a))
.pipe(gulp.dest(`./builds/cordova-${build_name}`))
})
function cordova_build_clean_www (build_name) {
return del([
`./builds/cordova-${build_name}/www/**/*`,
`!./builds/cordova-${build_name}/www/.gitignore`
])
}
function cordova_build_copy_html (build_name) {
let html_filter = filter(['**/*.html'], {restore: true})
return gulp.src([
`./html/**/*`,
])
// filter html files to perform replace on them
.pipe(html_filter)
// inject cordova.js
.pipe(replace('<!-- INJECT CORDOVA SCRIPTS -->', '<script src=\"cordova.js\"></script>'))
// since cordova builds only run local scripts and there's no runtime code execution fro the internet, CSP would not be necessary for it
// And the current CSP is not compatible with cordova build
// The simple solution is do remove it
.pipe(replace(/^\s*<meta\s+http-equiv="Content-Security-Policy".+>/gmi, ''))
// restore the filtered files
.pipe(html_filter.restore)
.pipe(gulp.dest(`./builds/cordova-${build_name}/www/`))
}
gulp.task('cordova-build-clean-www', () => {
let [ build_name ] = get_cordova_build_args()
return cordova_build_clean_www(build_name)
})
gulp.task('cordova-build-copy-html', () => {
let [ build_name ] = get_cordova_build_args()
return cordova_build_copy_html(build_name)
})
gulp.task('dist-to-cordova-build-dev', (done) => {
let [ build_name ] = get_cordova_build_args()
return gulp.series([
'build-dev',
cordova_build_clean_www.bind(null, build_name),
cordova_build_copy_html.bind(null, build_name),
])(done)
})
gulp.task('dist-to-cordova-build-prod', (done) => {
let [ build_name ] = get_cordova_build_args()
let output = gulp.series([
'build-prod',
cordova_build_clean_www.bind(null, build_name),
cordova_build_copy_html.bind(null, build_name),
])(done)
})
gulp.task('dist-to-cordova-build-dev-watch', (done) => {
let [ build_name ] = get_cordova_build_args()
let update = () => {
console.log('clean and copy html to cordova-build: ' + build_name)
gulp.series([ cordova_build_clean_www.bind(null, build_name),
cordova_build_copy_html.bind(null, build_name) ])()
}
let setNeedsUpdate_timeoutid = null
let setNeedsUpdate = () => {
if (setNeedsUpdate_timeoutid != null) {
clearTimeout(setNeedsUpdate_timeoutid)
}
setNeedsUpdate_timeoutid = setTimeout(() => {
update()
setNeedsUpdate_timeoutid = null
}, 1000)
}
BuildCompilerDoneListenerPlugin.addListener((plugin, compiler) => {
setNeedsUpdate(200)
})
let watcher = gulp.watch([
'./html/**/*',
'!./html/**/*#',
'!./html/**/*~',
'!./html/webpack/**/*',
])
watcher.on('change', (path, stats) => {
console.log(`File ${path} was changed`)
setNeedsUpdate()
})
watcher.on('add', (path, stats) => {
console.log(`File ${path} was added`)
setNeedsUpdate()
})
watcher.on('unlink', (path, stats) => {
console.log(`File ${path} was removed`)
setNeedsUpdate()
})
return gulp.series('build-script-dev:watch')(done)
})
gulp.task('default', function (done) {
console.log(`
Available commands:
- script-lint
- build-script-dev
- build-script-prod
- build-script-dev:watch
- build-prod
- build-dev
- dev (run with webpack watch)
- clean-cordova-build --build-name <build_name (default: main-build)>
- init-cordova-build --build-name <build_name (default: main-build)>
- dist-to-cordova-build-dev-watch --build-name <build_name (default: main-build)>
- dist-to-cordova-build-dev --build-name <build_name (default: main-build)>
- dist-to-cordova-build-prod --build-name <build_name (default: main-build)>
- cordova-build-clean-www --build-name <build_name (default: main-build)>
- cordova-build-copy-html --build-name <build_name (default: main-build)>
Other parameters:
--target es5 # set the webpack target build to es5 (used for older web browsers)
`)
done()
})