-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
IIFE paren style affects tree shaking #1101
Comments
I just learned something about
If the following change is made to rollup then all IIFE types are successfully tree shaken away: --- a/src/Module.js
+++ b/src/Module.js
@@ -18,7 +18,7 @@ function tryParse ( code, comments, acornOptions, id ) {
ecmaVersion: 7,
sourceType: 'module',
onComment: ( block, text, start, end ) => comments.push({ block, text, start, end }),
- preserveParens: true
+ preserveParens: false
}, acornOptions ));
} catch ( err ) {
err.code = 'PARSE_ERROR'; However 3 export tests fail with this change so it's not a solution:
So some tweaking of ParenthesizedExpression node handling in rollup is needed. |
This is the fix to allow classic IIFEs to be tree shaken away. No regressions. --- a/src/ast/nodes/CallExpression.js
+++ b/src/ast/nodes/CallExpression.js
@@ -5,6 +5,8 @@ import callHasEffects from './shared/callHasEffects.js';
export default class CallExpression extends Node {
bind ( scope ) {
+ while ( this.callee.type === 'ParenthesizedExpression' ) this.callee = this.callee.expression;
+
if ( this.callee.type === 'Identifier' ) {
const declaration = scope.findDeclaration( this.callee.name );
|
There are problems with other ParenthesizedExpressions in rollup. This code: export default class AssignmentExpression extends Node {
bind ( scope ) {
let subject = this.left;
while ( this.left.type === 'ParenthesizedExpression' ) subject = subject.expression;
this.subject = subject; errors out with (((x))) = 4; Likewise UpdateExpression has a similar bug: export default class UpdateExpression extends Node {
bind ( scope ) {
let subject = this.argument;
while ( this.argument.type === 'ParenthesizedExpression' ) subject = subject.expression;
this.subject = subject; for the input: ((x))++; The while loop conditions |
I was mistaken on that point. Here's what I think is the fix for AssignmentExpression and UpdateExpression: --- a/src/ast/nodes/AssignmentExpression.js
+++ b/src/ast/nodes/AssignmentExpression.js
@@ -6,7 +6,7 @@ import { NUMBER, STRING } from '../values.js';
export default class AssignmentExpression extends Node {
bind ( scope ) {
let subject = this.left;
- while ( this.left.type === 'ParenthesizedExpression' ) subject = subject.expression;
+ while ( subject.type === 'ParenthesizedExpression' ) subject = subject.expression;
this.subject = subject;
disallowIllegalReassignment( scope, subject ); --- a/src/ast/nodes/UpdateExpression.js
+++ b/src/ast/nodes/UpdateExpression.js
@@ -6,7 +6,7 @@ import { NUMBER } from '../values.js';
export default class UpdateExpression extends Node {
bind ( scope ) {
let subject = this.argument;
- while ( this.argument.type === 'ParenthesizedExpression' ) subject = subject.expression;
+ while ( subject.type === 'ParenthesizedExpression' ) subject = subject.expression;
this.subject = subject;
disallowIllegalReassignment( scope, this.argument ); |
@Rich-Harris since ParenthesizedExpression AST nodes are generally problematic, might it be better for rollup either remove them all at a common lower level except in the case of |
Removing ParenthesizedExpression AST nodes and replacing them with a |
Fix in #1138 |
Differing paren styles of IIFEs will affect whether code is tree shaken away.
Both CrockfordIIFE and UglifyIIFE were tree shaken away, but ClassicIIFE was not:
This is odd because the
acorn
ASTs of ClassicIIFE and CrockfordIIFE are basically identical except for the start and end of the CallExpression:The text was updated successfully, but these errors were encountered: