Skip to content

Commit

Permalink
Keep bundle order in bundle.result.json
Browse files Browse the repository at this point in the history
Resolves dowjones#71
  • Loading branch information
PlasmaPower committed May 20, 2016
1 parent ee109bf commit 0f0f10d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 22 deletions.
6 changes: 4 additions & 2 deletions lib/results/add-bundle-results-to-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ var through = require('through2'),
path = require('path'),
Bundle = require('./../model/bundle');

module.exports = function(key, type, resultOptions, env, bundleAllEnvironments) {
module.exports = function(key, type, resultOptions, env, bundleAllEnvironments, bundleOrder) {
return through.obj(function (file, enc, cb) {
if (path.extname(file.path) !== '.map') { // ignore .map files
resultOptions = resultOptions || {};
resultOptions.bundleOrder = bundleOrder;
file.bundle = new Bundle({
name: key,
type: type,
Expand All @@ -16,4 +18,4 @@ module.exports = function(key, type, resultOptions, env, bundleAllEnvironments)
this.push(file);
cb();
});
};
};
22 changes: 20 additions & 2 deletions lib/results/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ function defaulOptions(opts) {
module.exports = {
all: function (opts) {
var resultJsons = {},
resultOrder,
options = defaulOptions(opts);

function collectResults(file, enc, cb) {
addBundleResults(resultJsons, file, options.pathPrefix, options.fileName);
if (!resultOrder) {
try {
resultOrder = file.bundle.result.bundleOrder; // Any one of these properties could be null
} catch (err) {}
}
this.push(file);
cb();
}
Expand All @@ -40,8 +46,12 @@ module.exports = {
var streams = [];

_.each(resultJsons, function (result) {
var sortedContents = {};
_.each(resultOrder || Object.keys(result.contents), function (key) {
sortedContents[key] = result.contents[key];
});
var filePath = path.join(options.dest, result.filename),
data = JSON.stringify(result.contents, null, 2);
data = JSON.stringify(sortedContents, null, 2);
streams.push(fs.writeFileAsync(filePath, data));
});

Expand Down Expand Up @@ -83,7 +93,15 @@ module.exports = {
.then(function (data) {
var newData = resultJsons[envKey].contents;
var mergedData = _.merge(JSON.parse(data), newData);
return fs.writeFileAsync(filePath, JSON.stringify(mergedData, null, 2));
var sortedData = {};
var resultOrder;
try {
resultOrder = file.bundle.result.bundleOrder; // Any one of these properties could be null
} catch (err) {}
_.each(resultOrder || Object.keys(mergedData), function (key) {
sortedData[key] = mergedData[key];
});
return fs.writeFileAsync(filePath, JSON.stringify(sortedData, null, 2));
});
} else {
var freshData = JSON.stringify(resultJsons[envKey].contents, null, 2);
Expand Down
3 changes: 2 additions & 1 deletion lib/stream-bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function _bundle(config, env) {
type: type,
bundleName: bundleName,
bundleOptions: namedBundleObj[BundleKeys.OPTIONS],
bundleOrder: Object.keys(bundles),
isBundleAll: isBundleAll,
minSrcs: minSrcs
}));
Expand Down Expand Up @@ -98,4 +99,4 @@ function bundle(config) {
return streams;
}

module.exports = bundle;
module.exports = bundle;
6 changes: 3 additions & 3 deletions lib/stream-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ module.exports.scripts = function (opts) {
opts.bundleOptions.pluginOptions['gulp-sourcemaps'][BundleKeys.SCRIPTS].write
)
))
.pipe(addBundleResultsToFile(opts.bundleName, BundleKeys.SCRIPTS, opts.bundleOptions.result, opts.env, opts.isBundleAll));
.pipe(addBundleResultsToFile(opts.bundleName, BundleKeys.SCRIPTS, opts.bundleOptions.result, opts.env, opts.isBundleAll, opts.bundleOrder));
};

module.exports.styles = function (opts) {
Expand Down Expand Up @@ -140,5 +140,5 @@ module.exports.styles = function (opts) {
opts.bundleOptions.pluginOptions['gulp-sourcemaps'][BundleKeys.STYLES].write
)
))
.pipe(addBundleResultsToFile(opts.bundleName, BundleKeys.STYLES, opts.bundleOptions.result, opts.env, opts.isBundleAll));
};
.pipe(addBundleResultsToFile(opts.bundleName, BundleKeys.STYLES, opts.bundleOptions.result, opts.env, opts.isBundleAll, opts.bundleOrder));
};
2 changes: 2 additions & 0 deletions test/integ/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ describe('integration tests', function () {
return done(err);
}

data.indexOf('main').should.be.lessThan(data.indexOf('vendor'));
data.indexOf('vendor').should.be.lessThan(data.indexOf('customJs'));
JSON.parse(data).should.eql({
"customJs": {
"scripts": "<script src='/public/customJs-33c43745c3.js' type='text/javascript'></script>"
Expand Down
22 changes: 16 additions & 6 deletions test/unit/results/results-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,10 @@ describe('results', function () {
name: 'main',
type: BundleKeys.SCRIPTS,
env: '',
bundleAllEnvironments: true
bundleAllEnvironments: true,
result: {
bundleOrder: ['vendor', 'main']
}
});

var cssFile = new File({
Expand Down Expand Up @@ -379,6 +382,7 @@ describe('results', function () {
writeFile: function (writePath, data, cb) {
resultFileCount++;
if (path.join(resultPath, 'bundle.result.production.json') === writePath) {
data.indexOf('vendor').should.be.lessThan(data.indexOf('main'));
(JSON.parse(data)).should.eql({
"main": {
"scripts": "<script src='/public/main.production.js' type='text/javascript'></script>",
Expand All @@ -389,6 +393,7 @@ describe('results', function () {
}
});
} else if (path.join(resultPath, 'bundle.result.json') === writePath) {
data.indexOf('vendor').should.be.lessThan(data.indexOf('main'));
(JSON.parse(data)).should.eql({
"main": {
"scripts": "<script src='/public/main.js' type='text/javascript'></script>",
Expand Down Expand Up @@ -453,10 +458,13 @@ describe('results', function () {
}
});
} else {
data.indexOf('other').should.be.lessThan(data.indexOf('main'));
(JSON.parse(data)).should.eql({
"main": {
"scripts": "<script src='main.js' type='text/javascript'></script>",
"other": {
"styles": "<link href='main.css' media='all' rel='stylesheet' type='text/css'/>"
},
"main": {
"scripts": "<script src='main.js' type='text/javascript'></script>"
}
});
}
Expand Down Expand Up @@ -487,7 +495,8 @@ describe('results', function () {
});
jsFile.bundle = new Bundle({
name: 'main',
type: BundleKeys.SCRIPTS
type: BundleKeys.SCRIPTS,
result: { bundleOrder: ['other', 'main'] }
});

cssFile = new File({
Expand All @@ -496,8 +505,9 @@ describe('results', function () {
contents: new Buffer('vendor_bundle_content')
});
cssFile.bundle = new Bundle({
name: 'main',
type: BundleKeys.STYLES
name: 'other',
type: BundleKeys.STYLES,
result: { bundleOrder: ['other', 'main'] }
});

});
Expand Down
24 changes: 16 additions & 8 deletions test/unit/stream-bundles-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ describe('stream-bundles', function () {
name: 'main',
type: BundleType.SCRIPTS,
result: {
type: 'html'
type: 'html',
bundleOrder: ['main', 'other']
},
bundleAllEnvironments: true,
env: ''
Expand All @@ -608,7 +609,8 @@ describe('stream-bundles', function () {
name: 'main',
type: BundleType.SCRIPTS,
result: {
type: 'html'
type: 'html',
bundleOrder: ['main', 'other']
},
bundleAllEnvironments: true,
env: 'production'
Expand All @@ -621,7 +623,8 @@ describe('stream-bundles', function () {
name: 'main',
type: BundleType.SCRIPTS,
result: {
type: 'html'
type: 'html',
bundleOrder: ['main', 'other']
},
bundleAllEnvironments: true,
env: 'staging'
Expand All @@ -634,7 +637,8 @@ describe('stream-bundles', function () {
name: 'main',
type: BundleType.SCRIPTS,
result: {
type: 'html'
type: 'html',
bundleOrder: ['main', 'other']
},
bundleAllEnvironments: true,
env: 'development'
Expand All @@ -647,7 +651,8 @@ describe('stream-bundles', function () {
name: 'other',
type: BundleType.SCRIPTS,
result: {
type: 'html'
type: 'html',
bundleOrder: ['main', 'other']
},
bundleAllEnvironments: true,
env: ''
Expand All @@ -660,7 +665,8 @@ describe('stream-bundles', function () {
name: 'other',
type: BundleType.SCRIPTS,
result: {
type: 'html'
type: 'html',
bundleOrder: ['main', 'other']
},
bundleAllEnvironments: true,
env: 'production'
Expand All @@ -673,7 +679,8 @@ describe('stream-bundles', function () {
name: 'other',
type: BundleType.SCRIPTS,
result: {
type: 'html'
type: 'html',
bundleOrder: ['main', 'other']
},
bundleAllEnvironments: true,
env: 'staging'
Expand All @@ -686,7 +693,8 @@ describe('stream-bundles', function () {
name: 'other',
type: BundleType.SCRIPTS,
result: {
type: 'html'
type: 'html',
bundleOrder: ['main', 'other']
},
bundleAllEnvironments: true,
env: 'development'
Expand Down

0 comments on commit 0f0f10d

Please sign in to comment.