From 31f5fc6c415ed72feb14dc97dcc8e9cbc4dcf735 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 6 Sep 2017 15:18:43 -0400 Subject: [PATCH] added browserify script #494 --- package.json | 8 +++++- tasks/build-js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 tasks/build-js diff --git a/package.json b/package.json index 4097aaf4a3..b5673a3130 100644 --- a/package.json +++ b/package.json @@ -11,12 +11,17 @@ "scripts": { "test": "grunt test lint", "start": "node ./bin/www", - "preinstall": "node version-check.js" + "preinstall": "node version-check.js", + "build-js": "node ./tasks/build-js" }, "devDependencies": { "autoprefixer": "7.1.1", "babel-polyfill": "6.16.0", + "babel-preset-es2015": "^6.24.1", + "babel-preset-react": "^6.24.1", + "babelify": "^7.3.0", "bluebird": "3.4.6", + "browserify": "^14.4.0", "buster": "0.7.12", "chromedriver": "2.25.1", "express": "4.15.2", @@ -55,6 +60,7 @@ "selenium-server-standalone-jar": "3.0.1", "stylefmt": "^6.0.0", "stylelint": "^8.0.0", + "watchify": "^3.9.0", "worldview-components": "1.3.3" } } diff --git a/tasks/build-js b/tasks/build-js new file mode 100644 index 0000000000..534d88cb57 --- /dev/null +++ b/tasks/build-js @@ -0,0 +1,78 @@ +#!/usr/bin/env node +// template from https://github.com/petesaia/node-native-task-runner/blob/master/bin/build-js + +//const UglifyJS = require('uglify-js'); +const fs = require('fs'); +const browserify = require('browserify'); +const watchify = require('watchify'); + +const entry = 'wv.main.js'; +const output = './build/js.js'; +const outputMin = './build/js.mins.js'; + +// override, should set this from an argument: --dev or --prod +// and default to dev +process.env.NODE_ENV = 'development'; + +const b = browserify(entry, { + debug: true, + sourceMaps: true, + cache: {}, + packageCache: {}, + plugin: [process.env.NODE_ENV !== 'production' ? watchify : null], + basedir: './web/js' +}) + .transform('babelify', { + plugins: [ ], + presets: [ + 'es2015', + 'react' + ] + }); + +function bundle() { + const stream = fs.createWriteStream(output); + + const onComplete = (function() { + const begin = Date.now(); + + return function() { + console.log('Build complete in ' + (Date.now() - begin) / 1000 + 'secs'); + }; + })(); + + b.bundle() + .on('error', function(err) { + console.error(err); + this.emit('end'); + }) + .pipe(stream) + + stream.on('finish', function() { + if (process.env.NODE_ENV === 'production') { + // console.log('Complete, now compressing because production.'); + + // const minified = UglifyJS.minify(fs.readFileSync(output, 'utf8'), { + // // warnings: true, + // compress: { + // dead_code: true + // } + // }); + + // if (minified.error) { + // return console.error('UGLIFY Error:', minified.error); + // } + + // fs.writeFileSync(outputMin, minified.code); + // onComplete(); + + } else { + console.log('All done! (dev mode - not compressing)'); + onComplete(); + } + }); +} + +bundle(); + +b.on('update', bundle); \ No newline at end of file