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');
|
|
|
|
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
|
|
|
|
|
|
|
|
var buildDir = '../services/nginx/static'
|
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
gulp.task('clean', function() {
|
|
|
|
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';
|
|
|
|
var splitingFiles = splitingDir +'/*';
|
2016-10-21 06:38:58 +00:00
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
gulp.task('spliting', function() {
|
|
|
|
var jsonDeps = fs.readFileSync(splitingDir +'/deps.json');
|
|
|
|
var modules = JSON.parse(jsonDeps);
|
2016-11-11 11:04:50 +00:00
|
|
|
|
2016-11-23 10:44:33 +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
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
for(var modName in modules)
|
2016-10-21 06:38:58 +00:00
|
|
|
{
|
|
|
|
var deps = modules[modName];
|
2016-11-04 10:32:27 +00:00
|
|
|
var splitFile = './'+ modName +'/src/spliting.js';
|
2016-10-21 06:38:58 +00:00
|
|
|
|
|
|
|
try {
|
2016-11-23 10:44:33 +00:00
|
|
|
fs.unlinkSync(splitFile);
|
2016-10-21 06:38:58 +00:00
|
|
|
}
|
2016-11-23 10:44:33 +00:00
|
|
|
catch(e) {}
|
2016-10-21 06:38:58 +00:00
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
fs.appendFileSync(splitFile, importTpl);
|
2016-10-21 06:38:58 +00:00
|
|
|
|
|
|
|
var i = deps.length;
|
2016-11-23 10:44:33 +00:00
|
|
|
while(i--)
|
|
|
|
fs.appendFileSync(splitFile,
|
|
|
|
requireTpl.replace(/\$module/g, deps[i]));
|
2016-10-21 06:38:58 +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) {
|
|
|
|
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();
|
|
|
|
});
|
2016-11-18 13:12:57 +00:00
|
|
|
});
|
|
|
|
|
2016-11-23 10:44:33 +00:00
|
|
|
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: '/',
|
2016-11-23 17:56:12 +00:00
|
|
|
contentBase: buildDir,
|
|
|
|
quiet: false,
|
|
|
|
noInfo: false,
|
2016-11-25 14:35:26 +00:00
|
|
|
//hot: true,
|
2016-11-23 17:56:12 +00:00
|
|
|
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) {
|
|
|
|
if(err) throw new gutil.PluginError('webpack-dev-server', err);
|
|
|
|
gutil.log('[webpack-dev-server]', 'Listening');
|
|
|
|
});
|
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() {
|
2016-12-15 10:26:15 +00:00
|
|
|
var fileTpl = '\n"<%=dirname%>": <%=contents%>';
|
2016-11-11 11:04:50 +00:00
|
|
|
var globalTpl = 'var routes = {<%=contents%>\n}';
|
2016-12-15 10:26:15 +00:00
|
|
|
var regex = new RegExp('^src/(.*)/routes\.js$');
|
|
|
|
|
|
|
|
function cb(file) {
|
|
|
|
var relative = file.relative.replace('\\', '/');
|
|
|
|
var dirname = relative.match(regex)[1];
|
|
|
|
return {dirname: dirname};
|
|
|
|
}
|
2016-10-21 06:38:58 +00:00
|
|
|
|
2016-11-23 10:44:33 +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() {
|
|
|
|
gulp.watch(routeFiles, ['routes']);
|
|
|
|
gulp.watch(splitingFiles, ['spliting']);
|
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() {
|
|
|
|
return gulp.start('routes', 'webpack');
|
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() {
|
|
|
|
return gulp.start('watch', 'routes', 'webpack-dev-server');
|
2016-11-18 13:12:57 +00:00
|
|
|
});
|