salix/@salix/gulpfile.js

137 lines
3.5 KiB
JavaScript

var path = require('path');
var gulp = require('gulp');
var gutil = require('gulp-util');
var wrap = require('gulp-wrap');
var concat = require('gulp-concat');
var babel = require('gulp-babel');
var fs = require('fs');
var del = require('del');
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var webpackConfig = require('./webpack.config.js');
// Clean
var buildDir = '../services/nginx/static';
gulp.task('clean', function() {
return del(buildDir + '/*', {force: true});
});
// Spliting
var splitingDir = './app/src/spliting';
var splitingFiles = splitingDir + '/*';
gulp.task('spliting', function() {
var jsonDeps = fs.readFileSync(splitingDir + '/deps.json');
var modules = JSON.parse(jsonDeps);
var importTpl = fs.readFileSync(splitingDir + '/import.tpl.js', 'utf8');
var requireTpl = fs.readFileSync(splitingDir + '/require.tpl.js', 'utf8');
for (var modName in modules)
{
var deps = modules[modName];
var splitFile = './' + modName + '/src/spliting.js';
try {
fs.unlinkSync(splitFile);
}
catch (e) {}
fs.appendFileSync(splitFile, importTpl);
var i = deps.length;
while (i--)
fs.appendFileSync(splitFile,
requireTpl.replace(/\$module/g, deps[i]));
}
});
// Webpack
gulp.task('webpack', ['spliting'], function(callback) {
var myDevConfig = Object.create(webpackConfig);
myDevConfig.debug = true;
var devCompiler = webpack(myDevConfig);
devCompiler.run(function(err, stats) {
if (err) throw new gutil.PluginError('webpack', err);
gutil.log('[webpack]', stats.toString({colors: true}));
callback();
});
});
gulp.task('webpack-dev-server', ['spliting'], function() {
var myConfig = Object.create(webpackConfig);
myConfig.debug = true;
for (var entry in myConfig.entry)
myConfig.entry[entry]
.unshift('webpack-dev-server/client?http://0.0.0.0:8081/');
var devServer = new WebpackDevServer(webpack(myConfig), {
publicPath: '/',
contentBase: buildDir,
quiet: false,
noInfo: false,
// hot: true,
stats: {
assets: true,
colors: true,
version: false,
hash: false,
timings: true,
chunks: false,
chunkModules: false
}
})
.listen(8081, '0.0.0.0', function(err) {
if (err) throw new gutil.PluginError('webpack-dev-server', err);
gutil.log('[webpack-dev-server]', 'Listening');
});
});
// Routes
var routeFiles = './crud/**/routes.js';
gulp.task('routes', function() {
var fileTpl = '\n"<%=dirname%>": <%=contents%>';
var globalTpl = 'var routes = {<%=contents%>\n}';
var regex = new RegExp('^src/(.*)/routes\.js$');
function cb(file) {
var relative = file.relative.replace(/\\/g, '/');
var dirname = relative.match(regex)[1];
return {dirname: dirname};
}
return gulp.src(routeFiles)
.pipe(wrap(fileTpl, cb))
.pipe(concat('salix.routes.js', {newLine: ','}))
.pipe(wrap(globalTpl))
.pipe(babel({presets: ['es2015']}))
.pipe(gulp.dest(buildDir));
});
// Watch
gulp.task('watch', function() {
gulp.watch(routeFiles, ['routes']);
gulp.watch(splitingFiles, ['spliting']);
});
// Default
gulp.task('build', ['clean'], function() {
return gulp.start('routes', 'webpack');
});
gulp.task('default', ['clean'], function() {
return gulp.start('watch', 'routes', 'webpack-dev-server');
});