-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.babel.js
173 lines (149 loc) · 4.65 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
import autoprefixer from 'autoprefixer';
import babelify from 'babelify';
import browserify from 'browserify';
import buffer from 'vinyl-buffer';
import cssnano from 'cssnano';
import del from 'del';
import fs from 'fs-extra';
import gulp from 'gulp';
import gulpif from 'gulp-if';
import gulpLivereload from 'gulp-livereload';
import path from 'path';
import postcss from 'gulp-postcss';
import rename from 'gulp-rename';
import gulpSass from 'gulp-sass';
import sourceStream from 'vinyl-source-stream';
import svgify from 'svg-browserify';
import uglify from 'gulp-uglify';
import watchify from 'watchify';
import { argv } from 'yargs';
import sass from 'sass';
import gulpSourcemaps from 'gulp-sourcemaps';
const publicDir = `${__dirname}/public`;
const distDir = `${__dirname}/dist`;
const buildDir = `${publicDir}/build`;
const {
livereload = false,
sourcemaps = false,
} = argv;
const browserifyOptions = {
entries: ['frontend/src/main.js'],
debug: sourcemaps,
};
gulp.task('clean:styles', () => del([`${buildDir}/*.{css,css.map}`]));
gulp.task('build:styles', gulp.series('clean:styles', () => {
return gulp.src('frontend/styles/**/*.scss')
.pipe(gulpSass(sass)({
importers: [
(url) => {
let resolvedUrl = url;
if (url[0] === '~') {
resolvedUrl = path.resolve('node_modules', url.substr(1));
}
return { file: resolvedUrl };
}
],
}))
.pipe(postcss([
autoprefixer(),
cssnano(),
]))
.pipe(rename('style.css'))
.pipe(gulp.dest(buildDir))
.pipe(gulpif(livereload, gulpLivereload()));
}));
gulp.task('watch:styles', () => {
if (livereload) {
gulpLivereload.listen();
}
gulp.watch('frontend/styles/**/*.scss', gulp.series('build:styles'));
});
gulp.task('clean:scripts', () => del([
`${buildDir}/*.{js,js.map}`,
`${buildDir}/ogv`,
]));
function createBundler(options = {}) {
return browserify({...browserifyOptions, ...options})
.transform(babelify)
.transform(svgify);
}
function bundle(bundler) {
console.log('[browserify] Bundle start');
return bundler
.bundle(() => console.log('[browserify] Bundle completed'))
.on('error', (err) => {
console.log('[browserify]', err.toString());
})
.pipe(sourceStream('main.js'))
.pipe(rename('script.js'))
.pipe(buffer())
.pipe(gulpif(sourcemaps, gulpSourcemaps.init({ loadMaps: true })))
.pipe(uglify())
.pipe(gulpif(sourcemaps, gulpSourcemaps.write('./')))
.pipe(gulp.dest(buildDir))
.pipe(gulpif(livereload, gulpLivereload()));
}
const buildScripts = () => {
// Copy relevant ogv.js workers.
fs.copy(`${__dirname}/node_modules/ogv/dist`, `${buildDir}/ogv`, {
overwrite: true,
filter: (file) => {
const baseName = path.basename(file);
return (
baseName === 'dist' ||
baseName === 'ogv-worker-audio.js' ||
baseName.startsWith('ogv-decoder-audio-') ||
baseName.startsWith('ogv-demuxer-')
);
}
}, (error) => {
if (error && error.code !== 'EEXIST') {
throw error;
}
});
// Copy dist files to public once
fs.copy(distDir, publicDir, { overwrite: false }, (error) => {
if (error && error.code !== 'EEXIST') {
throw error;
}
});
// Below code plain just doesn't work while documentation states it should.
gulp
.src('dist/config.json')
.pipe(rename('config.json'))
.pipe(gulp.dest(publicDir, { overwrite: false }));
return bundle(createBundler());
};
gulp.task('build:scripts', gulp.series(
'clean:scripts',
buildScripts
));
gulp.task('watch:scripts', () => {
if (livereload) {
gulpLivereload.listen();
}
const bundler = createBundler(watchify.args)
.plugin(watchify);
bundler.on('update', () => {
console.log('[watch:scripts] Scripts changed, starting build');
bundle(bundler);
});
bundle(bundler);
});
gulp.task('clean:iconfont', () => del([`${buildDir}/iconfont/`]));
const buildIconfont = () =>
gulp.src('frontend/iconfont/**/*.{css,eot,svg,ttf,woff,woff2}')
.pipe(gulp.dest(`${buildDir}/iconfont`));
gulp.task('build:iconfont', gulp.series(
'clean:iconfont',
buildIconfont
));
gulp.task('build', gulp.parallel(
'build:scripts',
'build:styles',
'build:iconfont'
));
gulp.task('watch', gulp.parallel(
'watch:scripts',
'watch:styles'
));