Skip to content

Commit

Permalink
Merge pull request #86 from PlasmaPower/master
Browse files Browse the repository at this point in the history
Keep bundle order in bundle.result.json
  • Loading branch information
chmontgomery committed May 23, 2016
2 parents ee109bf + c02e21c commit d9b2700
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 35 deletions.
5 changes: 4 additions & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ Flag to disable all console logging.
Note: beyond this api, bundle results can be further modified with config options like
[custom result types](../examples/custom-result)

The order of the bundles in the results file will be the same as the order in which they were
specified in the config.

### options

Type: `Object` or `String`
Expand Down Expand Up @@ -124,4 +127,4 @@ gulp.task('bundle', function () {
});
```

Would result in a file named `manifest.json` created at the project root.
Would result in a file named `manifest.json` created at the project root.
6 changes: 3 additions & 3 deletions examples/custom-url-paths/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"other/custom/path/my-bundle-name": {
"styles": "<link href='/assets/other/custom/path/my-bundle-name-b9726e20e7.css' media='all' rel='stylesheet' type='text/css'/>"
},
"custom/path/structure/bundle-name": {
"scripts": "<script src='/assets/custom/path/structure/bundle-name-ca7a60f866.js' type='text/javascript'></script>"
},
"other/custom/path/my-bundle-name": {
"styles": "<link href='/assets/other/custom/path/my-bundle-name-b9726e20e7.css' media='all' rel='stylesheet' type='text/css'/>"
}
}
12 changes: 6 additions & 6 deletions examples/full/manifest.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
{
"jquery-stand-alone": {
"scripts": "<script src='/public/jquery-stand-alone-29f6b03537.js' type='text/javascript'></script>"
"header": {
"scripts": "<script src='/public/header-c380f8735f.js' type='text/javascript'></script>",
"styles": "<link href='/public/header-450a885fe9.css' media='all' rel='stylesheet' type='text/css'/>"
},
"vendor": {
"styles": "<link href='/public/vendor-752c24d067.css' media='all' rel='stylesheet' type='text/css'/>",
"scripts": "<script src='/public/vendor-b9c14db406.js' type='text/javascript'></script>"
},
"header": {
"styles": "<link href='/public/header-450a885fe9.css' media='all' rel='stylesheet' type='text/css'/>",
"scripts": "<script src='/public/header-c380f8735f.js' type='text/javascript'></script>"
},
"article": {
"styles": "<link href='/public/article-eb84c68ed8.css' media='all' rel='stylesheet' type='text/css'/>",
"scripts": "<script src='/public/article-19f4f97809.js' type='text/javascript'></script>"
Expand All @@ -18,6 +15,9 @@
"scripts": "<script src='/public/main-a2f0720d62.js' type='text/javascript'></script>",
"styles": "<link href='/public/main-56c9e7f7a2.css' media='all' rel='stylesheet' type='text/css'/>"
},
"jquery-stand-alone": {
"scripts": "<script src='/public/jquery-stand-alone-29f6b03537.js' type='text/javascript'></script>"
},
"ordered-bundle": {
"styles": "<link href='/public/ordered-bundle-d8d01cbb86.css' media='all' rel='stylesheet' type='text/css'/>",
"scripts": "<script src='/public/ordered-bundle-cbd3c3edb4.js' type='text/javascript'></script>"
Expand Down
6 changes: 3 additions & 3 deletions examples/result-json/bundle.result.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"customJs": {
"scripts": "<script src='/public/customJs-33c43745c3.js' type='text/javascript'></script>"
},
"main": {
"styles": "<link href='/public/main-0cd4ab1aa8.css' media='all' rel='stylesheet' type='text/css'/>",
"scripts": "<script src='/public/main-2742a3c06d.js' type='text/javascript'></script>"
},
"vendor": {
"styles": "<link href='/public/vendor-7c38ff6718.css' media='all' rel='stylesheet' type='text/css'/>",
"scripts": "<script src='/public/vendor-6873f46ec1.js' type='text/javascript'></script>"
},
"customJs": {
"scripts": "<script src='/public/customJs-33c43745c3.js' type='text/javascript'></script>"
}
}
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 readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ Which results in a `bundle.result.json` file similar to:
}
```

The order of the bundles will be the same as the order in which they were specified in the config.

[See here for a full example using hogan](examples/express-app-using-result-json)

## Other Features
Expand Down
5 changes: 5 additions & 0 deletions test/integ/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<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 d9b2700

Please sign in to comment.