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

Change to uglify es & output uglify errors #157

Merged
merged 15 commits into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from 12 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
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"babel-core": "^6.25.0",
"babel-generator": "^6.25.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"babel-to-estree": "^0.0.2",
"babylon": "^6.17.4",
"babylon-walk": "^1.0.2",
"browser-resolve": "^1.11.2",
Expand All @@ -33,7 +32,7 @@
"posthtml": "^0.9.2",
"resolve": "^1.4.0",
"serve-static": "^1.12.4",
"uglify-js": "^3.0.28",
"uglify-es": "^3.2.1",
"v8-compile-cache": "^1.1.0",
"worker-farm": "^1.4.1",
"ws": "^3.1.0"
Expand Down
39 changes: 29 additions & 10 deletions src/transforms/uglify.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
const {AST_Node, minify} = require('uglify-js');
const {toEstree} = require('babel-to-estree');
const types = require('babel-types');
const walk = require('babylon-walk');
const {minify} = require('uglify-es');
const generate = require('babel-generator').default;

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

// Convert to UglifyJS AST
var ast = AST_Node.from_mozilla_ast(toEstree(asset.ast, asset.contents));
var result = minify(ast, {
toplevel: true
});
// Convert AST into JS
let code = asset.isAstDirty
? generate(asset.ast).code
: asset.outputCode || asset.contents;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just call asset.generate().js here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I extracted this bit from generate because i saw TODO: Source maps and thought this would affect performance in the future. I'll change it back to asset.generate though :)


// Uglify did our code generation for us, so remove the old AST
let options = {
warnings: true,
mangle: {
toplevel: true
},
compress: {
drop_console: true
}
};

let result = minify(code, options);

if (result.error) throw result.error;

// Log all warnings
if (result.warnings) {
result.warnings.forEach(warning => {
// TODO: warn this using the logger
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would probably be better to use the logger to send warnings to the console, not really sure how to address the logger from here though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm yeah, the logger isn't really accessible from here, it lives only in the main process right now, not workers.

console.log(warning);
});
}

// babel-generator did our code generation for us, so remove the old AST
asset.ast = null;
asset.outputCode = result.code;
asset.isAstDirty = false;
Expand Down
10 changes: 6 additions & 4 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ describe('javascript', function() {
assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'bundle-loader.js', 'bundle-url.js'],
childBundles: [{
assets: ['local.js'],
childBundles: []
}]
childBundles: [
{
assets: ['local.js'],
childBundles: []
}
]
});

let output = run(b).default;
Expand Down
28 changes: 10 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -673,15 +673,7 @@ babel-template@^6.16.0, babel-template@^6.24.1, babel-template@^6.26.0:
babylon "^6.18.0"
lodash "^4.17.4"

babel-to-estree@^0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/babel-to-estree/-/babel-to-estree-0.0.2.tgz#505bc2bda6d962a9fbaf58e8aa06c6aed73497d0"
dependencies:
babel-traverse "^6.23.1"
babel-types "^6.23.0"
babylon "^6.16.1"

babel-traverse@^6.18.0, babel-traverse@^6.23.1, babel-traverse@^6.24.1, babel-traverse@^6.26.0:
babel-traverse@^6.18.0, babel-traverse@^6.24.1, babel-traverse@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.26.0.tgz#46a9cbd7edcc62c8e5c064e2d2d8d0f4035766ee"
dependencies:
Expand All @@ -695,7 +687,7 @@ babel-traverse@^6.18.0, babel-traverse@^6.23.1, babel-traverse@^6.24.1, babel-tr
invariant "^2.2.2"
lodash "^4.17.4"

babel-types@^6.15.0, babel-types@^6.18.0, babel-types@^6.19.0, babel-types@^6.23.0, babel-types@^6.24.1, babel-types@^6.26.0:
babel-types@^6.15.0, babel-types@^6.18.0, babel-types@^6.19.0, babel-types@^6.24.1, babel-types@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.26.0.tgz#a3b073f94ab49eb6fa55cd65227a334380632497"
dependencies:
Expand All @@ -712,7 +704,7 @@ babylon-walk@^1.0.2:
babel-types "^6.15.0"
lodash.clone "^4.5.0"

babylon@^6.16.1, babylon@^6.17.4, babylon@^6.18.0:
babylon@^6.17.4, babylon@^6.18.0:
version "6.18.0"
resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.18.0.tgz#af2f3b88fa6f5c1e4c634d1a0f8eac4f55b395e3"

Expand Down Expand Up @@ -4883,6 +4875,13 @@ typescript@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.6.2.tgz#3c5b6fd7f6de0914269027f03c0946758f7673a4"

uglify-es@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/uglify-es/-/uglify-es-3.2.1.tgz#93de0aad8a1bb629c8a316f686351bc4d6ece687"
dependencies:
commander "~2.12.1"
source-map "~0.6.1"

uglify-js@^2.6, uglify-js@^2.8.29:
version "2.8.29"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.8.29.tgz#29c5733148057bb4e1f75df35b7a9cb72e6a59dd"
Expand All @@ -4892,13 +4891,6 @@ uglify-js@^2.6, uglify-js@^2.8.29:
optionalDependencies:
uglify-to-browserify "~1.0.0"

uglify-js@^3.0.28:
version "3.2.1"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.2.1.tgz#d6427fd45a25fefc5d196689c0c772a6915e10fe"
dependencies:
commander "~2.12.1"
source-map "~0.6.1"

uglify-to-browserify@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/uglify-to-browserify/-/uglify-to-browserify-1.0.2.tgz#6e0924d6bda6b5afe349e39a6d632850a0f882b7"
Expand Down