salix/@salix/gulpfile.js

155 lines
4.0 KiB
JavaScript
Raw Normal View History

2016-10-06 09:18:02 +00:00
var path = require('path');
var gulp = require('gulp');
var gutil = require('gulp-util');
var wrap = require('gulp-wrap');
var concat = require('gulp-concat');
2017-01-24 15:06:07 +00:00
var extend = require('gulp-extend');
var babel = require('gulp-babel');
var fs = require('fs');
var del = require('del');
var webpack = require('webpack');
2016-12-15 10:26:15 +00:00
var WebpackDevServer = require('webpack-dev-server');
var webpackConfig = require('./webpack.config.js');
2016-10-06 09:18:02 +00:00
// Clean
2017-01-02 06:28:36 +00:00
var buildDir = '../services/nginx/static';
gulp.task('clean', function() {
2017-01-02 06:28:36 +00:00
return del(buildDir + '/*', {force: true});
});
// Spliting
var splitingDir = './app/src/spliting';
2017-01-24 15:06:07 +00:00
var splitingFiles = splitingDir +'/*';
gulp.task('spliting', function() {
2017-01-24 15:06:07 +00:00
var jsonDeps = fs.readFileSync(splitingDir +'/deps.json');
2017-01-02 06:28:36 +00:00
var modules = JSON.parse(jsonDeps);
2017-01-24 15:06:07 +00:00
var importTpl = fs.readFileSync(splitingDir +'/import.tpl.js', 'utf8');
var requireTpl = fs.readFileSync(splitingDir +'/require.tpl.js', 'utf8');
2017-01-24 15:06:07 +00:00
for (var modName in modules) {
2017-01-02 06:28:36 +00:00
var deps = modules[modName];
2017-01-24 15:06:07 +00:00
var splitFile = './'+ modName +'/src/spliting.js';
2017-01-02 06:28:36 +00:00
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]));
2017-01-02 06:28:36 +00:00
}
});
// Webpack
gulp.task('webpack', ['spliting'], function(callback) {
2017-01-02 06:28:36 +00:00
var myDevConfig = Object.create(webpackConfig);
myDevConfig.debug = true;
2017-01-02 06:28:36 +00:00
var devCompiler = webpack(myDevConfig);
2017-01-02 06:28:36 +00:00
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() {
2017-01-02 06:28:36 +00:00
var myConfig = Object.create(webpackConfig);
myConfig.debug = true;
2017-01-02 06:28:36 +00:00
for (var entry in myConfig.entry)
myConfig.entry[entry]
.unshift('webpack-dev-server/client?http://0.0.0.0:8081/');
2017-01-02 06:28:36 +00:00
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) {
2017-01-24 15:06:07 +00:00
if (err) throw new gutil.PluginError('webpack-dev-server', err);
gutil.log('[webpack-dev-server]', 'Listening');
});
2017-01-02 06:28:36 +00:00
});
2017-01-24 15:06:07 +00:00
// Locale
var localeFiles = './**/locale/*.json';
gulp.task('locales', function() {
var modules = ['core', 'crud', 'app', 'login'];
var langs = ['es', 'en'];
for(var mod of modules)
for(var lang of langs) {
var localeFiles = `./${mod}/**/locale/${lang}.json`;
gulp.src(localeFiles)
.pipe(extend(`${lang}.json`))
.pipe(gulp.dest(`${buildDir}/locale/${mod}`));
}
});
// Routes
var routeFiles = './crud/**/routes.js';
gulp.task('routes', function() {
2017-01-02 06:28:36 +00:00
var fileTpl = '\n"<%=dirname%>": <%=contents%>';
var globalTpl = 'var routes = {<%=contents%>\n}';
var regex = new RegExp('^src/(.*)/routes\.js$');
2016-12-15 10:26:15 +00:00
2017-01-02 06:28:36 +00:00
function cb(file) {
var relative = file.relative.replace(/\\/g, '/');
var dirname = relative.match(regex)[1];
return {dirname: dirname};
}
2017-01-02 06:28:36 +00:00
return gulp.src(routeFiles)
2016-12-15 10:26:15 +00:00
.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() {
2017-01-02 06:28:36 +00:00
gulp.watch(splitingFiles, ['spliting']);
2017-01-24 15:06:07 +00:00
gulp.watch(routeFiles, ['routes']);
gulp.watch(localeFiles, ['locales']);
});
// Default
gulp.task('build', ['clean'], function() {
2017-01-24 15:06:07 +00:00
return gulp.start('routes', 'webpack', 'locales');
});
gulp.task('default', ['clean'], function() {
2017-01-24 15:06:07 +00:00
return gulp.start('watch', 'routes', 'locales', 'webpack-dev-server');
});