Skip to content

Commit

Permalink
Minify JSON (#471)
Browse files Browse the repository at this point in the history
* uglify-json

* throw on error

* Clean up JSONAsset

Don’t extend from JSAsset
  • Loading branch information
Jasper De Moor authored and devongovett committed Jan 6, 2018
1 parent a852dd2 commit c858843
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/assets/JSONAsset.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
const JSAsset = require('./JSAsset');
const Asset = require('../Asset');
const {minify} = require('uglify-es');

class JSONAsset extends JSAsset {
async load() {
return 'module.exports = ' + (await super.load()) + ';';
class JSONAsset extends Asset {
constructor(name, pkg, options) {
super(name, pkg, options);
this.type = 'js';
}

parse() {}
collectDependencies() {}
pretransform() {}
transform() {}
generate() {
let code = `module.exports = ${this.contents};`;

if (this.options.minify) {
let minified = minify(code);
if (minified.error) {
throw minified.error;
}

code = minified.code;
}

return {
js: code
};
}
}

module.exports = JSONAsset;
3 changes: 3 additions & 0 deletions test/integration/uglify-json/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test": "test"
}
9 changes: 9 additions & 0 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,13 @@ describe('javascript', function() {
assert.equal(typeof output.test, 'function');
assert.equal(output.test(), 'pkg-main-module');
});

it('should minify JSON files', async function() {
await bundle(__dirname + '/integration/uglify-json/index.json', {
production: true
});

let json = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
assert(json.includes('test:"test"'));
});
});

0 comments on commit c858843

Please sign in to comment.