diff --git a/lib/results/add-bundle-results-to-file.js b/lib/results/add-bundle-results-to-file.js index e95415ee..06cff27b 100644 --- a/lib/results/add-bundle-results-to-file.js +++ b/lib/results/add-bundle-results-to-file.js @@ -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, @@ -16,4 +18,4 @@ module.exports = function(key, type, resultOptions, env, bundleAllEnvironments) this.push(file); cb(); }); -}; \ No newline at end of file +}; diff --git a/lib/results/index.js b/lib/results/index.js index d86f881b..40873741 100644 --- a/lib/results/index.js +++ b/lib/results/index.js @@ -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(); } @@ -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)); }); @@ -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); diff --git a/lib/stream-bundles.js b/lib/stream-bundles.js index d1b23cb1..20213b34 100644 --- a/lib/stream-bundles.js +++ b/lib/stream-bundles.js @@ -38,6 +38,7 @@ function _bundle(config, env) { type: type, bundleName: bundleName, bundleOptions: namedBundleObj[BundleKeys.OPTIONS], + bundleOrder: Object.keys(bundles), isBundleAll: isBundleAll, minSrcs: minSrcs })); @@ -98,4 +99,4 @@ function bundle(config) { return streams; } -module.exports = bundle; \ No newline at end of file +module.exports = bundle; diff --git a/lib/stream-files.js b/lib/stream-files.js index 64b104f3..d1587fb8 100644 --- a/lib/stream-files.js +++ b/lib/stream-files.js @@ -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) { @@ -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)); -}; \ No newline at end of file + .pipe(addBundleResultsToFile(opts.bundleName, BundleKeys.STYLES, opts.bundleOptions.result, opts.env, opts.isBundleAll, opts.bundleOrder)); +}; diff --git a/test/integ/index-test.js b/test/integ/index-test.js index b182eede..f4fa1531 100644 --- a/test/integ/index-test.js +++ b/test/integ/index-test.js @@ -217,6 +217,11 @@ describe('integration tests', function () { return done(err); } + if (!(data instanceof String || typeof data === 'string')) { // TODO: This won't be needed once Node 0.10, 0.12 are no longer supported + data = String(data); + } + 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": "" diff --git a/test/unit/results/results-test.js b/test/unit/results/results-test.js index f6b43d7a..0650d243 100644 --- a/test/unit/results/results-test.js +++ b/test/unit/results/results-test.js @@ -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({ @@ -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": "", @@ -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": "", @@ -453,10 +458,13 @@ describe('results', function () { } }); } else { + data.indexOf('other').should.be.lessThan(data.indexOf('main')); (JSON.parse(data)).should.eql({ - "main": { - "scripts": "", + "other": { "styles": "" + }, + "main": { + "scripts": "" } }); } @@ -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({ @@ -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'] } }); }); diff --git a/test/unit/stream-bundles-test.js b/test/unit/stream-bundles-test.js index 87270319..a7fe1754 100644 --- a/test/unit/stream-bundles-test.js +++ b/test/unit/stream-bundles-test.js @@ -595,7 +595,8 @@ describe('stream-bundles', function () { name: 'main', type: BundleType.SCRIPTS, result: { - type: 'html' + type: 'html', + bundleOrder: ['main', 'other'] }, bundleAllEnvironments: true, env: '' @@ -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' @@ -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' @@ -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' @@ -647,7 +651,8 @@ describe('stream-bundles', function () { name: 'other', type: BundleType.SCRIPTS, result: { - type: 'html' + type: 'html', + bundleOrder: ['main', 'other'] }, bundleAllEnvironments: true, env: '' @@ -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' @@ -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' @@ -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'