-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
211 lines (194 loc) · 6.63 KB
/
gulpfile.babel.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
import { watch, series, parallel, src, dest } from 'gulp' // gulp
import noop from 'gulp-noop' // gulp
import browserSync from 'browser-sync' // gulp
import cacheBuster from 'gulp-rev' // gulp
import sourcemaps from 'gulp-sourcemaps' // gulp
import filenames from 'gulp-filenames' // gulp
import tap from 'gulp-tap' // gulp
import sprite from 'gulp-svg-sprite' // gulp
import imagemin from 'gulp-imagemin' // gulp
import postcss from 'gulp-postcss' // css
import postcssImport from 'postcss-import' // css
import postcssNesting from 'postcss-nesting' // css
import postcssNano from 'cssnano' // css
import postcssCustomMedia from 'postcss-custom-media' // css
// import postcssCustomProperties from 'postcss-custom-properties' // css
import postcssCalc from 'postcss-calc' // css
import postscssAutoprefixer from 'autoprefixer' // css
import postcssMixins from 'postcss-mixins' // css
import browserify from 'browserify' // js
import buffer from 'vinyl-buffer' // js
import babelify from 'babelify' // js
import uglify from 'gulp-uglify-es' // js
import fs from 'fs' // general
import del from 'del' // general
import yargs from 'yargs' // general
import dotenv from 'dotenv' // general
dotenv.config()
// set isProduction and module variables
const {
production: isProduction = false
} = yargs.argv
// create browserSync server
const server = browserSync.create()
// create array of postcss processors (order is important)
const cssProcessors = [
postcssImport, // must be the first
postcssMixins, // https://github.com/postcss/postcss-mixins
postcssNesting, // https://github.com/postcss/postcss-nested
postcssCalc, // https://github.com/postcss/postcss-calc
postcssCustomMedia, // https://drafts.csswg.org/mediaqueries-5/#custom-mq
].concat(isProduction ? [ // enable additional processors on production (saves time on development)
// postcssCustomProperties, // https://www.w3.org/TR/css-variables-1/
postscssAutoprefixer({ browsers: ['last 2 versions', 'ie >= 11'] }), // same as babel,
postcssNano,
] : [])
const config = {
serverUrl: `http://127.0.0.1:8000/`,
proxyPort: 3000,
buildDest: `dist/`,
js: {
entry: `src/js/*.js`,
watch: [`src/js/**/*.js`],
},
css: {
entry: `src/css/*.css`,
watch: [`src/css/**/*.css`, `src/js/components/**/*.css`],
},
images: {
entry: `src/images/**`,
watch: [],
directory: `images`,
},
etc: {
entry: `src/etc/**`,
watch: [`src/etc/**`],
directory: `etc`,
},
icons: {
entry: `src/icons/*.svg`,
watch: [`src/icons/*.svg`],
spriteConfig: {
mode: {
inline: true,
symbol: { // generate right into the build folder
dest: ``,
sprite: `sprite.svg`
}
}
}
},
templates: {
watch: [`*.html`]
},
manifest: {
base: `${process.cwd()}/dist/`,
path: `${process.cwd()}/dist/asset-manifest.json`,
merge: true,
}
}
/*
* Private tasks
* */
function reload(done) {
server.reload()
done()
}
async function serve() {
await server.init({
proxy: config.serverUrl,
port: config.proxyPort,
open: true,
notify: false
})
}
function clean(done) {
del([`${config.buildDest}/**/*`])
done()
}
function js() {
return src(config.js.entry, { read: false }) // no need of reading because browserify does
.pipe(tap(file => { // transform files using gulp-tap
file.contents = browserify(file.path, {
transform: [babelify],
debug: isProduction // enable source maps on production
}).bundle()
}))
.pipe(buffer()) // need this if you want to continue using the stream with other plugins
.pipe(isProduction ? sourcemaps.init({ loadMaps: true }) : noop())
.pipe(isProduction ? cacheBuster() : noop()) // cache bust on production
.pipe(isProduction ? uglify() : noop())
.pipe(isProduction ? sourcemaps.write('.', { includeContent: false }) : noop())
.pipe(isProduction ? dest(config.buildDest) : noop()) // this build is for manifest.json
.pipe(isProduction ? cacheBuster.manifest(config.manifest.path, config.manifest) : noop())
.pipe(dest(config.buildDest))
}
function css() {
return src(config.css.entry)
.pipe(isProduction ? sourcemaps.init() : noop()) // generate source-maps only for production
.pipe(isProduction ? cacheBuster() : noop()) // cache bust on production
.pipe(postcss(cssProcessors))
.pipe(isProduction ? sourcemaps.write('.', { includeContent: false }) : noop())
.pipe(isProduction ? dest(config.buildDest) : noop()) // this build is for manifest.json
.pipe(isProduction ? cacheBuster.manifest(config.manifest.path, config.manifest) : noop())
.pipe(dest(config.buildDest))
}
function icons() {
return src(config.icons.entry)
.pipe(sprite(config.icons.spriteConfig))
.pipe(dest(config.buildDest))
}
function images() {
return src(config.images.entry)
.pipe(imagemin([
imagemin.svgo({
plugins: [
{ removeViewBox: true },
{ cleanupIDs: false }
]
})
]))
.pipe(dest(config.buildDest + config.images.directory))
}
function etc() {
return src(config.etc.entry)
.pipe(dest(config.buildDest + config.etc.directory))
}
function createDevManifest(done) {
// asset manifest is not generated during development in order to save time
// nevertheless, it is required by php script to load the assets
const manifest = {}
for (const filename of filenames.get(`files`)) {
manifest[filename] = filename
}
fs.writeFile(config.manifest.path, JSON.stringify(manifest, null, 4), () => {
done()
})
}
function getCssAndJsFileNames() {
return src([config.css.entry, config.js.entry])
.pipe(filenames(`files`))
.pipe(noop())
}
function watchFiles() {
watch(config.css.watch, series(css, reload))
watch(config.js.watch, series(js, reload))
watch(config.icons.watch, series(icons, reload))
watch(config.templates.watch, series(reload))
}
/*
* Public task
* */
export default isProduction ?
series(
clean,
css, // not parallel because manifest.json creation
js, // not parallel because manifest.json creation
parallel(icons, images, etc)
) :
series(
clean,
getCssAndJsFileNames,
parallel(js, css, icons, serve, createDevManifest, images, etc),
watchFiles
)