-
Notifications
You must be signed in to change notification settings - Fork 708
/
gulpfile.js
250 lines (227 loc) · 5.83 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
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
// generated on <%= date %> using <%= name %> <%= version %>
const { src, dest, watch, series, parallel, lastRun } = require('gulp');
const gulpLoadPlugins = require('gulp-load-plugins');
<%_ if (includeModernizr) { -%>
const fs = require('fs');
const mkdirp = require('mkdirp');
const Modernizr = require('modernizr');
<%_ } -%>
const browserSync = require('browser-sync');
const del = require('del');
const autoprefixer = require('autoprefixer');
<%_ if (includeSass) { -%>
const sass = require('gulp-sass')(require('sass'));
<%_ } -%>
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() {
<%_ if (includeSass) { -%>
return src('app/styles/*.scss', {
sourcemaps: !isProd,
})
.pipe($.plumber())
.pipe(sass.sync({
outputStyle: 'expanded',
precision: 10,
includePaths: ['.']
}).on('error', sass.logError))
<%_ } else { -%>
return src('app/styles/*.css', {
sourcemaps: !isProd,
})
<%_ } -%>
.pipe($.postcss([
autoprefixer()
]))
.pipe(dest('.tmp/styles', {
sourcemaps: !isProd,
}))
.pipe(server.reload({stream: true}));
};
function scripts() {
return src('app/scripts/**/*.js', {
sourcemaps: !isProd,
})
.pipe($.plumber())
.pipe($.babel())
.pipe(dest('.tmp/scripts', {
sourcemaps: !isProd ? '.' : false,
}))
.pipe(server.reload({stream: true}));
};
<%_ if (includeModernizr) { -%>
async function modernizr() {
const readConfig = () => new Promise((resolve, reject) => {
fs.readFile(`${__dirname}/modernizr.json`, 'utf8', (err, data) => {
if (err) reject(err);
resolve(JSON.parse(data));
})
})
const createDir = () => new Promise((resolve, reject) => {
mkdirp(`${__dirname}/.tmp/scripts`, err => {
if (err) reject(err);
resolve();
})
});
const generateScript = config => new Promise((resolve, reject) => {
Modernizr.build(config, content => {
fs.writeFile(`${__dirname}/.tmp/scripts/modernizr.js`, content, err => {
if (err) reject(err);
resolve(content);
});
})
});
const [config] = await Promise.all([
readConfig(),
createDir()
]);
await generateScript(config);
}
<%_ } -%>
const lintBase = (files, options) => {
return src(files)
.pipe($.eslint(options))
.pipe(server.reload({stream: true, once: true}))
.pipe($.eslint.format())
.pipe($.if(!server.active, $.eslint.failAfterError()));
}
function lint() {
return lintBase('app/scripts/**/*.js', { fix: true })
.pipe(dest('app/scripts'));
};
function lintTest() {
return lintBase('test/spec/**/*.js');
};
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,
<%_ if (includeModernizr) { -%>
series(parallel(styles, scripts, modernizr), html),
<%_ } else { -%>
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);
<%_ if (includeSass) { -%>
watch('app/styles/**/*.scss', styles);
<%_ } else { -%>
watch('app/styles/**/*.css', styles);
<%_ } -%>
watch('app/scripts/**/*.js', scripts);
<%_ if (includeModernizr) { -%>
watch('modernizr.json', modernizr);
<%_ } -%>
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('test/index.html').on('change', server.reload);
watch('app/scripts/**/*.js', scripts);
watch('test/spec/**/*.js', lintTest);
}
function startDistServer() {
server.init({
notify: false,
port,
server: {
baseDir: 'dist',
routes: {
'/node_modules': 'node_modules'
}
}
});
}
let serve;
if (isDev) {
<%_ if (includeModernizr) { -%>
serve = series(clean, parallel(styles, scripts, modernizr, fonts), startAppServer);
<%_ } else { -%>
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;