var gulp = require ('gulp'); var wrap = require('gulp-wrap'); var concat = require ('gulp-concat'); var babel = require ('gulp-babel'); var fs = require ('fs'); // Routes var fileTpl = '\n"<%=file.path%>": <%=contents%>'; var globalTpl = 'var routes = {<%=contents%>\n}'; gulp.task ('routes', function () { return gulp.src ('./@salix/**/routes.js') .pipe (wrap (fileTpl)) .pipe (concat ('salix.routes.js', {newLine: ','})) .pipe (wrap (globalTpl)) .pipe (babel ({presets: ['es2015']})) .pipe (gulp.dest ('./build/private')); }); // Spliting var baseDir = './@salix/app/src/spliting'; var depsFile = baseDir +'/deps.json'; function splitingFunc () { try { var jsonDeps = fs.readFileSync (depsFile); var modules = JSON.parse (jsonDeps); } catch (e) { console.error (e); return; } var importTpl = fs.readFileSync (baseDir +'/import.tpl.js', 'utf8'); var requireTpl = fs.readFileSync (baseDir +'/require.tpl.js', 'utf8'); for (var modName in modules) { var deps = modules[modName]; var splitFile = './@salix/'+ 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])); } } gulp.task ('spliting', function () { splitingFunc (); //gulp.watch (depsFile, splitingFunc); }); // Default gulp.task ('default', ['routes', 'spliting']);