Skip to content

Commit

Permalink
Make sure external re-exports are included for moduleSideEffects: fal…
Browse files Browse the repository at this point in the history
…se (#2905)

* Always include external reexports even when moduleSideEffects are false

* Treat implicit external modules the same way as explicit ones for module
side-effects
  • Loading branch information
lukastaegert committed Jun 7, 2019
1 parent c68bd95 commit 0a0b67f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 9 deletions.
11 changes: 5 additions & 6 deletions src/Module.ts
Expand Up @@ -456,7 +456,6 @@ export default class Module {

for (const exportName of this.getExports()) {
const variable = this.getVariableForExportName(exportName);

variable.deoptimizePath(UNKNOWN_PATH);
if (!variable.included) {
variable.include();
Expand All @@ -466,14 +465,14 @@ export default class Module {

for (const name of this.getReexports()) {
const variable = this.getVariableForExportName(name);

if (variable instanceof ExternalVariable) {
variable.reexported = variable.module.reexported = true;
} else if (!variable.included) {
variable.deoptimizePath(UNKNOWN_PATH);
if (!variable.included) {
variable.include();
variable.deoptimizePath(UNKNOWN_PATH);
this.graph.needsTreeshakingPass = true;
}
if (variable instanceof ExternalVariable) {
variable.module.reexported = true;
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/ModuleLoader.ts
Expand Up @@ -398,7 +398,11 @@ export class ModuleLoader {
error(errUnresolvedImport(source, importer));
}
this.graph.warn(errUnresolvedImportTreatedAsExternal(source, importer));
return { id: source, external: true, moduleSideEffects: true };
return {
external: true,
id: source,
moduleSideEffects: this.hasModuleSideEffects(source, true)
};
}
return resolvedId;
}
Expand Down
1 change: 0 additions & 1 deletion src/ast/variables/Variable.ts
Expand Up @@ -18,7 +18,6 @@ export default class Variable implements ExpressionEntity {
isReassigned = false;
module?: Module | ExternalModule;
name: string;
reexported = false;
renderBaseName: string | null = null;
renderName: string | null = null;
safeExportName: string | null = null;
Expand Down
Expand Up @@ -25,6 +25,9 @@ module.exports = {
if (id === 'internal') {
return id;
}
if (id === 'implicit-external') {
return null;
}
const moduleSideEffects = JSON.parse(id.split('-')[1]);
if (moduleSideEffects) {
return { id, moduleSideEffects, external: true };
Expand All @@ -38,5 +41,15 @@ module.exports = {
}
}
}
}
},
warnings: [
{
code: 'UNRESOLVED_IMPORT',
importer: 'main.js',
message:
"'implicit-external' is imported by main.js, but could not be resolved – treating it as an external dependency",
source: 'implicit-external',
url: 'https://rollupjs.org/guide/en#warning-treating-module-as-external-dependency'
}
]
};
@@ -1,3 +1,4 @@
import 'pluginsideeffects-false';
import 'pluginsideeffects-true';
import 'internal';
import 'implicit-external';
@@ -0,0 +1,24 @@
const assert = require('assert');

module.exports = {
description: 'handles reexporting external values when module side-effects are false',
context: {
require(id) {
return { value: id };
}
},
exports(exports) {
assert.deepStrictEqual(exports, {
external: 'external',
value: 'externalStar'
});
},
options: {
external() {
return true;
},
treeshake: {
moduleSideEffects: false
}
}
};
@@ -0,0 +1,2 @@
export { value as external } from 'external';
export * from 'externalStar';

0 comments on commit 0a0b67f

Please sign in to comment.