Skip to content

Commit

Permalink
.uglifyrc config support (#287)
Browse files Browse the repository at this point in the history
* run prettier

* add uglify config
  • Loading branch information
Jasper De Moor authored and devongovett committed Dec 20, 2017
1 parent 7fcae06 commit 1947a15
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/transforms/uglify.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
const {minify} = require('uglify-es');
const config = require('../utils/config');

async function getConfig(asset) {
return config.load(asset.name, ['.uglifyrc']);
}

module.exports = async function(asset) {
await asset.parseIfNeeded();

// Convert AST into JS
let code = asset.generate().js;

let customConfig = await getConfig(asset);
let options = {
warnings: true,
mangle: {
Expand All @@ -16,6 +22,10 @@ module.exports = async function(asset) {
}
};

if (customConfig) {
options = Object.assign(options, customConfig);
}

let result = minify(code, options);

if (result.error) throw result.error;
Expand Down
5 changes: 5 additions & 0 deletions test/integration/uglify-config/.uglifyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compress": {
"drop_console": false
}
}
4 changes: 4 additions & 0 deletions test/integration/uglify-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function () {
console.log('Hello world');
return 1 + 1;
};
13 changes: 13 additions & 0 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ describe('javascript', function() {
assert(!js.includes('local.a'));
});

it('should use uglify config', async function() {
let b = await bundle(__dirname + '/integration/uglify-config/index.js', {
production: true
});

let output = run(b);
assert.equal(typeof output, 'function');
assert.equal(output(), 2);

let js = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
assert(js.includes('console.log'));
});

it('should insert global variables when needed', async function() {
let b = await bundle(__dirname + '/integration/globals/index.js');

Expand Down

0 comments on commit 1947a15

Please sign in to comment.