salix/@salix/gulpfile.js

107 lines
2.3 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-stream');
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-watch', ['spliting'], function ()
{
webpackConfig.watch = true;
return gulp.src (['app/index.js', 'login/index.js'])
.pipe (webpack (webpackConfig))
.pipe (gulp.dest (buildDir));
});
gulp.task ('webpack', ['spliting'], function ()
{
return gulp.src (['app/index.js', 'login/index.js'])
.pipe (webpack (webpackConfig))
.pipe (gulp.dest (buildDir));
});
// Routes
var routeFiles = './crud/**/routes.js';
gulp.task ('routes', function ()
{
var fileTpl = '\n"<%=file.relative%>": <%=contents%>';
var globalTpl = 'var routes = {<%=contents%>\n}';
return gulp.src (routeFiles)
.pipe (wrap (fileTpl))
.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-watch');
});