-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathgulpfile.js
More file actions
253 lines (217 loc) · 7.1 KB
/
gulpfile.js
File metadata and controls
253 lines (217 loc) · 7.1 KB
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
const gulp = require('gulp');
const del = require('del');
const chalk = require('chalk');
const gulpChange = require('gulp-change');
const paths = require('../config/build.json');
const plugins = require('gulp-load-plugins')();
const webpack = require('webpack-stream');
const sass = require('gulp-sass')(require('sass'));
const Utilities = require('./utilities').Utilities;
const argv = require('yargs').argv;
const {prod: isProd, firefox: isFirefox, pkg: pkgPath, config} = argv;
/** helper method to ensure array type */
const ensureArray = path => Array.isArray(path) ? path : [path];
/** read project package.json **/
const pkg = Utilities.readJSON(pkgPath);
/** read project's config file, if specified **/
let customPaths = null;
if (Utilities.fileExists(config)) {
// if config is a file
customPaths = Utilities.readJSON(config);
} else if (pkg.xtbuild !== undefined) {
// otherwise config should be specified in package.json
customPaths = pkg.xtbuild;
}
/** replace default configs with project-level configs **/
if (customPaths) {
for (let key in customPaths) {
if (customPaths.hasOwnProperty(key)) {
paths[key] = customPaths[key];
}
}
}
const clean = () => del([paths.dist + '/*']);
const script = ({src, name, mode}, done = _ => true) => {
const webpackOptions = {
// use mode if specified explicitly; otherwise choose by --env
mode: mode || (isProd ? 'production' : 'development'),
// match sourcemap name with configured js file name
output: {filename: `${name}.js`},
// use source map with dev builds only
devtool: isProd ? undefined : 'cheap-source-map',
// allow TS importing
resolve: {
extensions: ['.ts', '.tsx', '...']
},
// use ts-loader to compile TS files
module: {
rules: [
// all files with a `.ts`, `.cts`, `.mts` or `.tsx` extension will be handled by `ts-loader`
{
test: /\.([cm]?ts|tsx)$/,
loader: 'ts-loader',
options: {
configFile: 'src/tsconfig.json'
}
}
]
}
};
return gulp.src(src)
.pipe(webpack(webpackOptions))
.on('error', (err) => {
console.log(err.toString());
this.emit('end');
})
.pipe(plugins.rename(path => {
path.dirname = '';
path.basename = name;
}))
.pipe(gulp.dest(paths.dist))
.on('end', done);
};
const style = ({src, name}, done = _ => true) => {
return gulp.src(src)
// convert to css
.pipe(sass().on('error', sass.logError))
// concatenate multiple src files
.pipe(plugins.concat(`${name}.css`))
// minify
.pipe(plugins.cleanCss())
// rename to user-specified name
.pipe(plugins.rename((path) => {
path.dirname = '';
path.basename = name;
}))
.pipe(gulp.dest(paths.dist))
.on('end', done);
};
const copy = (src, done = _ => true) => {
// nested copy specified using glob pattern
if (src.endsWith('*')) {
return gulp.src(src, {base: 'src'})
.pipe(gulp.dest(paths.dist))
.on('end', done);
}
// copy single file or directory
return gulp.src(src)
.pipe(plugins.rename(path => {
path.dirname = '';
}))
.pipe(gulp.dest(paths.dist))
.on('end', done);
};
const locale = (language, done = _ => true) => {
return gulp.src(paths.locales_dir + language + '/**/*.json')
.pipe(plugins.mergeJson({fileName: 'messages.json'}))
.pipe(plugins.jsonminify())
.pipe(gulp.dest(paths.dist + '/_locales/' + language))
.on('end', done);
};
const copyManifest = done => {
const {version} = pkg;
const performChange = (content) => {
let mft = JSON.parse(content);
mft.version = version; // use version from package
if (isFirefox && mft.firefox) mft = {...mft, ...mft.firefox};
else if (!isFirefox && mft.chrome) mft = {...mft, ...mft.chrome};
delete mft.chrome;
delete mft.firefox;
return JSON.stringify(mft);
};
return gulp.src(paths.manifest)
.pipe(gulpChange(performChange))
.pipe(plugins.jsonminify())
.pipe(plugins.rename(path => {
path.dirname = '';
path.basename = 'manifest';
path.extname = '.json';
}))
.pipe(gulp.dest(paths.dist))
.on('end', done);
};
const copyAssets = done => {
return gulp.src(paths.assets)
.pipe(gulp.dest(paths.dist + '/assets'))
.on('end', done);
};
const buildHtml = done => {
return gulp.src(paths.html)
.pipe(plugins.htmlmin({collapseWhitespace: true}))
.pipe(plugins.rename(path => {
path.dirname = '';
}))
.pipe(gulp.dest(paths.dist))
.on('end', done);
};
const customCommands = done => {
if (!paths.commands || !paths.commands.length) {
return done();
}
return require('child_process')
.exec(paths.commands, done);
};
const release = done => {
if (!isProd) return done();
return gulp.src(paths.dist + '/**/*')
.pipe(plugins.zip(`${paths.release_name || 'release'}.zip`))
.pipe(gulp.dest(paths.releases))
.on('end', done);
};
const dynamicFunc = (action, name) => {
const f = action;
Object.defineProperty(f, 'name', {
value: name,
writable: false
});
return f;
};
const scripts = paths.js_bundles.map(obj =>
dynamicFunc(_ => script(obj), `${obj.name}.js`));
const styles = paths.scss_bundles.map(obj =>
dynamicFunc(_ => style(obj), `${obj.name}.css`));
const locales = paths.locales_list.map(lang =>
dynamicFunc(_ => locale(lang), `locale ${lang}`));
const copies = ensureArray(paths.copyAsIs).map(obj =>
dynamicFunc(_ => copy(obj), `copy ${obj}`));
const watch = () => {
console.log(chalk.bold.yellow('watching...'));
if (scripts.length) {
gulp.watch(ensureArray(paths.js), gulp.parallel(...scripts));
}
if (styles.length) {
gulp.watch(ensureArray(paths.scss), gulp.parallel(...styles));
}
if (copies.length) {
gulp.watch(ensureArray(paths.copyAsIs), gulp.parallel(...copies));
}
if (paths.locales_list.length) {
gulp.watch(paths.locales_dir + '**/*.json', gulp.parallel(...locales));
}
gulp.watch(paths.manifest, copyManifest);
gulp.watch(ensureArray(paths.html), buildHtml);
gulp.watch(ensureArray(paths.assets), copyAssets);
// gulp.watch(paths.commands_watch_path || '', customCommands);
};
const build = gulp.series(
clean,
gulp.parallel(
...scripts,
...styles,
...copies,
...locales,
copyManifest,
copyAssets,
buildHtml
),
customCommands,
release
);
/*
* Define default task that can be called by just running `gulp` from cli
*/
exports.default = build;
/*
* If watch flag is defined, run build and keep watching
*/
exports.watch = gulp.series(build, watch);