2015-04-29 22:45:02 +00:00
|
|
|
var exec = require('child_process').exec;
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
var gulp = require('gulp');
|
2015-06-07 22:11:03 +00:00
|
|
|
var gutil = require('gulp-util');
|
2015-12-28 23:34:32 +00:00
|
|
|
var express = require('express');
|
2016-01-25 00:01:37 +00:00
|
|
|
var proxy = require('express-http-proxy');
|
2015-12-28 23:34:32 +00:00
|
|
|
var webpack = require('webpack');
|
2017-04-14 02:33:44 +00:00
|
|
|
var through = require('through2');
|
|
|
|
var br = require('brotli');
|
2018-05-16 01:46:42 +00:00
|
|
|
var del = require('del');
|
2017-04-14 02:33:44 +00:00
|
|
|
|
|
|
|
function brotli(opts) {
|
2020-04-30 05:54:30 +00:00
|
|
|
return through.obj(function (file, enc, callback) {
|
2017-04-14 02:33:44 +00:00
|
|
|
if (file.isNull()) {
|
|
|
|
return callback(null, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.isStream()) {
|
2018-04-05 23:46:22 +00:00
|
|
|
this.emit(
|
|
|
|
'error',
|
|
|
|
new gutil.PluginError('brotli', 'Streams not supported')
|
|
|
|
);
|
2017-04-14 02:33:44 +00:00
|
|
|
} else if (file.isBuffer()) {
|
|
|
|
file.path += '.br';
|
2018-10-06 06:09:29 +00:00
|
|
|
file.contents = Buffer.from(br.compress(file.contents, opts).buffer);
|
2017-04-14 02:33:44 +00:00
|
|
|
return callback(null, file);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-06-07 22:11:03 +00:00
|
|
|
|
2018-05-16 01:46:42 +00:00
|
|
|
function clean() {
|
|
|
|
return del(['dist']);
|
2018-05-16 03:02:48 +00:00
|
|
|
}
|
2018-05-16 01:46:42 +00:00
|
|
|
|
|
|
|
function js(cb) {
|
2015-12-28 23:34:32 +00:00
|
|
|
var config = require('./webpack.config.prod.js');
|
|
|
|
var compiler = webpack(config);
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-12-28 23:34:32 +00:00
|
|
|
process.env['NODE_ENV'] = 'production';
|
2015-06-07 22:11:03 +00:00
|
|
|
|
2020-04-30 05:54:30 +00:00
|
|
|
compiler.run(function (err, stats) {
|
2020-05-08 02:16:23 +00:00
|
|
|
if (err) {
|
|
|
|
throw new gutil.PluginError('webpack', err);
|
|
|
|
}
|
2015-06-07 22:11:03 +00:00
|
|
|
|
2018-04-05 23:46:22 +00:00
|
|
|
gutil.log(
|
|
|
|
'[webpack]',
|
|
|
|
stats.toString({
|
|
|
|
colors: true
|
|
|
|
})
|
|
|
|
);
|
2016-01-06 23:28:11 +00:00
|
|
|
|
2020-05-08 02:16:23 +00:00
|
|
|
if (stats.hasErrors()) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2016-01-06 23:28:11 +00:00
|
|
|
|
2015-12-28 23:34:32 +00:00
|
|
|
cb();
|
|
|
|
});
|
2018-05-16 01:46:42 +00:00
|
|
|
}
|
2015-06-01 22:09:28 +00:00
|
|
|
|
2018-05-16 01:46:42 +00:00
|
|
|
function config() {
|
|
|
|
return gulp.src('../config.default.toml').pipe(gulp.dest('dist'));
|
|
|
|
}
|
2015-02-16 20:53:01 +00:00
|
|
|
|
2018-11-10 11:18:45 +00:00
|
|
|
function public() {
|
|
|
|
return gulp.src('public/**/*').pipe(gulp.dest('dist'));
|
2018-05-16 01:46:42 +00:00
|
|
|
}
|
2016-02-03 01:22:45 +00:00
|
|
|
|
2015-12-28 23:34:32 +00:00
|
|
|
function compress() {
|
2018-04-05 23:46:22 +00:00
|
|
|
return gulp
|
2018-11-10 11:18:45 +00:00
|
|
|
.src(['dist/**/*(*.js|*.css|*.json)', '!dist/**/*(*.dev.js)'])
|
2017-04-14 02:33:44 +00:00
|
|
|
.pipe(brotli({ quality: 11 }))
|
2018-05-16 01:46:42 +00:00
|
|
|
.pipe(gulp.dest('dist'));
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 01:46:42 +00:00
|
|
|
function cleanup() {
|
2018-11-10 11:18:45 +00:00
|
|
|
return del(['dist/**/*(*.js|*.css|*.json|*.map)']);
|
2018-05-16 01:46:42 +00:00
|
|
|
}
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2018-05-16 01:46:42 +00:00
|
|
|
function bindata(cb) {
|
2018-04-05 23:46:22 +00:00
|
|
|
exec(
|
2018-05-16 01:46:42 +00:00
|
|
|
'go-bindata -nomemcopy -nocompress -pkg assets -o ../assets/bindata.go -prefix "dist" dist/...',
|
2018-04-05 23:46:22 +00:00
|
|
|
cb
|
|
|
|
);
|
2018-05-16 01:46:42 +00:00
|
|
|
}
|
2015-02-05 00:37:34 +00:00
|
|
|
|
2018-05-16 01:46:42 +00:00
|
|
|
function serve() {
|
2018-04-13 18:47:39 +00:00
|
|
|
var config = require('./webpack.config.dev.js');
|
|
|
|
var compiler = webpack(config);
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
app.use(
|
|
|
|
require('webpack-dev-middleware')(compiler, {
|
|
|
|
noInfo: true,
|
|
|
|
publicPath: config.output.publicPath,
|
|
|
|
headers: {
|
|
|
|
'Access-Control-Allow-Origin': '*'
|
|
|
|
}
|
|
|
|
})
|
2018-04-05 23:46:22 +00:00
|
|
|
);
|
|
|
|
|
2018-04-13 18:47:39 +00:00
|
|
|
app.use(require('webpack-hot-middleware')(compiler));
|
2018-04-05 23:46:22 +00:00
|
|
|
|
2018-04-13 18:47:39 +00:00
|
|
|
app.use('/', express.static('dist'));
|
2018-04-05 23:46:22 +00:00
|
|
|
|
2018-04-13 18:47:39 +00:00
|
|
|
app.use(
|
|
|
|
'*',
|
|
|
|
proxy('localhost:1337', {
|
2020-04-30 05:54:30 +00:00
|
|
|
proxyReqPathResolver: function (req) {
|
2018-04-13 18:47:39 +00:00
|
|
|
return req.originalUrl;
|
2018-04-05 23:46:22 +00:00
|
|
|
}
|
2018-04-13 18:47:39 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-04-30 05:54:30 +00:00
|
|
|
app.listen(3000, function (err) {
|
2018-04-13 18:47:39 +00:00
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
return;
|
|
|
|
}
|
2018-04-05 23:46:22 +00:00
|
|
|
|
2018-04-13 18:47:39 +00:00
|
|
|
console.log('Listening at http://localhost:3000');
|
|
|
|
});
|
2018-05-16 01:46:42 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 05:54:30 +00:00
|
|
|
const build = gulp.series(
|
|
|
|
clean,
|
|
|
|
gulp.parallel(js, config),
|
|
|
|
compress,
|
|
|
|
cleanup,
|
|
|
|
bindata
|
|
|
|
);
|
2018-05-16 01:46:42 +00:00
|
|
|
|
2018-05-16 03:02:48 +00:00
|
|
|
const dev = gulp.series(
|
|
|
|
clean,
|
2018-11-10 11:18:45 +00:00
|
|
|
gulp.parallel(serve, public, gulp.series(config, bindata))
|
2018-05-16 03:02:48 +00:00
|
|
|
);
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2018-05-16 01:46:42 +00:00
|
|
|
gulp.task('build', build);
|
|
|
|
gulp.task('default', dev);
|