-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.babel.js
159 lines (134 loc) · 3.66 KB
/
gulpfile.babel.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
import fs from "fs";
import gulp from 'gulp';
import {merge} from 'event-stream'
import browserify from 'browserify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import preprocessify from 'preprocessify';
import gulpif from "gulp-if";
const $ = require('gulp-load-plugins')();
var production = process.env.NODE_ENV === "production";
var target = process.env.TARGET || "chrome";
var environment = process.env.NODE_ENV || "development";
var generic = JSON.parse(fs.readFileSync(`./config/${environment}.json`));
var specific = JSON.parse(fs.readFileSync(`./config/${target}.json`));
var context = Object.assign({}, generic, specific);
var manifest = {
dev: {
"background": {
"scripts": [
"scripts/livereload.js"
]
}
},
firefox: {
"applications": {
"gecko": {
"id": "yanzhen@xsky.com"
}
}
}
}
// Tasks
gulp.task('clean', () => {
return pipe(`./build/${target}`, $.clean())
})
gulp.task('build', (cb) => {
$.runSequence('clean', 'styles', 'ext', cb)
});
gulp.task('watch', ['build'], () => {
$.livereload.listen();
gulp.watch(['./src/**/*']).on("change", () => {
$.runSequence('build', $.livereload.reload);
});
});
gulp.task('default', ['build']);
gulp.task('ext', ['manifest', 'js'], () => {
return mergeAll(target)
});
// -----------------
// COMMON
// -----------------
gulp.task('js', () => {
return buildJS(target)
})
gulp.task('styles', () => {
return gulp.src('src/styles/**/*.scss')
.pipe($.plumber())
.pipe($.sass.sync({
outputStyle: 'expanded',
precision: 10,
includePaths: ['.']
}).on('error', $.sass.logError))
.pipe(gulp.dest(`build/${target}/styles`));
});
gulp.task("manifest", () => {
return gulp.src('./manifest.json')
.pipe(gulpif(!production, $.mergeJson({
fileName: "manifest.json",
jsonSpace: " ".repeat(4),
endObj: manifest.dev
})))
.pipe(gulpif(target === "firefox", $.mergeJson({
fileName: "manifest.json",
jsonSpace: " ".repeat(4),
endObj: manifest.firefox
})))
.pipe(gulp.dest(`./build/${target}`))
});
// -----------------
// DIST
// -----------------
gulp.task('dist', (cb) => {
$.runSequence('build', 'zip', cb)
});
gulp.task('zip', () => {
return pipe(`./build/${target}/**/*`, $.zip(`${target}.zip`), './dist')
})
// 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))
}
function mergeAll(dest) {
return merge(
pipe('./src/icons/**/*', `./build/${dest}/icons`),
pipe(['./src/_locales/**/*'], `./build/${dest}/_locales`),
pipe([`./src/images/${target}/**/*`], `./build/${dest}/images`),
pipe(['./src/images/shared/**/*'], `./build/${dest}/images`),
pipe(['./src/**/*.html'], `./build/${dest}`)
)
}
function buildJS(target) {
const files = [
'contentscript.js',
'popup.js',
'livereload.js'
]
let tasks = files.map( file => {
return browserify({
entries: 'src/scripts/' + file,
debug: true
})
.transform('babelify', { presets: ['es2015'] })
.transform(preprocessify, {
includeExtensions: ['.js'],
context: context
})
.bundle()
.pipe(source(file))
.pipe(buffer())
.pipe(gulpif(!production, $.sourcemaps.init({ loadMaps: true }) ))
.pipe(gulpif(!production, $.sourcemaps.write('./') ))
.pipe(gulpif(production, $.uglify({
"mangle": false,
"output": {
"ascii_only": true
}
})))
.pipe(gulp.dest(`build/${target}/scripts`));
});
return merge.apply(null, tasks);
}