-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
180 lines (159 loc) · 4.19 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
168
169
170
171
172
173
174
175
176
177
178
179
180
// generated on 2019-05-04 using generator-webapp 4.0.0-5
const { src, dest, watch, series, parallel, lastRun } = require("gulp");
const gulpLoadPlugins = require("gulp-load-plugins");
const browserSync = require("browser-sync");
const del = require("del");
const autoprefixer = require("autoprefixer");
const cssnano = require("cssnano");
const { argv } = require("yargs");
const $ = gulpLoadPlugins();
const server = browserSync.create();
const port = argv.port || 9000;
const isProd = process.env.NODE_ENV === "production";
const isTest = process.env.NODE_ENV === "test";
const isDev = !isProd && !isTest;
function styles() {
return src("app/styles/*.css")
.pipe($.if(!isProd, $.sourcemaps.init()))
.pipe($.postcss([autoprefixer()]))
.pipe($.if(!isProd, $.sourcemaps.write()))
.pipe(dest(".tmp/styles"))
.pipe(server.reload({ stream: true }));
}
function scripts() {
return src("app/scripts/**/*.js")
.pipe($.plumber())
.pipe($.if(!isProd, $.sourcemaps.init()))
.pipe($.babel())
.pipe($.if(!isProd, $.sourcemaps.write(".")))
.pipe(dest(".tmp/scripts"))
.pipe(server.reload({ stream: true }));
}
const lintBase = files => {
return src(files)
.pipe($.eslint({ fix: true }))
.pipe(server.reload({ stream: true, once: true }))
.pipe($.eslint.format())
.pipe($.if(!server.active, $.eslint.failAfterError()));
};
function lint() {
return lintBase("app/scripts/**/*.js").pipe(dest("app/scripts"));
}
function lintTest() {
return lintBase("test/spec/**/*.js").pipe(dest("test/spec"));
}
function html() {
return src("app/*.html")
.pipe($.useref({ searchPath: [".tmp", "app", "."] }))
.pipe($.if(/\.js$/, $.uglify({ compress: { drop_console: true } })))
.pipe(
$.if(/\.css$/, $.postcss([cssnano({ safe: true, autoprefixer: false })]))
)
.pipe(
$.if(
/\.html$/,
$.htmlmin({
collapseWhitespace: true,
minifyCSS: true,
minifyJS: { compress: { drop_console: true } },
processConditionalComments: true,
removeComments: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
})
)
)
.pipe(dest("dist"));
}
function images() {
return src("app/images/**/*", { since: lastRun(images) })
.pipe($.imagemin())
.pipe(dest("dist/images"));
}
function fonts() {
return src("app/fonts/**/*.{eot,svg,ttf,woff,woff2}").pipe(
$.if(!isProd, dest(".tmp/fonts"), dest("dist/fonts"))
);
}
function extras() {
return src(["app/*", "!app/*.html"], {
dot: true
}).pipe(dest("dist"));
}
function clean() {
return del([".tmp", "dist"]);
}
function measureSize() {
return src("dist/**/*").pipe($.size({ title: "build", gzip: true }));
}
const build = series(
clean,
parallel(
lint,
series(parallel(styles, scripts), html),
images,
fonts,
extras
),
measureSize
);
function startAppServer() {
server.init({
notify: false,
port,
server: {
baseDir: [".tmp", "app"],
routes: {
"/node_modules": "node_modules"
}
}
});
watch(["app/*.html", "app/images/**/*", ".tmp/fonts/**/*"]).on(
"change",
server.reload
);
watch("app/styles/**/*.css", styles);
watch("app/scripts/**/*.js", scripts);
watch("app/fonts/**/*", fonts);
}
function startTestServer() {
server.init({
notify: false,
port,
ui: false,
server: {
baseDir: "test",
routes: {
"/scripts": ".tmp/scripts",
"/node_modules": "node_modules"
}
}
});
watch("app/scripts/**/*.js", scripts);
watch(["test/spec/**/*.js", "test/index.html"]).on("change", server.reload);
watch("test/spec/**/*.js", lintTest);
}
function startDistServer() {
server.init({
notify: false,
port,
server: {
baseDir: "dist",
routes: {
"/node_modules": "node_modules"
}
}
});
}
let serve;
if (isDev) {
serve = series(clean, parallel(styles, scripts, fonts), startAppServer);
} else if (isTest) {
serve = series(clean, scripts, startTestServer);
} else if (isProd) {
serve = series(build, startDistServer);
}
exports.serve = serve;
exports.build = build;
exports.default = build;