2016-10-06 09:18:02 +00:00
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
var path = require('path');
|
|
|
|
var gulp = require('gulp');
|
2016-11-11 11:04:50 +00:00
|
|
|
var gutil = require('gulp-util');
|
2016-10-21 06:38:58 +00:00
|
|
|
var wrap = require('gulp-wrap');
|
2016-11-23 10:44:33 +00:00
|
|
|
var concat = require('gulp-concat');
|
2017-01-24 15:06:07 +00:00
|
|
|
var extend = require('gulp-extend');
|
2016-11-23 10:44:33 +00:00
|
|
|
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');
|
2016-11-23 10:44:33 +00:00
|
|
|
var webpackConfig = require('./webpack.config.js');
|
2016-10-06 09:18:02 +00:00
|
|
|
|
2016-11-18 13:12:57 +00:00
|
|
|
// Clean
|
|
|
|
|
2017-01-02 06:28:36 +00:00
|
|
|
var buildDir = '../services/nginx/static';
|
2016-11-18 13:12:57 +00:00
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
gulp.task('clean', function() {
|
2017-01-02 06:28:36 +00:00
|
|
|
return del(buildDir + '/*', {force: true});
|
2016-11-18 13:12:57 +00:00
|
|
|
});
|
|
|
|
|
2016-10-21 06:38:58 +00:00
|
|
|
// Spliting
|
|
|
|
|
2016-11-11 11:04:50 +00:00
|
|
|
var splitingDir = './app/src/spliting';
|
2017-01-24 15:06:07 +00:00
|
|
|
var splitingFiles = splitingDir +'/*';
|
2016-10-21 06:38:58 +00:00
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
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);
|
2016-11-11 11:04:50 +00:00
|
|
|
|
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');
|
2016-10-21 06:38:58 +00:00
|
|
|
|
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,
|
2016-11-23 10:44:33 +00:00
|
|
|
requireTpl.replace(/\$module/g, deps[i]));
|
2017-01-02 06:28:36 +00:00
|
|
|
}
|
2016-11-11 11:04:50 +00:00
|
|
|
});
|
2016-10-21 06:38:58 +00:00
|
|
|
|
2016-11-11 11:04:50 +00:00
|
|
|
// Webpack
|
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
gulp.task('webpack', ['spliting'], function(callback) {
|
2017-01-02 06:28:36 +00:00
|
|
|
var myDevConfig = Object.create(webpackConfig);
|
|
|
|
myDevConfig.debug = true;
|
2016-11-23 10:44:33 +00:00
|
|
|
|
2017-01-02 06:28:36 +00:00
|
|
|
var devCompiler = webpack(myDevConfig);
|
2016-11-23 10:44:33 +00:00
|
|
|
|
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();
|
|
|
|
});
|
2016-11-18 13:12:57 +00:00
|
|
|
});
|
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
gulp.task('webpack-dev-server', ['spliting'], function() {
|
2017-01-02 06:28:36 +00:00
|
|
|
var myConfig = Object.create(webpackConfig);
|
|
|
|
myConfig.debug = true;
|
2016-11-23 10:44:33 +00:00
|
|
|
|
2017-01-02 06:28:36 +00:00
|
|
|
for (var entry in myConfig.entry)
|
|
|
|
myConfig.entry[entry]
|
2016-11-23 10:44:33 +00:00
|
|
|
.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
|
|
|
|
}
|
|
|
|
})
|
2016-11-23 10:44:33 +00:00
|
|
|
.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}`));
|
|
|
|
}
|
2016-10-21 06:38:58 +00:00
|
|
|
});
|
|
|
|
|
2016-11-11 11:04:50 +00:00
|
|
|
// Routes
|
|
|
|
|
|
|
|
var routeFiles = './crud/**/routes.js';
|
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
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};
|
|
|
|
}
|
2016-10-21 06:38:58 +00:00
|
|
|
|
2017-01-02 06:28:36 +00:00
|
|
|
return gulp.src(routeFiles)
|
2016-12-15 10:26:15 +00:00
|
|
|
.pipe(wrap(fileTpl, cb))
|
2016-11-23 10:44:33 +00:00
|
|
|
.pipe(concat('salix.routes.js', {newLine: ','}))
|
|
|
|
.pipe(wrap(globalTpl))
|
|
|
|
.pipe(babel({presets: ['es2015']}))
|
|
|
|
.pipe(gulp.dest(buildDir));
|
2016-11-11 11:04:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Watch
|
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
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']);
|
2016-11-11 11:04:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Default
|
2016-10-21 06:38:58 +00:00
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
gulp.task('build', ['clean'], function() {
|
2017-01-24 15:06:07 +00:00
|
|
|
return gulp.start('routes', 'webpack', 'locales');
|
2016-11-18 13:12:57 +00:00
|
|
|
});
|
2016-10-21 06:38:58 +00:00
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
gulp.task('default', ['clean'], function() {
|
2017-01-24 15:06:07 +00:00
|
|
|
return gulp.start('watch', 'routes', 'locales', 'webpack-dev-server');
|
2016-11-18 13:12:57 +00:00
|
|
|
});
|