salix/gulpfile.js

152 lines
3.8 KiB
JavaScript
Raw Normal View History

var gulp = require('gulp');
var gutil = require('gulp-util');
var wrap = require('gulp-wrap');
var concat = require('gulp-concat');
var extend = require('gulp-extend');
var fs = require('fs');
var merge = require('merge-stream');
var del = require('del');
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
// Configuration
2017-03-01 08:55:17 +00:00
var srcDir = './client';
var buildDir = './services/nginx/static';
var langs = ['es', 'en'];
var modules = require('./spliting/modules.json');
var webpackConfig = require('./webpack.config.js');
// Clean
gulp.task('clean', function() {
return del(`${buildDir}/*`, {force: true});
});
// Spliting
var splitingFiles = './spliting/*';
gulp.task('spliting', function() {
var importTpl = fs.readFileSync('./spliting/import.tpl.js', 'utf8');
var requireTpl = fs.readFileSync('./spliting/require.tpl.js', 'utf8');
2017-03-01 08:55:17 +00:00
for (var modName in modules) {
var deps = modules[modName];
var splitFile = `${srcDir}/${modName}/src/spliting.js`;
try {
fs.unlinkSync(splitFile);
2017-03-01 08:55:17 +00:00
} catch (e) {}
fs.appendFileSync(splitFile, importTpl);
var i = deps.length;
2017-03-01 08:55:17 +00:00
while (i--) {
fs.appendFileSync(splitFile,
requireTpl.replace(/\$module/g, deps[i]));
2017-03-01 08:55:17 +00:00
}
}
});
// Webpack
gulp.task('webpack', ['spliting'], function(callback) {
var configCopy = Object.create(webpackConfig);
var compiler = webpack(configCopy);
compiler.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(callback) {
var configCopy = Object.create(webpackConfig);
2017-03-01 08:55:17 +00:00
for (var entry in configCopy.entry) {
configCopy.entry[entry]
.unshift('webpack-dev-server/client?http://0.0.0.0:8081/');
2017-03-01 08:55:17 +00:00
}
var compiler = webpack(configCopy);
2017-03-01 08:55:17 +00:00
new WebpackDevServer(compiler, {
publicPath: '/',
contentBase: buildDir,
quiet: false,
noInfo: false,
// hot: true,
stats: {
assets: true,
colors: true,
version: false,
hash: false,
timings: true,
chunks: false,
chunkModules: false
}
2017-03-07 16:11:56 +00:00
}).listen(8081, '0.0.0.0', function(err) {
if (err) throw new gutil.PluginError('webpack-dev-server', err);
});
});
// Locale
var localeFiles = `./${srcDir}/**/locale/*.json`;
gulp.task('locales', function() {
var streams = [];
2017-03-07 16:11:56 +00:00
for (var mod in modules)
2017-03-01 08:55:17 +00:00
for (var lang of langs) {
var localeFiles = `./client/${mod}/**/locale/${lang}.json`;
streams.push(gulp.src(localeFiles)
.pipe(extend(`${lang}.json`))
.pipe(gulp.dest(`${buildDir}/locale/${mod}`)));
}
return merge(streams);
});
// Routes
var routeFiles = `./${srcDir}/**/routes.js`;
gulp.task('routes', function() {
var fileTpl = '\n"<%=dirname%>": <%=contents%>';
var globalTpl = 'var routes = {<%=contents%>\n}';
var regex = new RegExp('^(.*)/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('routes.js', {newLine: ','}))
.pipe(wrap(globalTpl))
.pipe(gulp.dest(buildDir));
});
// Watch
gulp.task('watch', function() {
gulp.watch(splitingFiles, ['spliting']);
gulp.watch(routeFiles, ['routes']);
gulp.watch(localeFiles, ['locales']);
});
// Default
gulp.task('build', ['clean'], function() {
return gulp.start('routes', 'locales', 'webpack');
});
gulp.task('default', ['clean'], function() {
return gulp.start('watch', 'routes', 'locales', 'webpack-dev-server');
});