diff --git a/.eslintrc b/.eslintrc index 1886d7e..66a9eff 100644 --- a/.eslintrc +++ b/.eslintrc @@ -5,4 +5,3 @@ extend: "eslint:recommended" rules: indent: [2, 4, {SwitchCase: 1}] quotes: [2, 'single'] - dot-notation: [2, {allowKeywords: false}] diff --git a/README.md b/README.md index b14c623..886c3ef 100644 --- a/README.md +++ b/README.md @@ -29,13 +29,14 @@ browserify -p [ extractify OPTIONS ] ### options ``` js -// Sample OPTIONS +// Sampe command line OPTIONS +browserify test/files/main.js -p ['extractify' --lazy [ [ --entries [ './test/files/dep4.js' './test/files/dep6.js' ] --outfile './test/lazy_bundle/lazy_bundle.js' ] ] --bundleMapOption [ --injectSoft false --dest 'test/lazy_bundle/map.json' ] ] > test/lazy_bundle/main_bundle.js + +// Sample api OPTIONS { - { - bundleMapOption: { - injectSoft: false, - dest: 'lazy_bundle/map.json' - } + bundleMapOption: { + injectSoft: false, + dest: 'lazy_bundle/map.json' }, lazy: [ { @@ -67,9 +68,10 @@ b.plugin('extractify', { lazy: [ { entries: [ - './files/dep4.js' + './files/dep4.js', + './files/dep6.js' ], - outfile: './bundles/lazy_bundle_dep4.js' + outfile: './bundles/lazy_bundle.js' } ] }); diff --git a/index.js b/index.js index 0ddab6c..8d272b8 100644 --- a/index.js +++ b/index.js @@ -8,9 +8,11 @@ var async = require('async'); var xtend = require('xtend'); var shasum = require('shasum'); var path = require('path'); +var processOptions = require('./lib/process_options'); var browserify; var pipeline; + function checkExistInArr(file, arr) { return ~arr.indexOf(file); } @@ -63,6 +65,7 @@ function getLazyBrowserify(referenceBundle, opt) { } module.exports = function extractify(b, opts) { + var bopts = b._options; var basedir = bopts.basedir || process.cwd(); var mainExternals = []; @@ -72,6 +75,7 @@ module.exports = function extractify(b, opts) { browserify = b.constructor; pipeline = b.pipeline.constructor; + opts = processOptions(opts); b.on('reset', function() { onReset = true; addHooks(); @@ -90,7 +94,8 @@ module.exports = function extractify(b, opts) { var injectOnce = true; var mapObject = {}; - if (lazyBundleMapOption && lazyBundleMapOption.injectSoft === false) { + if (lazyBundleMapOption && + (lazyBundleMapOption.injectSoft === false || lazyBundleMapOption.injectSoft === 'false')) { lazyBundleMapInjectSoft = false; } @@ -117,9 +122,10 @@ module.exports = function extractify(b, opts) { mapObject = require(row.file); mapObject = xtend(mapObject, moduleBundleMap); row.source = 'module.exports=' + JSON.stringify(mapObject, null, 4); - } else if (lazyBundleMapInjectSoft === false) { - fs.writeFileSync(path.resolve(basedir, lazyBundleMapDestination), + } else if (lazyBundleMapInjectSoft === false && injectOnce) { + fs.writeFileSync(path.resolve(basedir, lazyBundleMapDestination), JSON.stringify(moduleBundleMap, null, 4), {encoding: 'utf8'}); + injectOnce = false; } } @@ -172,7 +178,7 @@ module.exports = function extractify(b, opts) { order: 0 }); - if (lazyEntriesAll.indexOf(lazyEntry) >=0) { + if (lazyEntriesAll.indexOf(lazyEntry) >= 0) { throw new Error('Duplicate lazy config entry'); } diff --git a/lib/process_options.js b/lib/process_options.js new file mode 100644 index 0000000..30607dc --- /dev/null +++ b/lib/process_options.js @@ -0,0 +1,48 @@ +function handleUnderScore(obj, prop) { + if (!Array.isArray(obj[prop]) && obj[prop]._ && Array.isArray(obj[prop]._)) { + if (obj[prop]._.length === 0) { + obj[prop] = [obj[prop]]; + } else { + obj[prop] = obj[prop]._; + } + delete obj[prop]._; + } + return obj; +} + +module.exports = function processOptions(opts) { + if (!(opts && opts.lazy)) { + throw new Error('Please provide lazy option. Refer to extractify documentation for available options'); + } + + if (opts._) { + //command line + if (!opts._.length) { + delete opts._; + } + if (opts.bundleMapOption && opts.bundleMapOption._) { + delete opts.bundleMapOption._; + } + + } else { + // api or grunt + return opts; + } + + // process commandline options + opts = handleUnderScore(opts, 'lazy'); + for (var i = 0; i < opts.lazy.length; i++) { + if (opts.lazy[i].entries && typeof opts.lazy[i].entries === 'string') { + // handle single entry + opts.lazy[i].entries = [opts.lazy[i].entries]; + } else { + // handle multiple entries + opts.lazy[i] = handleUnderScore(opts.lazy[i], 'entries'); + } + + delete opts.lazy[i]._; + } + + return opts; +}; + diff --git a/package.json b/package.json index 9d61cac..4a31815 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "extractify", - "version": "1.0.2", + "version": "1.0.3", "description": "Browserify plugin to extract code to be lazy loaded into separate bundles", "repository": { "type": "git", diff --git a/test/command_line_options.js b/test/command_line_options.js new file mode 100644 index 0000000..b39351b --- /dev/null +++ b/test/command_line_options.js @@ -0,0 +1,104 @@ +/* Copyright 2016, Yahoo Inc. + Copyrights licensed under the MIT License. + See the accompanying LICENSE file for terms. */ + +var test = require('tap').test; + +test('commandline options', function(t) { + var processOptions = require('../lib/process_options'); + var opt1 = { + '_': [], + 'lazy': { + '_': [{ + '_': [], + 'entries': { + '_': ['foo1', 'bar1'] + }, + 'outfile': 'oo1' + }, { + '_': [], + 'entries': { + '_': ['foo12', 'bar2'] + }, + 'outfile': 'oo2' + }] + }, + 'bundleMapOption': { + '_': [], + 'dest': 'boo', + 'basedir': 'moo' + } + }; + var expected1 = { + 'lazy': [{ + 'entries': ['foo1', 'bar1'], + 'outfile': 'oo1' + }, { + 'entries': ['foo12', 'bar2'], + 'outfile': 'oo2' + }], + 'bundleMapOption': { + 'dest': 'boo', + 'basedir': 'moo' + } + }; + var opt2 = { + '_': [], + 'lazy': { + '_': [{ + '_': [], + 'entries': { + '_': ['foo1', 'bar1'] + }, + 'outfile': 'oo1' + }] + } + }; + var expected2 = { + 'lazy': [{ + 'entries': ['foo1', 'bar1'], + 'outfile': 'oo1' + }] + }; + var opt3 = { + '_': [], + 'lazy': { + '_': [{ + '_': [], + 'entries': { + '_': ['foo1', 'bar1'] + }, + 'outfile': 'oo1' + }] + } + }; + var expected3 = { + 'lazy': [{ + 'entries': ['foo1', 'bar1'], + 'outfile': 'oo1' + }] + }; + var opt4 = { + '_': [], + 'lazy': { + '_': [], + 'entries': 'foo1', + 'output': 'oo1' + } + }; + var expected4 = { + 'lazy': [{ + 'entries': ['foo1'], + 'output': 'oo1' + }] + }; + + t.plan(5); + t.same(processOptions(opt1), expected1); + t.same(processOptions(opt2), expected2); + t.same(processOptions(opt3), expected3); + t.same(processOptions(opt4), expected4); + t.throws(function() { + processOptions({}); + }); +});