Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flattened format #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module.exports = {
- `ignorePaths` is an array of paths to ignore. Path could be a string or regexp.
- `extensionsRegex` is a regexp for assets you always want to include. Example: `/\.(jpe?g|png|gif|svg)$/i`
- `format` allows you to pick the manifest output file format.
- Currently supports `general` (default), `rails` or passing in a function.
- Currently supports `general` (default), `rails`, `flattened` or passing in a function.

If you want to use a custom function it could look like this:

Expand Down
32 changes: 32 additions & 0 deletions format.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,38 @@ Format.prototype.normalizeChunks = function () {
return output;
};

function parseChunkValue (name, value) {
var ext = value.split('.').slice(-1)[0];
return name + '.' + ext;
};

/**
* Assets resulting in more than a single chunk will yield an array. This function creates
* one entry for each of those in the resulting manifest.

* @returns {object}
*/
Format.prototype.flattened = function () {
var output = {};
output.assets = this.assets;

for (var chunkName in this.data.assetsByChunkName) {
var chunkValue = this.data.assetsByChunkName[chunkName];
if (typeof chunkValue === 'string') {
output.assets[parseChunkValue(chunkName, chunkValue)] = chunkValue;
} else if (Array.isArray(chunkValue)) {
chunkValue = chunkValue.filter(function(item) {
return item.indexOf('hot-update.js') === -1;
});
chunkValue.forEach(function (val) {
output.assets[parseChunkValue(chunkName, val)] = val;
});
}
}
output.publicPath = this.data.publicPath;
return output;
};

/**
* At the end of the day the chunks are assets so combine them into the assets.

Expand Down
22 changes: 22 additions & 0 deletions tests/formatTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,27 @@ function test_rails() {
'images/spinner.37348967baeae34bfa408c1f16794db1.gif')
}

function test_flattened() {
var data = format.flattened();

assert.equal(data.publicPath, 'http://localhost:2992/assets/');

assert.equal(data.assets['app_js.js'],
'app_js.5018c3226e10bf313701.js');
assert.equal(data.assets['hot_app_js.js'],
'app_js.5018c3226e10bf313701.js');
assert.equal(data.assets['app_css.js'],
'app_css.291431bdd7415f9ff51d.js');
assert.equal(data.assets['app_css.css'],
'app_css.291431bdd7415f9ff51d.css');
assert.equal(data.assets['app_css.js'],
'app_css.291431bdd7415f9ff51d.js');
assert.equal(data.assets['images/spinner.gif'],
'images/spinner.37348967baeae34bfa408c1f16794db1.gif')
assert.notEqual(data.assets['images/credit-cards/visa.png'],
'visa.26bcf191ee12e711aa540ba8d0c901b7.png')
}

test_general();
test_rails();
test_flattened();