Skip to content

Commit

Permalink
Tree-shaking the namespace if it is passed to function
Browse files Browse the repository at this point in the history
  • Loading branch information
TrickyPi committed May 8, 2024
1 parent e3966ab commit 90b1383
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/ast/nodes/MemberExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ export default class MemberExpression
}
}
this.object.includePath(path, context, includeChildrenRecursively);
this.property.includePath(path, context, includeChildrenRecursively);
this.property.includePath(EMPTY_PATH, context, includeChildrenRecursively);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ast/variables/LocalVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export default class LocalVariable extends Variable {

includePath(path?: ObjectPath): void {
if (!this.included) {
super.includePath();
super.includePath(path);
for (const declaration of this.declarations) {
// If node is a default export, it can save a tree-shaking run to include the full declaration now
if (!declaration.included)
Expand Down
9 changes: 8 additions & 1 deletion src/ast/variables/ParameterVariable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AstContext } from '../../Module';
import { EMPTY_ARRAY } from '../../utils/blank';
import type { DeoptimizableEntity } from '../DeoptimizableEntity';
import type { HasEffectsContext } from '../ExecutionContext';
import { createInclusionContext, type HasEffectsContext } from '../ExecutionContext';
import type { NodeInteraction } from '../NodeInteractions';
import { INTERACTION_ASSIGNED, INTERACTION_CALLED } from '../NodeInteractions';
import type ExportDefaultDeclaration from '../nodes/ExportDefaultDeclaration';
Expand Down Expand Up @@ -180,6 +180,13 @@ export default class ParameterVariable extends LocalVariable {
return knownValue.hasEffectsOnInteractionAtPath(path, interaction, context);
}

includePath(path?: ObjectPath): void {
super.includePath(path);
if (this.knownValue && path) {
this.knownValue.includePath(path, createInclusionContext(), false);
}
}

deoptimizeArgumentsOnInteractionAtPath(interaction: NodeInteraction, path: ObjectPath): void {
// For performance reasons, we fully deoptimize all deeper interactions
if (
Expand Down
4 changes: 2 additions & 2 deletions src/ast/variables/Variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ export default class Variable extends ExpressionEntity {
* previously.
* Once a variable is included, it should take care all its declarations are included.
*/
includePath(_path?: ObjectPath): void {
includePath(path?: ObjectPath): void {
this.included = true;
this.renderedLikeHoisted?.includePath();
this.renderedLikeHoisted?.includePath(path);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module.exports = defineTest({
description:
'Passing the namespace to a function will disable the tree-shaking of namespace (Ideally, it should not disable the tree-shaking of namespace)'
description: 'Passing the namespace to a function does not disable the tree-shaking of namespace'
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
define(['exports'], (function (exports) { 'use strict';

const foo = () => {};
const bar = () => {};

exports.bar = bar;
exports.foo = foo;

}));
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const foo = () => {};
const bar = () => {};

exports.bar = bar;
exports.foo = foo;
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const foo = () => {};
const bar = () => {};

export { bar, foo };
export { foo };
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ System.register([], (function (exports) {
execute: (function () {

const foo = exports("foo", () => {});
const bar = exports("bar", () => {});

})
};
Expand Down

0 comments on commit 90b1383

Please sign in to comment.