salix/@salix/gulpfile.js

82 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-10-06 09:18:02 +00:00
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 webpack = require ('webpack-stream');
var webpackConfig = require ('./webpack.config.js');
2016-10-06 09:18:02 +00:00
// 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];
2016-11-04 10:32:27 +00:00
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', ['spliting'], function (callback)
{
return gulp.src (['app/index.js', 'login/index.js'])
.pipe (webpack (webpackConfig))
.pipe (gulp.dest ('build/public/'));
});
// 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 ('./build/public'));
});
// Watch
gulp.task ('watch', function ()
{
gulp.watch (routeFiles, ['routes']);
gulp.watch (splitingFiles, ['spliting']);
});
// Default
gulp.task ('build', ['routes', 'webpack']);
gulp.task ('default', ['watch', 'build']);