From 59f619a407d11ba8516288097cdbb9c30efb5c31 Mon Sep 17 00:00:00 2001 From: David Gang Date: Thu, 5 Jan 2017 15:57:55 +0200 Subject: [PATCH] Support for browserify (#28) * synchronize * Clarify engine support: Add engine support for nodejs through package.json. Fixes issue #12 * add support for browserify * Update package.json --- gulp/config.js | 18 ++++++++++- gulp/tasks/buildCustomJs.js | 63 +++++++++++++++++++++++++------------ gulpfile.js | 10 +++--- package.json | 7 +++-- 4 files changed, 70 insertions(+), 28 deletions(-) diff --git a/gulp/config.js b/gulp/config.js index ce4a39b8..65996e30 100644 --- a/gulp/config.js +++ b/gulp/config.js @@ -5,11 +5,20 @@ let customModuleFile = 'custom.module.js'; let customCssFile = 'custom1.css'; let mainFile = 'main.js'; +let browserify; let view ; function setView(_view) { view = _view; } +function getBrowserify() { + return browserify; +} + +function setBrowserify(_browserify) { + browserify = _browserify; +} + function getView(){ return view; } @@ -30,6 +39,10 @@ function mainPath() { return viewJsDir()+'/*.js'; } +function mainJsPath() { + return viewJsDir()+'/main.js'; +} + function customCssMainPath() { return viewCssDir()+'/*.css'; } @@ -79,6 +92,7 @@ let buildParams = { customPath: customPath, customModulePath: customModulePath, mainPath: mainPath, + mainJsPath: mainJsPath, viewJsDir: viewJsDir, viewCssDir: viewCssDir, customCssPath: customCssPath, @@ -94,6 +108,8 @@ module.exports = { buildParams: buildParams, PROXY_SERVER: PROXY_SERVER, setView: setView, - view: getView + view: getView, + getBrowserify: getBrowserify, + setBrowserify: setBrowserify }; diff --git a/gulp/tasks/buildCustomJs.js b/gulp/tasks/buildCustomJs.js index 9498cbbb..3e5d4061 100644 --- a/gulp/tasks/buildCustomJs.js +++ b/gulp/tasks/buildCustomJs.js @@ -1,14 +1,16 @@ 'use strict'; -let gulp = require('gulp'); -let babel = require('gulp-babel'); -let config = require('../config.js'); -let rename = require("gulp-rename"); -let concat = require("gulp-concat"); -let debug = require('gulp-debug'); -var wrap = require("gulp-wrap"); -var glob = require('glob'); -var gutil = require('gulp-util'); +const gulp = require('gulp'); +const babel = require('gulp-babel'); +const config = require('../config.js'); +const rename = require("gulp-rename"); +const concat = require("gulp-concat"); +const debug = require('gulp-debug'); +const wrap = require("gulp-wrap"); +const glob = require('glob'); +const gutil = require('gulp-util'); +const fs = require("fs"); +const browserify = require("browserify"); let buildParams = config.buildParams; @@ -19,24 +21,45 @@ gulp.task('watch-js', () => { gulp.task('custom-js', () => { + if(config.getBrowserify()) { + buildByBrowserify(); + } + else { + buildByConcatination(); + } + +}); + +function buildByConcatination() { return gulp.src([buildParams.customModulePath(),buildParams.mainPath(),buildParams.customNpmJsPath(),'!'+buildParams.customPath(),'!'+buildParams.customNpmJsModulePath(),'!'+buildParams.customNpmJsCustomPath()]) .pipe(concat(buildParams.customFile)) .pipe(babel({ presets: ['es2015'] })) .on("error", function(err) { - if (err && err.codeFrame) { - gutil.log( - gutil.colors.red("Browserify error: "), - gutil.colors.cyan(err.filename) + ` [${err.loc.line},${err.loc.column}]`, - "\r\n" + err.message + "\r\n" + err.codeFrame); - } - else { - gutil.log(err); - } - this.emit("end"); + if (err && err.codeFrame) { + gutil.log( + gutil.colors.red("Browserify error: "), + gutil.colors.cyan(err.filename) + ` [${err.loc.line},${err.loc.column}]`, + "\r\n" + err.message + "\r\n" + err.codeFrame); + } + else { + gutil.log(err); + } + this.emit("end"); }) .pipe(wrap('(function(){\n"use strict";\n<%= contents %>\n})();')) .pipe(gulp.dest(buildParams.viewJsDir())); +} -}); +function buildByBrowserify() { + return browserify({ + entries: buildParams.mainJsPath(), + paths:[ + buildParams.viewJsDir()+'/node_modules' + ] + }) + .transform("babelify",{presets: ["es2015"]}) + .bundle() + .pipe(fs.createWriteStream(buildParams.customPath())); +} diff --git a/gulpfile.js b/gulpfile.js index 0546efc8..135fc85b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,11 +1,11 @@ -var gulp = require('gulp'); -var minimist = require('minimist'); -var requireDir = require('require-dir'); +const gulp = require('gulp'); +const minimist = require('minimist'); +const requireDir = require('require-dir'); requireDir('./gulp/tasks', { recurse: true }); -var config = require('./gulp/config'); +const config = require('./gulp/config'); var options = minimist(process.argv.slice(2)); config.setView(options.view); - +config.setBrowserify(options.browserify); diff --git a/package.json b/package.json index 3dc7a0c6..05c7a2dc 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,10 @@ "author": "", "devDependencies": { "babel-preset-es2015": "6.6.0", + "babelify": "7.3.0", "bluebird": "3.3.1", "browser-sync": "2.9.9", + "browserify": "13.1.1", "concat-stream": "1.5.1", "connect-modrewrite": "0.8.2", "css": "2.2.1", @@ -40,6 +42,7 @@ "shelljs": "0.6.0", "tar-fs": "1.13.0" }, - "engines" : { "node" : ">=4.2" } + "engines": { + "node": ">=4.2" + } } -