diff --git a/src/transforms/uglify.js b/src/transforms/uglify.js index bbd15dfaa5d..4aa0716163f 100644 --- a/src/transforms/uglify.js +++ b/src/transforms/uglify.js @@ -1,4 +1,9 @@ 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(); @@ -6,6 +11,7 @@ module.exports = async function(asset) { // Convert AST into JS let code = asset.generate().js; + let customConfig = await getConfig(asset); let options = { warnings: true, mangle: { @@ -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; diff --git a/test/integration/uglify-config/.uglifyrc b/test/integration/uglify-config/.uglifyrc new file mode 100644 index 00000000000..c04d0dc21f9 --- /dev/null +++ b/test/integration/uglify-config/.uglifyrc @@ -0,0 +1,5 @@ +{ + "compress": { + "drop_console": false + } +} \ No newline at end of file diff --git a/test/integration/uglify-config/index.js b/test/integration/uglify-config/index.js new file mode 100644 index 00000000000..68b69440c27 --- /dev/null +++ b/test/integration/uglify-config/index.js @@ -0,0 +1,4 @@ +module.exports = function () { + console.log('Hello world'); + return 1 + 1; +}; \ No newline at end of file diff --git a/test/javascript.js b/test/javascript.js index 2e50378c9bd..11f20bf4dd8 100644 --- a/test/javascript.js +++ b/test/javascript.js @@ -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');