Skip to content

Commit

Permalink
Do not count accessing members of an external namespace as side-effec…
Browse files Browse the repository at this point in the history
…ts. (#4001)
  • Loading branch information
lukastaegert committed Mar 18, 2021
1 parent 9c19865 commit 5dfc489
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/ast/variables/ExternalVariable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ExternalModule from '../../ExternalModule';
import Identifier from '../nodes/Identifier';
import { ObjectPath } from '../utils/PathTracker';
import Variable from './Variable';

export default class ExternalVariable extends Variable {
Expand All @@ -21,6 +22,10 @@ export default class ExternalVariable extends Variable {
}
}

hasEffectsWhenAccessedAtPath(path: ObjectPath) {
return path.length > (this.isNamespace ? 1 : 0);
}

include() {
if (!this.included) {
this.included = true;
Expand Down
4 changes: 4 additions & 0 deletions test/form/samples/treeshake-namespace-access/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
description: 'does not count namespace property access as side-effect',
options: { external: 'external' }
};
5 changes: 5 additions & 0 deletions test/form/samples/treeshake-namespace-access/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
define(['external'], function (external) { 'use strict';

console.log('main');

});
5 changes: 5 additions & 0 deletions test/form/samples/treeshake-namespace-access/_expected/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

require('external');

console.log('main');
3 changes: 3 additions & 0 deletions test/form/samples/treeshake-namespace-access/_expected/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'external';

console.log('main');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function () {
'use strict';

console.log('main');

}());
11 changes: 11 additions & 0 deletions test/form/samples/treeshake-namespace-access/_expected/system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
System.register(['external'], function () {
'use strict';
return {
setters: [function () {}],
execute: function () {

console.log('main');

}
};
});
8 changes: 8 additions & 0 deletions test/form/samples/treeshake-namespace-access/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(['external'], factory) :
factory();
}((function () { 'use strict';

console.log('main');

})));
4 changes: 4 additions & 0 deletions test/form/samples/treeshake-namespace-access/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as external from 'external';

const unused = external.foo;
console.log('main');

0 comments on commit 5dfc489

Please sign in to comment.