-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
156 lines (138 loc) · 4.02 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
// Requires Gulp v4.
const { src, dest, watch, series, parallel } = require('gulp');
// CSS Tools
const sass = require('gulp-sass')(require('sass'));
const cleancss = require('gulp-clean-css');
const autoprefixer = require('gulp-autoprefixer');
// JS Tools
const rollup = require('rollup');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const { babel } = require('@rollup/plugin-babel');
// Build Tools
const browsersync = require('browser-sync').create();
const concat = require('gulp-concat');
const terser = require('gulp-terser');
const rename = require('gulp-rename');
// Config
const config = require('./gulp-config');
// Minify SASS
function sassMinify() {
return src(config.style.output + 'style.css')
.pipe(cleancss()) // minify it
.pipe(rename({ suffix: '-min' })) // rename it
.pipe(dest(config.style.output))
.pipe(browsersync.stream()); // change the browser styles with no page reload
}
function sassCompile() {
return src(config.style.main)
.pipe(sass())
.on('error', function (err) {
console.log(err.formatted.toString());
this.emit('end');
})
.pipe(
// Add browser prefixes
autoprefixer({
overrideBrowserslist: ['> 1%', 'last 2 versions'],
})
)
.pipe(dest(config.style.output))
.pipe(browsersync.stream())
.on('end', () => {
sassMinify();
});
}
// Get all the code we've imported into our main script file
// Roll all those exported classes up into a single file
function jsRollup() {
return rollup
.rollup({
input: config.script.main,
plugins: [nodeResolve()],
})
.then((bundle) => {
writeFile(bundle, 'es', config.script.es6);
});
}
// Transiple the other output file to something
// old browsers can understand
function jsTranspile() {
return rollup
.rollup({
input: config.script.main,
plugins: [
nodeResolve(),
babel({
presets: [['@babel/env', { modules: false }]],
sourceMaps: true,
babelHelpers: 'bundled',
exclude: 'node_modules/**',
}),
],
})
.then((bundle) => {
writeFile(bundle, 'iife', config.script.es5);
});
}
function writeFile(bundle, format, file) {
return bundle.write({
file: file,
format,
name: 'script',
});
}
// Concat any code that from the dependencies folder before the
// rest of the code. This is useful for old scripts that are not
// available in module form.
function jsConcatES6() {
return src([config.script.deps, config.script.es6]).pipe(concat('script-esm.js')).pipe(dest(config.script.output));
}
function jsConcatES5() {
return src([config.script.deps, config.script.es5]).pipe(concat('script.js')).pipe(dest(config.script.output));
}
// Create minified versions of our JS for prod
function jsMinify() {
return src([config.script.es5, config.script.es6])
.pipe(terser({ output: { comments: false } })) // Minify the JS
.pipe(rename({ suffix: '-min' })) // Rename it
.pipe(dest(config.script.output)) // Write it to the output folder
.pipe(browsersync.stream());
}
function reload(done) {
browsersync.reload();
done();
}
// Watch all the build files, refresh code live in the browser,
// If the markup changes, reload the browser
function watchFiles() {
browsersync.init({
proxy: config.server.proxy,
host: config.server.host,
//server: {
// baseDir: './',
//},
open: 'external',
reloadOnRestart: true,
});
// Watch the Templates
// Force reload on change
watch(config.markup.templates, reload);
// Watch the Style
// Don't force style reload because we're
// streaming changes to browsersync
watch(
[config.style.src, config.style.templates, config.style.main],
{ events: 'all', ignoreInitial: false },
series(sassCompile)
);
// Watch the JS
// Don't force style reload because we're
// streaming changes to browsersync
watch(
[config.script.modules, config.script.deps, config.script.templates, config.script.main],
series(jsRollup, jsTranspile, jsConcatES6, jsConcatES5, jsMinify)
);
}
exports.watch = watchFiles;
// Buid all the code from source
exports.build = series(jsRollup, jsTranspile, jsConcatES6, jsConcatES5, jsMinify, sassCompile);