forked from team-thyme/soundboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
157 lines (129 loc) · 3.86 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
import gulp from 'gulp';
import { argv } from 'yargs';
import { spawn } from 'child_process';
import path from 'path';
import del from 'del';
import sourceStream from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import sass from 'gulp-sass';
import postcss from 'gulp-postcss';
import rename from 'gulp-rename';
import gulpif from 'gulp-if';
import { log } from 'gulp-util';
import gulpLivereload from 'gulp-livereload';
import gulpSourcemaps from 'gulp-sourcemaps';
import uglify from 'gulp-uglify';
import browserify from 'browserify';
import svgify from 'svg-browserify';
import babelify from 'babelify';
import watchify from 'watchify';
import yamlify from 'yamlify';
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
const buildDir = `${__dirname}/public/build`;
const {
compress = true,
livereload = false,
sourcemaps = false,
} = argv;
const browserifyOptions = {
entries: ['src/client/scripts/main.js'],
debug: sourcemaps,
};
gulp.task('php-server', (callback) => {
const {
host = '0.0.0.0',
port = 80,
} = argv;
// Start PHP built-in web server
spawn('php', [
'-S',
`${host}:${port}`,
'-t',
`${__dirname}/public`,
`${__dirname}/router.php`,
], {
shell: true,
stdio: [
process.stdin,
process.stdout,
process.stderr,
],
});
});
gulp.task('build', ['build:scripts', 'build:styles', 'build:iconfont']);
gulp.task('watch', ['watch:scripts', 'watch:styles']);
gulp.task('clean:styles', () => del([`${buildDir}/*.{css,css.map}`]));
gulp.task('build:styles', ['clean:styles'], () => {
const sassOptions = {
importer(url, prev, done) {
if (url[0] === '~') {
url = path.resolve('node_modules', url.substr(1));
}
return { file: url };
},
};
const processors = [
autoprefixer({ browsers: ['last 1 version'] }),
];
if (compress) {
processors.push(cssnano());
}
return gulp.src('src/client/styles/**/*.scss')
.pipe(gulpif(sourcemaps, gulpSourcemaps.init()))
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(postcss(processors))
.pipe(rename('style.css'))
.pipe(gulpif(sourcemaps, gulpSourcemaps.write('./')))
.pipe(gulp.dest(buildDir))
.pipe(gulpif(livereload, gulpLivereload()));
});
gulp.task('watch:styles', (callback) => {
if (livereload) {
gulpLivereload.listen();
}
gulp.watch('src/client/styles/**/*.scss', ['build:styles']);
});
gulp.task('clean:scripts', () => del([`${buildDir}/*.{js,js.map}`]));
function createBundler(options = {}) {
return browserify(Object.assign({}, browserifyOptions, options))
.transform(babelify)
.transform(svgify)
.transform(yamlify);
}
function bundle(bundler) {
log('[browserify] Bundle start');
return bundler.bundle(() => log('[browserify] Bundle completed'))
.on('error', (err) => {
log('[browserify]', err.toString());
})
.pipe(sourceStream('main.js'))
.pipe(rename('script.js'))
.pipe(buffer())
.pipe(gulpif(sourcemaps, gulpSourcemaps.init({ loadMaps: true })))
.pipe(gulpif(compress, uglify()))
.pipe(gulpif(sourcemaps, gulpSourcemaps.write('./')))
.pipe(gulp.dest(buildDir))
.pipe(gulpif(livereload, gulpLivereload()));
}
gulp.task('build:scripts', ['clean:scripts'], () => {
const bundler = createBundler();
return bundle(bundler);
});
gulp.task('watch:scripts', (callback) => {
if (livereload) {
gulpLivereload.listen();
}
const bundler = createBundler(watchify.args)
.plugin(watchify);
bundler.on('update', () => {
log('[watch:scripts] Scripts changed, starting build');
bundle(bundler);
});
bundle(bundler);
});
gulp.task('clean:iconfont', () => del([`${buildDir}/iconfont/`]));
gulp.task('build:iconfont', ['clean:iconfont'], () => {
return gulp.src('src/client/iconfont/**/*.{css,eot,svg,ttf,woff,woff2}')
.pipe(gulp.dest(`${buildDir}/iconfont`));
});