-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
469 lines (418 loc) · 14.5 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*!
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 2018 jeffy-g hirotom1107@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
// @ts-check
"use strict";
//
// imports
//
const path = require("path");
const fs = require("fs");
const del = require("del");
const pug = require("pug");
const gulp = require("gulp");
const greplace = require("gulp-replace");
/** @type {{ [key: string]: any }} */
// @ts-ignore cannot apply "resolveJsonModule" option
const thisPackage = require("./package.json");
const utils = require("./etc/utils");
//
// constants
//
/** ts compiled out put. */
const JS_DEST_DIR = "./build";
const SRC_DIR = "./src";
const SCSS_ROOT = `${SRC_DIR}/css`;
// "target", "module" property
const TSConfigPath = "./tsconfig-base.json";
// if need optional parametar.
/**
* @typedef {object} BuildSettings
* @prop {boolean} [debug]
* @prop {boolean} [publish] + means use bundle script and shift css resource url
* + add google analytics code
* + ⚠️ Can not be used with electron option
* @prop {boolean} [electron] build for electron (shift jquery cdn url etc)
* @prop {boolean} [firebase] build for firebase site
* @prop {boolean} [webpack]
* @prop {boolean} [mangle] enable mangle for terser minifier
* @prop {boolean} [noMinify] disable minifier
* @prop {boolean} [nostrip] do not use "rm-cstyle-cmts" at not "production" build
* @prop {"production" | "development"} [mode] specify webpack build mode (default: development)
* @prop {string} [file] for `zip` task. can compress single resource.
* @prop {string} [comment] for `zip` task. inject comment to compressed file.
* @prop {string} [pugOutDir]
*
* @prop {boolean} [major] version major?
* @prop {boolean} [minor] version minor?
*/
/**
* @type {BuildSettings}
*/
const settings = utils.getExtraArgs({ startIndex: 3, prefix: "--" }, true);
/** copy transpiled code for dist package. */
const COPY_FILEs = [
`${SRC_DIR}/**/{main.css${settings.mode === "production" ? "": "*"},style.css,react-table.css,*.jpg}`,
`${SRC_DIR}/**/!(jquery).js`,
`${SRC_DIR}/**/!(*-web_page_load).png`, // OK
`!${SRC_DIR}/eve/**/*.png`, // OK
];
// ------------------------------- shared function ------------------------------- //
/**
* delete files by "globs" pattern.
* done callback(gulp) specified if need.
* @param {string|string[]} globs file pattern.
* @param {() => void} done gulp callback function.
*/
function _del_hook(globs, done) {
del(globs, { force: true }).then(paths => {
console.log(`Deleted files and folders:\n${paths.join("\n")}`);
done && done();
});
}
/**
*
* @param {string} sourceFile
* @param {string} comment
*/
function _zip(sourceFile, comment) {
utils.compressScript(sourceFile, comment);
}
// gulp zip --file ./build/webpack.js --comment "this is test"
gulp.task("zip", done => {
const { file, comment } = settings;
if (file) {
_zip(file, comment);
} else {
console.log(`usage: gulp zip --file [js path] --comment [zip entry comment]`);
}
done && done();
});
// for "webpack"
/**
* it is bundled in webpack.js, other code becomes unnecessary.(at webpack
* @param {() => void} done gulp callback function.
* @requires openssl binary executable
*/
function postWebpacked(done) {
console.log();
console.log(" ---> running postWebpacked");
const { exec } = require("child_process");
/** @type {(src: string, signal?: () => void) => void} */
const digestAndCompress = (scriptPath, doneSignal) => {
exec(`openssl dgst -sha384 -binary ${path.normalize(scriptPath)} | openssl base64 -A`,
// exec(`openssl dgst -sha384 -binary < ${path.normalize(scriptPath)} | openssl base64 -A`,
(err, stdout, stderr) => {
if (err) {
console.error(err);
} else {
const sha384 = `sha384-${stdout}`;
console.log(sha384);
_zip(scriptPath, sha384);
}
if (doneSignal) {
doneSignal();
}
}
);
};
const existsTypeMap = fs.existsSync(`${JS_DEST_DIR}/typeid-map.js`);
digestAndCompress(`${JS_DEST_DIR}/webpack.js`, !existsTypeMap? done: void 0);
console.log();
if (existsTypeMap) {
digestAndCompress(`${JS_DEST_DIR}/typeid-map.js`, done);
}
console.log(" <--- running postWebpacked");
}
/**
* **replace main script element `src` attribute by `mainJsName`**.
*
* @param {string} mainJsName main entry script name.
* @param {string} dest resource copy destination.
* @param {() => void} [done] notify callback. (optional
*/
function copyResource(mainJsName, dest, done) {
// copy index.html and replace main script name.
gulp.src("./src/index.html").pipe( // always replace.
greplace(/init.(?:js|ts)/, mainJsName)
).pipe(gulp.dest(dest));
gulp.src("./callback/index.html").pipe(gulp.dest(dest + "/callback/"));
gulp.src(COPY_FILEs).pipe(gulp.dest(dest)).on("end", () => {
done && done();
});
}
/**
* generate `dependencies.ts`
*/
function createDependenciesTS() {
const dependencies = {...thisPackage.dependencies};
// for (const packageName of Object.keys(dependencies)) {
// if (packageName.includes("electron")) {
// dependencies[packageName] += " (at Electron browser)";
// }
// }
utils.writeTextUTF8(
`export const dependencies = ${JSON.stringify(dependencies, null, 1)};`,
"./src/dependencies.ts"
);
// - - - -
}
/**
* #### generate index.html from pug templete.
*
* gulp pug [--electron --pugOutDir ../out-repo]
*
* @param {string} renderTo output directory. if not specified then apply "./src"
* @param {() => void} [done] the callback (optional
*/
function doPug(renderTo, done) {
console.log(settings);
let cmpOption; {
let json = utils.removeJsonComments(
utils.readTextUTF8(TSConfigPath)
);
cmpOption = eval(`(${json})`).compilerOptions;
// DEVNOTE: error when ends with trailing comma
// cmpOption = JSON.parse(json).compilerOptions;
}
const appConfig = require(`${SRC_DIR}/page-config.json`);
const pug_file = `${SRC_DIR}/index.pug`;
const post_title = `(${thisPackage.version}, target: ${cmpOption.target}, ${cmpOption.module})`;
// @ts-ignore missing function parameter detection
let html = pug.renderFile(pug_file, /* locals: */ {
pretty: true,
compileDebug: false, // important?
vars: {
post_title,
webpack: settings.webpack,
publish: settings.publish,
electron: settings.electron,
firebase: settings.firebase,
}
});
html = html.replace(
/<!-- page-config-json -->/,
`<script id="page-config" type="application/json">
${JSON.stringify(appConfig, null, 2)}
</script>`);
//
!renderTo && (renderTo = SRC_DIR);
utils.writeTextUTF8(html, `${renderTo}/index.html`);
if (settings.electron) {
utils.fireReplace(
/(?<=version: )"\d+\.\d+\.\d+-dev"/, `"${thisPackage.version}"`, ["./src/index.ts"]
);
// DEVNOTE: basically, it is a process unrelated to electron build
// create DependenciesTS();
}
done && done();
// return gulp.src([pug_file]).pipe(pug({ pretty: true }))
}
/**
* #### compile sass file.
*
* node ./etc/build-tool -cmd sass [-destDir ../out-repo/css]
*
* @param {string} destDir result output directory
* @param {() => void} [done] callback when process done.
*/
function doSass(destDir, done) {
const sass = require("sass");
const name = "main";
!destDir && (destDir = SCSS_ROOT);
const resultFilePath = `${destDir}/${name}.css`;
sass.render({
file: `${SCSS_ROOT}/${name}.scss`,
outFile: resultFilePath,
outputStyle: "expanded",
// sourceMap: true, // default false
// sourceMapContents: false,
// // @ts-ignore https://sass-lang.com/documentation/js-api#sourcemapembed
// embedSourceMap: false
}, function (
/** @type {import("sass").SassException} */error,
/** @type {import("sass").Result} */result
) {
if (!error) {
const copyDest = `${JS_DEST_DIR}/css/`;
// No errors during the compilation, write this result on the disk
utils.writeTextUTF8(result.css, resultFilePath, () => {
gulp.src([resultFilePath]).pipe(gulp.dest(copyDest));
console.log(`${resultFilePath} -> ${copyDest}`);
});
if (result.map) {
const mapFile = `${resultFilePath}.map`;
utils.writeTextUTF8(result.map, mapFile, () => {
gulp.src([mapFile]).pipe(gulp.dest(copyDest));
console.log(`${mapFile} -> ${copyDest}`);
});
}
}
});
done && done();
}
/**
*
* @param {Error} err
* @param {import("webpack").Stats} stats
* @returns {boolean} when error occurs returns `true`
*/
const checkWebpackError = (err/* : Error */, stats/* : Stats */) => {
if (err) {
console.error(err.stack || err);
// @ts-ignore
if (err.details) {
// @ts-ignore
console.error(err.details);
}
return true;
}
const info = stats.toJson();
let fail = false;
let errorText = "";
if (stats.hasErrors()) {
fail = true;
errorText += "[[errors]]\n" + info.errors.join("\n\n");
}
if (stats.hasWarnings()) {
errorText += "\n\n[[warnings]]\n" + info.warnings.join("\n\n");
}
if (errorText) {
utils.writeTextUTF8(errorText, `./logs/${utils.dateStringForFile()}-webpack-errors.log`);
}
return fail;
};
/**
* @param {string} configFile
* @param {() => void} done
*/
// gulp webpack:build --mode production --mangle
const doWebpack = (configFile, done) => {
/** @typedef {import("webpack").Configuration} WebpackConfigration */
const webpack = require("webpack");
/** @type {WebpackConfigration} */
const webpackConfig = require(configFile)(
{ mangle: !!settings.mangle },
settings.mode
);
console.log(settings);
settings["noMinify"] && (webpackConfig.optimization.minimizer = [], webpackConfig.optimization.minimize = false);
console.time("doWebpack::webpack");
webpack(webpackConfig, (err/* : Error */, stats/* : Stats */) => {
console.log();
console.timeEnd("doWebpack::webpack");
if (checkWebpackError(err, stats)) {
done && done();
return;
}
console.log("- - - - > webpack process done <- - - -");
// console.log("- - - - > webpack process done, error=%s <- - - -", err);
if (settings.mode !== "production" && !settings.nostrip) {
console.log("doWebpack::rm-cstyle-cmts firing");
const rmc = require("rm-cstyle-cmts");
const originWebpackPath = `${JS_DEST_DIR}/webpack.js`;
const tmpWebpackPath = `${JS_DEST_DIR}/webpack-rmc.js`;
console.time("doWebpack::removeComments");
utils.readTextUTF8(originWebpackPath, (err, contents) => {
const commentsRemoved = rmc(contents);
utils.writeTextUTF8(commentsRemoved, tmpWebpackPath, () => {
fs.unlinkSync(originWebpackPath);
fs.renameSync(tmpWebpackPath, originWebpackPath);
console.timeEnd("doWebpack::removeComments");
postWebpacked(done);
});
});
} else {
postWebpacked(done); // <- this is a bit slow...
}
});
};
/**
*
* @param {() => void} done
* @param {string} configFile
*/
// production watch: npx gulp webpack:build --mangle --mode production
const doWebpackWatch = (done, configFile = "./webpack.config") => {
/** @typedef {import("webpack").Configuration} WebpackConfigration */
const webpack = require("webpack");
/** @type {WebpackConfigration} */
const webpackConfig = require(configFile)(
{ mangle: !!settings.mangle },
settings.mode
);
console.log(settings);
webpackConfig.watch = true;
webpackConfig.watchOptions = {
poll: 1000
};
webpack(webpackConfig, (err, stats) => {
if (checkWebpackError(err, stats)) {
done && done();
return;
}
postWebpacked(done);
});
};
// npx gulp webpack:watch
gulp.task("webpack:watch", done => {
doWebpackWatch(done);
});
// ---------------------------------- tasks ---------------------------------- //
/**
* task "clean"
* @param {() => void} done gulp callback function.
*/
gulp.task("clean", done => {
_del_hook([`${JS_DEST_DIR}/**/*`], done);
});
gulp.task("pug", done => {
doPug(settings.pugOutDir, done);
});
gulp.task("sass", done => {
doSass(SCSS_ROOT, done);
});
//
// webpack tasks.
//
gulp.task("webpack:build", done => {
settings.webpack = true;
doSass(SCSS_ROOT);
doPug(settings.pugOutDir);
copyResource("webpack.js", `${JS_DEST_DIR}`);
// DEVNOTE: need this task before `doWebpack` because bundle it.
createDependenciesTS();
doWebpack("./webpack.config", done);
});
// for local server
gulp.task("webpack", done => {
settings.webpack = true;
// force nostrip
settings.nostrip = true;
doWebpack("./webpack.config", done);
});
gulp.task("webpack:js", done => {
settings.webpack = true;
doSass(SCSS_ROOT);
doPug(settings.pugOutDir);
copyResource("webpack.js", `${JS_DEST_DIR}`);
//
createDependenciesTS();
doWebpack("./webpack.configjs", done);
});
gulp.task("sass:watch", function () {
gulp.watch("./src/css/**/*.scss", gulp.task("sass"));
});