Skip to content

Commit

Permalink
Includes all dependencies from modules with no-treeshake
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jun 10, 2021
1 parent 76dedd6 commit d1bb3c9
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/Module.ts
Expand Up @@ -202,6 +202,7 @@ export default class Module {
implicitlyLoadedBefore = new Set<Module>();
importDescriptions: { [name: string]: ImportDescription } = Object.create(null);
importMetas: MetaProperty[] = [];
importedFromNotTreeshaken = false;
importers: string[] = [];
imports = new Set<Variable>();
includedDynamicImporters: Module[] = [];
Expand Down Expand Up @@ -365,16 +366,15 @@ export default class Module {
}
if (!this.options.treeshake || this.info.hasModuleSideEffects === 'no-treeshake') {
for (const dependency of this.dependencies) {
if (dependency instanceof ExternalModule || dependency.isIncluded()) {
relevantDependencies.add(dependency);
}
relevantDependencies.add(dependency);
}
} else {
this.addRelevantSideEffectDependencies(
relevantDependencies,
necessaryDependencies,
alwaysCheckedDependencies
);
}
this.addRelevantSideEffectDependencies(
relevantDependencies,
necessaryDependencies,
alwaysCheckedDependencies
);
for (const dependency of necessaryDependencies) {
relevantDependencies.add(dependency);
}
Expand Down Expand Up @@ -622,7 +622,7 @@ export default class Module {
}

isIncluded(): boolean {
return this.ast!.included || this.namespace.included;
return this.ast!.included || this.namespace.included || this.importedFromNotTreeshaken;
}

linkImports(): void {
Expand Down
7 changes: 7 additions & 0 deletions src/ModuleLoader.ts
Expand Up @@ -389,6 +389,13 @@ export class ModuleLoader {
module.dependencies.add(dependency);
dependency.importers.push(module.id);
}
if (!this.options.treeshake || module.info.hasModuleSideEffects === 'no-treeshake') {
for (const dependency of module.dependencies) {
if (dependency instanceof Module) {
dependency.importedFromNotTreeshaken = true;
}
}
}
}

private getNormalizedResolvedIdWithoutDefaults(
Expand Down
13 changes: 13 additions & 0 deletions test/chunking-form/samples/no-treeshake-imports/_config.js
@@ -0,0 +1,13 @@
module.exports = {
description: 'includes all imports when setting moduleSideEffects to "no-treeshake"',
options: {
input: ['main1', 'main2'],
plugins: {
transform(code, id) {
if (id.includes('main')) {
return { moduleSideEffects: 'no-treeshake' };
}
}
}
}
};
@@ -0,0 +1,5 @@
define(function () { 'use strict';



});
@@ -0,0 +1,6 @@
define(['./generated-empty'], function (empty) { 'use strict';

console.log('main1');
const unused = 1;

});
@@ -0,0 +1,6 @@
define(['./generated-empty'], function (empty) { 'use strict';

console.log('main2');
const unused = 2;

});
@@ -0,0 +1,2 @@
'use strict';

@@ -0,0 +1,6 @@
'use strict';

require('./generated-empty.js');

console.log('main1');
const unused = 1;
@@ -0,0 +1,6 @@
'use strict';

require('./generated-empty.js');

console.log('main2');
const unused = 2;
@@ -0,0 +1 @@

@@ -0,0 +1,4 @@
import './generated-empty.js';

console.log('main1');
const unused = 1;
@@ -0,0 +1,4 @@
import './generated-empty.js';

console.log('main2');
const unused = 2;
@@ -0,0 +1,10 @@
System.register([], function () {
'use strict';
return {
execute: function () {



}
};
});
@@ -0,0 +1,12 @@
System.register(['./generated-empty.js'], function () {
'use strict';
return {
setters: [function () {}],
execute: function () {

console.log('main1');
const unused = 1;

}
};
});
@@ -0,0 +1,12 @@
System.register(['./generated-empty.js'], function () {
'use strict';
return {
setters: [function () {}],
execute: function () {

console.log('main2');
const unused = 2;

}
};
});
Empty file.
4 changes: 4 additions & 0 deletions test/chunking-form/samples/no-treeshake-imports/main1.js
@@ -0,0 +1,4 @@
import './empty';

console.log('main1');
const unused = 1;
4 changes: 4 additions & 0 deletions test/chunking-form/samples/no-treeshake-imports/main2.js
@@ -0,0 +1,4 @@
import './empty';

console.log('main2');
const unused = 2;

0 comments on commit d1bb3c9

Please sign in to comment.