-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
144 lines (124 loc) · 3.65 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
"use strict";
const gulp = require("gulp")
, sourcemaps = require("gulp-sourcemaps")
, minifyCSS = require("gulp-csso")
, sass = require("gulp-sass")
, merge = require("merge2")
, del = require("del")
, runSequence = require("run-sequence")
, htmlmin = require("gulp-htmlmin")
, tsify = require("tsify")
, babelify = require("babelify");
const bundler = require("./tools/bundler");
const paths = {
sass: {
src: ["src/sass/**/*.scss", "src/sass/**/*.sass"],
dest: "dist/css"
},
html: {
src: ["src/*.html"],
dest: "dist/"
},
assets: {
src: "assets/**/*",
dest: "dist/"
}
};
const bundles = [
{
entries: "src/main.ts",
name: "app.bundle.js",
dest: "dist/js",
plugins: [ [tsify, "tsconfig.json"] ],
transforms: [ ["brfs"] ],
debug: true,
watch: false,
},
{
entries: "src/background.ts",
name: "background.bundle.js",
dest: "dist/js",
plugins: [ [tsify, "tsconfig.json"] ],
transforms: [ ["brfs"] ],
debug: true,
watch: false,
},
];
const browserEnvOptions = {
"targets": {
"chrome": 52
}
};
const state = {
/** true if scripts should be run in production mode. */
production: false,
};
gulp.task("scripts", function() {
return bundler(bundles.map(b => {
let transforms = b.transforms;
if (!transforms) transforms = [];
transforms.push([babelify, {
global: true,
presets: [
["env", browserEnvOptions]
],
extensions: [ ".tsx", ".ts", ".js", ".jsx",".json" ]
}]);
b.transforms = transforms;
return b;
}));
});
gulp.task("scripts:prod", function() {
return bundler(bundles.map(b => {
let transforms = b.transforms;
if (!transforms) transforms = [];
transforms.push([babelify, {
global: true,
presets: [
"minify", ["env", browserEnvOptions]
],
extensions: [ ".tsx", ".ts", ".js", ".jsx", ".json" ]}]);
b.transforms = transforms;
return b;
}), {debug: false});
});
gulp.task("scripts:watch", function() {
return bundler(bundles, { watch: true });
});
gulp.task("sass", function() {
let s = gulp.src(paths.sass.src)
if (!state.production) s = s.pipe(sourcemaps.init());
s = s.pipe(sass().on("error", sass.logError));
if (state.production) s = s.pipe(minifyCSS({restructure: true, debug: true}));
if (!state.production) s = s.pipe(sourcemaps.write());
s = s.pipe(gulp.dest(paths.sass.dest));
});
gulp.task("html", function() {
let s = gulp.src(paths.html.src);
if (state.production) s = s.pipe(htmlmin({collapseWhitespace: true}));
s = s.pipe(gulp.dest(paths.html.dest));
return s;
});
gulp.task("assets", function() {
return gulp.src(paths.assets.src)
.pipe(gulp.dest(paths.assets.dest));
});
gulp.task("build", function(callback) {
runSequence("clean", ["scripts", "sass", "html", "assets"], callback);
});
gulp.task("build:prod", function(callback) {
console.log("--PRODUCTION MODE--");
state.production = true;
runSequence("clean", ["scripts:prod", "sass", "html", "assets"], callback);
});
gulp.task("watch", ["clean"], function(callback) {
runSequence("clean", ["scripts:watch", "sass", "html", "assets"], () => {
gulp.watch(paths.sass.src, ["sass"]);
gulp.watch(paths.html.src, ["html"]);
gulp.watch(paths.assets.src, ["assets"]);
callback();
})
});
gulp.task("clean", function() {
return del(["dist/**/*"]);
});