-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
159 lines (138 loc) · 4 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
const gulp = require('gulp');
const checkFileSize = require('gulp-check-filesize');
const concat = require('gulp-concat');
const deleteFiles = require('gulp-rimraf');
const eslint = require('gulp-eslint');
const imagemin = require('gulp-imagemin');
const lintHTML = require('gulp-htmllint');
const minifyHTML = require('gulp-minify-html');
const minifyCSS = require('gulp-clean-css');
const rename = require('gulp-rename');
const replaceHTML = require('gulp-html-replace');
const rollup = require('rollup');
const stylelint = require('gulp-stylelint');
const { terser } = require('rollup-plugin-terser');
const zip = require('gulp-zip');
const paths = {
src: {
dir: './src',
css: 'css/**.css',
html: '**.html',
js: {
input: 'js/main.js',
all: 'js/**'
},
images: 'assets/images/**'
},
dist: {
dir: './dist',
css: 'style.min.css',
js: 'script.min.js',
images: 'dist/images'
},
zip: {
dir: './zip'
}
};
let rollupCache;
gulp.task('lintHTML', () => {
return gulp.src(paths.src.html)
.pipe(lintHTML());
});
gulp.task('lintCSS', () => {
return gulp.src(paths.src.css)
.pipe(stylelint({
reporters: [{ formatter: 'string', console: true }]
}));
});
gulp.task('lintJS', () => {
return gulp.src(`${paths.src.dir}/${paths.src.js.all}`)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('cleanDist', () => {
return gulp.src(`${paths.dist.dir}/*`, { read: false })
.pipe(deleteFiles());
});
gulp.task('buildHTML', () => {
return gulp.src(`${paths.src.dir}/${paths.src.html}`)
.pipe(replaceHTML({
css: paths.dist.css,
js: paths.dist.js
}))
.pipe(minifyHTML())
.pipe(gulp.dest(paths.dist.dir));
});
gulp.task('buildCSS', () => {
return gulp.src(`${paths.src.dir}/${paths.src.css}`)
.pipe(concat(paths.dist.css))
.pipe(minifyCSS())
.pipe(gulp.dest(paths.dist.dir));
});
gulp.task('buildJS', async function () {
const input = `${paths.src.dir}/${paths.src.js.input}`;
const output = `${paths.dist.dir}/${paths.dist.js}`;
let bundle;
try {
bundle = await rollup.rollup({
cache: rollupCache,
input,
plugins: [
terser()
]
});
} catch (error) {
// Fix the error so that when Gulp gets it, it can report the details of where
// the error was discovered instead of the rollup stack that discovered it.
// error.loc for the file and position (or error.pos and error.id)
if (error.loc) {
const { loc } = error;
error.stack = `Error: ${error.message}\n at (${loc.file}:${loc.line}:${loc.column}\n${error.frame}`;
} else if (error.pos) {
error.stack = `Error: ${error.message}\n at (${error.pos}\n${error.id}`;
}
throw error;
}
rollupCache = bundle.cache;
await bundle.write({
compact: true,
file: output,
format: 'iife',
sourcemap: true
});
});
gulp.task('optimizeImages', () => {
return gulp.src(`${paths.src.dir}/${paths.src.images}`, {since: gulp.lastRun('optimizeImages')})
.pipe(imagemin())
.pipe(gulp.dest(paths.dist.images));
});
gulp.task('zip', () => {
const thirteenKb = 13 * 1024;
gulp.src('zip/*')
.pipe(deleteFiles());
return gulp.src(`${paths.dist.dir}/**`, { ignore: `${paths.dist.dir}/**.map` })
.pipe(zip('game.zip'))
.pipe(gulp.dest('zip'))
.pipe(checkFileSize({ fileSizeLimit: thirteenKb }));
});
gulp.task('test', gulp.parallel(
'lintHTML',
'lintCSS',
'lintJS'
));
gulp.task('build', gulp.series(
'cleanDist',
gulp.parallel('buildHTML', 'buildCSS', 'buildJS', 'optimizeImages'),
'zip'
));
gulp.task('watch', () => {
gulp.watch(`${paths.src.dir}/${paths.src.html}`, gulp.series('buildHTML', 'zip'));
gulp.watch(`${paths.src.dir}/${paths.src.css}`, gulp.series('buildCSS', 'zip'));
gulp.watch(`${paths.src.dir}/${paths.src.js.all}`, gulp.series('buildJS', 'zip'));
gulp.watch(`${paths.src.dir}/${paths.src.images}`, gulp.series('optimizeImages', 'zip'));
});
gulp.task('default', gulp.series(
'build',
'watch'
));