generated from staticwebdev/vanilla-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
204 lines (171 loc) · 6.39 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
const gulp = require('gulp');
const dumber = require('gulp-dumber');
const au2 = require('@aurelia/plugin-gulp').default;
const fs = require('fs');
const typescript = require('gulp-typescript');
const plumber = require('gulp-plumber');
const merge2 = require('merge2');
const terser = require('gulp-terser');
const gulpif = require('gulp-if');
const del = require('del');
const devServer = require('./dev-server');
const cssModule = require('gulp-dumber-css-module');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const postcssUrl = require('postcss-url');
const isProduction = process.env.NODE_ENV === 'production';
const isTest = process.env.NODE_ENV === 'test';
const dist = 'dist';
// Read more in https://dumber.js.org
const dr = dumber({
// src folder is by default "src".
// src: 'src',
// requirejs baseUrl, dumber default is "/dist"
baseUrl: isProduction ? '/' : '/' + dist,
// can turn off cache for production build
// cache: !isProduction,
// entry bundle name, dumber default is "entry-bundle"
// entryBundle: 'entry-bundle',
// Turn on hash for production build
hash: false,
// Note prepend/append only affects entry bundle.
// prepend before amd loader.
// dumber-module-loader is injected automatically by dumber bundler after prepends.
// prepend: [],
// Explicit dependencies, can use either "deps" (short name) or "dependencies" (full name).
// deps: [],
// Code split is intuitive and flexible.
// https://dumber.js.org/options/code-split
//
// You provide a function to return a bundle name for every single module.
// The function takes two parameters:
//
// moduleId:
// for local src file "src/foo/bar.js", the module id is "foo/bar.js"
// for local src file "src/foo/bar.css" (or any other non-js file), the module id is "foo/bar.css"
// for npm package file "node_modules/foo/bar.js", the module id is "foo/bar.js"
// for npm package file "node_modules/@scoped/foo/bar.js", the module id is "@scoped/foo/bar.js"
//
// packageName:
// for any local src file, the package name is undefined
// for npm package file "node_modules/foo/bar.js", the package name is "foo"
// for npm package file "node_modules/@scoped/foo/bar.js", the package name is "@scoped/foo"
// Here we skip code splitting in test mode.
codeSplit: isTest ? undefined : function (moduleId, packageName) {
// Here for any local src, put into app-bundle
if (!packageName) return 'app-bundle';
// The codeSplit func does not need to return a valid bundle name.
// For any undefined return, dumber put the module into entry bundle,
// this means no module can skip bundling.
},
// onManifest is an optional callback, it provides a file name map like:
// {
// "some-bundle.js": "some-bundle.1234.js",
// "other-bundle.js": "other-bundle.3455.js"
// }
// Or when hash is off
// {
// "some-bundle.js": "some-bundle.js",
// "other-bundle.js": "other-bundle.js"
// }
// If you turned on hash, you need this callback to update index.html
onManifest: isTest ? undefined : function (filenameMap) {
// Update index.html entry-bundle.js with entry-bundle.hash...js
console.log('Update index.html with ' + filenameMap['entry-bundle.js']);
const indexHtml = fs.readFileSync('_index_dev.html').toString()
.replace('entry-bundle.js', filenameMap['entry-bundle.js']);
fs.writeFileSync('index.html', indexHtml);
const indexHtmlProd = fs.readFileSync('_index.html').toString()
.replace('entry-bundle.js', filenameMap['entry-bundle.js']);
fs.writeFileSync('dist/index.html', indexHtmlProd);
}
});
function buildJs(src) {
const ts = typescript.createProject('tsconfig.json', { noEmitOnError: true });
return gulp.src(src, { sourcemaps: !isProduction })
.pipe(gulpif(!isProduction && !isTest, plumber()))
.pipe(au2())
.pipe(ts());
}
function buildHtml(src) {
return gulp.src(src)
.pipe(gulpif(!isProduction && !isTest, plumber()))
.pipe(au2({ useCSSModule: true }));
}
function copyResources() {
console.log('Copying Content');
return gulp
.src(["content/**/*.{gif,jpg,png,svg}"])
.pipe(gulp.dest("dist/content"));
}
function buildCss(src) {
return gulp.src(src, { sourcemaps: !isProduction })
.pipe(postcss([
autoprefixer(),
// use postcss-url to inline any image/font/svg.
// postcss-url by default use base64 for images, but
// encodeURIComponent for svg which does NOT work on
// some browsers.
// Here we enforce base64 encoding for all assets to
// improve compatibility on svg.
postcssUrl({ url: 'inline', encodeType: 'base64' })
]))
.pipe(cssModule());
}
function buildSource() {
copyResources();
// Merge all js/css/html file streams to feed dumber.
// dumber knows nothing about .ts/.less/.scss/.md files,
// gulp-* plugins transpiled them into js/css/html before
// sending to dumber.
return merge2(
gulp.src('src/**/*.json'),
buildJs('src/**/*.ts'),
buildHtml('src/**/*.html'),
buildCss('src/**/*.css')
)
// Note we did extra call `dr()` here, this is designed to cater watch mode.
// dumber here consumes (swallows) all incoming Vinyl files,
// then generates new Vinyl files for all output bundle files.
.pipe(dr())
// Terser fast minify mode
// https://github.com/terser-js/terser#terser-fast-minify-mode
// It's a good balance on size and speed to turn off compress.
.pipe(gulpif(isProduction, terser({ compress: false })))
.pipe(gulp.dest(dist, { sourcemaps: isProduction ? false : '.' }))
.pipe(gulp.src("./index.html").pipe(gulp.dest("./dist/")));;
}
function clean() {
return del(dist);
}
function clearCache() {
return dr.clearCache();
}
// exports.copyResources = copyResources;
const build = gulp.parallel(copyResources, buildSource);
const serve = gulp.series(
build,
function startServer(done) {
devServer.run({
open: !process.env.CI,
port: 9000
});
done();
}
)
// Reload browserSync
function reload(done) {
console.log('Reloading the browser');
devServer.reload();
done();
}
// Watch all files for rebuild and reload browserSync.
function watch() {
gulp.watch(['src/**/*', 'content/**/*'], gulp.series(build, reload));
}
const run = gulp.series(clean, serve, watch);
exports.build = build;
exports.clean = clean;
exports['clear-cache'] = clearCache;
exports.run = run;
exports.default = run;