Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #7 from yahoo/commandLineOptions
Browse files Browse the repository at this point in the history
Make extractify command line friendly
  • Loading branch information
tufandevrim committed May 18, 2016
2 parents 40db677 + c259570 commit 74a7018
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 14 deletions.
1 change: 0 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ extend: "eslint:recommended"
rules:
indent: [2, 4, {SwitchCase: 1}]
quotes: [2, 'single']
dot-notation: [2, {allowKeywords: false}]
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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'
}
]
});
Expand Down
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 = [];
Expand All @@ -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();
Expand All @@ -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;
}

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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');
}

Expand Down
48 changes: 48 additions & 0 deletions lib/process_options.js
Original file line number Diff line number Diff line change
@@ -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;
};

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
104 changes: 104 additions & 0 deletions test/command_line_options.js
Original file line number Diff line number Diff line change
@@ -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({});
});
});

0 comments on commit 74a7018

Please sign in to comment.