-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
129 lines (119 loc) · 3.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
var webpack = require('webpack');
var webpackConfigFunc = require('./webpack.config');
var gulp = require('gulp');
var zip = require('gulp-zip');
var clean = require('gulp-clean');
var jsoncombine = require('gulp-jsoncombine');
var minimist = require('minimist');
var packageConfig = require('./package.json');
const { exit } = require('process');
const uglify = require('gulp-uglify');
//parse arguments
var knownOptions = {
string: ['env', 'browser', 'manifest', 'channel'],
default: {
env: 'dev',
browser: 'chrome',
manifest: 'mv3',
channel: 'store'
}
};
var supported_envs = ['dev', 'pro'];
var supported_browsers = ['chrome', 'firefox', 'edge', 'brave'];
var supported_mvs = ['mv2', 'mv3'];
var brandName = 'side';
var version = packageConfig.version;
var validVersion = version.split('-beta')[0];
var options = {
env: knownOptions.default.env,
browser: knownOptions.default.browser,
manifest: knownOptions.default.manifest
};
options = minimist(process.argv.slice(2), knownOptions);
if (!supported_envs.includes(options.env)) {
console.error(`not supported env: [${options.env}]. It should be one of ${supported_envs.join(', ')}.`);
exit(0);
}
if (!supported_browsers.includes(options.browser)) {
console.error(`not supported browser: [${options.browser}]. It should be one of ${supported_browsers.join(', ')}.`);
exit(0);
}
if (!supported_mvs.includes(options.manifest)) {
console.error(`not supported browser: [${options.manifest}]. It should be one of ${supported_mvs.join(', ')}.`);
exit(0);
}
//tasks...
function task_clean() {
return gulp.src(`dist/${options.browser}/*`, { read: false }).pipe(clean());
}
function task_prepare() {
return gulp.src('build/_raw/**/*').pipe(gulp.dest(`dist/${options.browser}`));
}
function task_merge_manifest() {
let baseFile = '_base_v3';
if (options.manifest == 'mv2') {
baseFile = '_base_v2';
}
return gulp
.src([
`dist/${options.browser}/manifest/${baseFile}.json`,
`dist/${options.browser}/manifest/${options.browser}.json`
])
.pipe(
jsoncombine('manifest.json', (data, meta) => {
const result = Object.assign({}, data[baseFile], data[options.browser]);
result.version = validVersion;
return Buffer.from(JSON.stringify(result));
})
)
.pipe(gulp.dest(`dist/${options.browser}`));
}
function task_clean_tmps() {
return gulp.src(`dist/${options.browser}/manifest`, { read: false }).pipe(clean());
}
function task_webpack(cb) {
webpack(
webpackConfigFunc({
version: validVersion,
config: options.env,
browser: options.browser,
manifest: options.manifest,
channel: options.channel
}),
cb
);
}
function task_uglify(cb) {
if (options.env == 'pro') {
return gulp
.src(`dist/${options.browser}/**/*.js`)
.pipe(uglify())
.pipe(gulp.dest(`dist/${options.browser}`));
}
cb();
}
function task_package(cb) {
if (options.env == 'pro') {
if (options.browser == 'firefox') {
return gulp
.src(`dist/${options.browser}/**/*`)
.pipe(zip(`${brandName}-${options.browser}-${options.manifest}-v${version}.xpi`))
.pipe(gulp.dest('./dist'));
} else {
return gulp
.src(`dist/${options.browser}/**/*`)
.pipe(zip(`${brandName}-${options.browser}-${options.manifest}-v${version}.zip`))
.pipe(gulp.dest('./dist'));
}
}
cb();
}
exports.build = gulp.series(
task_clean,
task_prepare,
task_merge_manifest,
task_clean_tmps,
task_webpack,
task_uglify,
task_package
);