-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
167 lines (149 loc) · 4.64 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
const gulp = require('gulp')
const {
src,
series,
parallel,
watch,
dest
} = require('gulp');
const preprocess = require("gulp-preprocess");
const $ = require('gulp-load-plugins')()
/**
* The target browser. Used to customize the manifest and choose the destination
* folder for builds and distribution packaging
* @type {('chrome' | 'firefox' | 'opera')}
*/
const target = process.env.TARGET || "chrome";
/**
* The environment. Used to disable the background script on development
* @type {('production' | 'development' )}
*/
const environment = process.env.NODE_ENV || "development";
/**
*
*/
const manifestInfo = {
dev: {
"background": {
"scripts": [
// "js/livereload.js",
]
}
},
firefox: {
"applications": {
"gecko": {
"id": "{901c6ba2-023e-4cab-9842-2a8c9d051a15}"
}
}
}
}
// Should there be a need for scss
// function styles() {
// // convert scss to css in build folder
// // return src('src/styles/**/*.scss')
// .pipe(plumber())
// .pipe($.sass.sync({
// outputStyle: 'expanded',
// precision: 10,
// includePaths: ['.']
// }).on('error', $.sass.logError))
// .pipe(dest(`build/${target}/styles`))
// }
/** Task: Delete 'build' folder for current target browser */
function cleanBuild() {
// remove the build contents
return src(`./build/${target}`, {
read: false,
allowEmpty: true
}).pipe($.clean())
}
/** Task: Delete 'dist' folder for current target browser */
function cleanDist() {
// remove the dist contents
return src(`./dist/${target}`, {
read: false,
allowEmpty: true
}).pipe($.clean())
}
/** Task: Delete parent 'dist' and 'build' folders */
function cleanAll() {
// remove the dist contents
return src([`./dist/`, `./build/`], {
read: false,
allowEmpty: true
}).pipe($.clean())
}
/** Task: Generates the manifest to meet different browser requirements */
function manifest() {
// append specific info to manifest and place it in the target folder
return src('./manifest.json')
.pipe($.mergeJson({
fileName: "manifest.json",
jsonSpace: " ".repeat(4),
endObj: target === "firefox" ? manifestInfo.firefox : manifestInfo.dev,
// if development environment, disable the background script
edit: (parsedJson, file) => {
if (environment === 'development') {
delete parsedJson.background;
}
return parsedJson;
}
}))
.pipe(gulp.dest(`./build/${target}`))
}
/** Task: Copy CSS folder from `src/` to `build/` */
function moveCss() {
return pipe('./src/css/**/*', `./build/${target}/css`);
}
/** Task: Copy extra folder from `src/` to `build/` */
function moveExtra() {
return pipe('./src/extra/**/*', `./build/${target}/extra`);
}
/** Task: Copy icons folder from `src/` to `build/` */
function moveIcons() {
return pipe('./src/icons/**/*', `./build/${target}/icons`);
}
/** Task: Copy javascript folder from `src/` to `build/` */
function moveJs() {
return src('./src/js/**/*')
.pipe(preprocess({ context: { DEBUG: environment === 'development' }}))
.pipe(gulp.dest(`./build/${target}/js`));
}
/** Task: Copy html files from `src/` to `build/` */
function moveHtml() {
return pipe(['./src/changelog.html', './src/options.html'], `./build/${target}`);
}
/** Task: Zips each browser build inside the `dist/` folder. Assumes build completed */
function zip() {
// zip all files in the target folder for dist
return src([`./build/${target}/**/*`, `./build/${target}/**/**/*`])
.pipe($.zip(`${target}.zip`))
.pipe(dest('./dist'));
}
/** Task: Watch for any changes in the source folder */
function startWatching() {
$.livereload.listen()
watch(['./src/*', './src/**/*', './src/**/**/*']).on("change", () => {
exports.build()
$.livereload.reload()
});
}
// Helpers
function pipe(src, ...transforms) {
return transforms.reduce((stream, transform) => {
const isDest = typeof transform === 'string'
return stream.pipe(isDest ? gulp.dest(transform) : transform)
}, gulp.src(src))
}
// Export tasks
exports.clean = series(cleanBuild, cleanDist);
exports.build = series(
exports.clean,
manifest,
parallel(moveHtml, moveCss, moveJs, moveExtra, moveIcons)
);
exports.dist = series(exports.build, zip);
exports.watch = series(exports.build, startWatching);
exports.cleanAll = cleanAll;
exports.default = exports.build;