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

Includes all dependencies from modules with no-treeshake #4138

Merged
merged 1 commit into from Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;