Skip to content

Commit

Permalink
Expose syntheticNamedExports to ModuleInfo (#3847)
Browse files Browse the repository at this point in the history
* Expose syntheticNamedExports in module info, added docs

* Updated tests, changed info after transform

* Use getter

* Added test for default synthetic export

* Moved syntheticNamedExports to ModuleInfo entirely

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
Amareis and lukastaegert committed Nov 2, 2020
1 parent b41e532 commit 9540051
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 41 deletions.
2 changes: 2 additions & 0 deletions docs/05-plugin-development.md
Expand Up @@ -656,6 +656,8 @@ Returns additional information about the module in question in the form
implicitlyLoadedAfterOneOf: string[], // implicit relationships, declared via this.emitChunk
implicitlyLoadedBefore: string[], // implicit relationships, declared via this.emitChunk
hasModuleSideEffects: boolean | "no-treeshake" // are imports of this module included if nothing is imported from it
meta: {[plugin: string]: any} // custom module meta-data
syntheticNamedExports: boolean | string // final value of synthetic named exports
}
```
Expand Down
4 changes: 2 additions & 2 deletions src/Chunk.ts
Expand Up @@ -241,7 +241,7 @@ export default class Chunk {
if (!chunkModules.has(importer)) {
this.dynamicEntryModules.push(module);
// Modules with synthetic exports need an artificial namespace for dynamic imports
if (module.syntheticNamedExports && !outputOptions.preserveModules) {
if (module.info.syntheticNamedExports && !outputOptions.preserveModules) {
includedNamespaces.add(module);
this.exports.add(module.namespace);
}
Expand Down Expand Up @@ -379,7 +379,7 @@ export default class Chunk {
}
}
for (const module of this.dynamicEntryModules) {
if (module.syntheticNamedExports) continue;
if (module.info.syntheticNamedExports) continue;
if (!this.facadeModule && this.canModuleBeFacade(module, exposedVariables)) {
this.facadeModule = module;
this.facadeChunkByModule.set(module, this);
Expand Down
3 changes: 2 additions & 1 deletion src/ExternalModule.ts
Expand Up @@ -58,7 +58,8 @@ export default class ExternalModule {
},
isEntry: false,
isExternal: true,
meta
meta,
syntheticNamedExports: false
};
}

Expand Down
30 changes: 17 additions & 13 deletions src/Module.ts
Expand Up @@ -246,7 +246,7 @@ export default class Module {
private readonly options: NormalizedInputOptions,
isEntry: boolean,
hasModuleSideEffects: boolean | 'no-treeshake',
public syntheticNamedExports: boolean | string,
syntheticNamedExports: boolean | string,
meta: CustomPluginOptions
) {
this.excludeFromSourcemap = /\0/.test(id);
Expand Down Expand Up @@ -284,7 +284,8 @@ export default class Module {
},
isEntry,
isExternal: false,
meta
meta,
syntheticNamedExports
};
}

Expand Down Expand Up @@ -392,7 +393,7 @@ export default class Module {
}
const exportNamesByVariable: Map<Variable, string[]> = new Map();
for (const exportName of this.getAllExportNames()) {
if (exportName === this.syntheticNamedExports) continue;
if (exportName === this.info.syntheticNamedExports) continue;
let tracedVariable = this.getVariableForExportName(exportName);
if (tracedVariable instanceof ExportDefaultVariable) {
tracedVariable = tracedVariable.getOriginalVariable();
Expand Down Expand Up @@ -455,7 +456,9 @@ export default class Module {
if (this.syntheticNamespace === null) {
this.syntheticNamespace = undefined;
this.syntheticNamespace = this.getVariableForExportName(
typeof this.syntheticNamedExports === 'string' ? this.syntheticNamedExports : 'default'
typeof this.info.syntheticNamedExports === 'string'
? this.info.syntheticNamedExports
: 'default'
);
}
if (!this.syntheticNamespace) {
Expand All @@ -465,10 +468,11 @@ export default class Module {
message: `Module "${relativeId(
this.id
)}" that is marked with 'syntheticNamedExports: ${JSON.stringify(
this.syntheticNamedExports
this.info.syntheticNamedExports
)}' needs ${
typeof this.syntheticNamedExports === 'string' && this.syntheticNamedExports !== 'default'
? `an export named "${this.syntheticNamedExports}"`
typeof this.info.syntheticNamedExports === 'string' &&
this.info.syntheticNamedExports !== 'default'
? `an export named "${this.info.syntheticNamedExports}"`
: 'a default export'
}.`
});
Expand Down Expand Up @@ -538,7 +542,7 @@ export default class Module {
// we don't want to create shims when we are just
// probing export * modules for exports
if (!isExportAllSearch) {
if (this.syntheticNamedExports) {
if (this.info.syntheticNamedExports) {
let syntheticExport = this.syntheticExports.get(name);
if (!syntheticExport) {
const syntheticNamespace = this.getSyntheticNamespace();
Expand Down Expand Up @@ -580,7 +584,7 @@ export default class Module {
}

for (const exportName of this.getExports()) {
if (includeNamespaceMembers || exportName !== this.syntheticNamedExports) {
if (includeNamespaceMembers || exportName !== this.info.syntheticNamedExports) {
const variable = this.getVariableForExportName(exportName);
variable.deoptimizePath(UNKNOWN_PATH);
if (!variable.included) {
Expand Down Expand Up @@ -736,7 +740,7 @@ export default class Module {
};

this.scope = new ModuleScope(this.graph.scope, this.astContext);
this.namespace = new NamespaceVariable(this.astContext, this.syntheticNamedExports);
this.namespace = new NamespaceVariable(this.astContext, this.info.syntheticNamedExports);
this.ast = new Program(ast, { type: 'Module', context: this.astContext }, this.scope);
this.info.ast = ast;

Expand All @@ -757,7 +761,7 @@ export default class Module {
originalSourcemap: this.originalSourcemap,
resolvedIds: this.resolvedIds,
sourcemapChain: this.sourcemapChain,
syntheticNamedExports: this.syntheticNamedExports,
syntheticNamedExports: this.info.syntheticNamedExports,
transformDependencies: this.transformDependencies,
transformFiles: this.transformFiles
};
Expand Down Expand Up @@ -803,7 +807,7 @@ export default class Module {
this.info.hasModuleSideEffects = moduleSideEffects;
}
if (syntheticNamedExports != null) {
this.syntheticNamedExports = syntheticNamedExports;
this.info.syntheticNamedExports = syntheticNamedExports;
}
if (meta != null) {
this.info.meta = { ...this.info.meta, ...meta };
Expand Down Expand Up @@ -964,7 +968,7 @@ export default class Module {
externalVariable.include();
this.imports.add(externalVariable);
mergedNamespaces.push(externalVariable);
} else if (module.syntheticNamedExports) {
} else if (module.info.syntheticNamedExports) {
const syntheticNamespace = module.getSyntheticNamespace();
syntheticNamespace.include();
this.imports.add(syntheticNamespace);
Expand Down
2 changes: 1 addition & 1 deletion src/ast/variables/NamespaceVariable.ts
Expand Up @@ -46,7 +46,7 @@ export default class NamespaceVariable extends Variable {
}
const memberVariables = Object.create(null);
for (const name of this.context.getExports().concat(this.context.getReexports())) {
if (name[0] !== '*' && name !== this.module.syntheticNamedExports) {
if (name[0] !== '*' && name !== this.module.info.syntheticNamedExports) {
memberVariables[name] = this.context.traceExport(name);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/rollup/types.d.ts
Expand Up @@ -171,6 +171,7 @@ interface ModuleInfo {
isEntry: boolean;
isExternal: boolean;
meta: CustomPluginOptions;
syntheticNamedExports: boolean | string;
}

export type GetModuleInfo = (moduleId: string) => ModuleInfo | null;
Expand Down
Expand Up @@ -80,7 +80,8 @@ module.exports = {
importers: [],
isEntry: true,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
});
assert.deepStrictEqual(JSON.parse(JSON.stringify(this.getModuleInfo(ID_DEP))), {
ast: {
Expand Down Expand Up @@ -138,7 +139,8 @@ module.exports = {
importers: [],
isEntry: true,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
});
},
generateBundle(options, bundle) {
Expand Down
Expand Up @@ -76,7 +76,8 @@ module.exports = {
importers: [],
isEntry: true,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
});
assert.deepStrictEqual(JSON.parse(JSON.stringify(this.getModuleInfo(ID_DEP))), {
ast: {
Expand Down Expand Up @@ -134,7 +135,8 @@ module.exports = {
importers: [],
isEntry: true,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
});
},
generateBundle(options, bundle) {
Expand Down
Expand Up @@ -125,7 +125,8 @@ module.exports = {
importers: [],
isEntry: true,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
});
assert.deepStrictEqual(JSON.parse(JSON.stringify(this.getModuleInfo(ID_MAIN2))), {
ast: {
Expand Down Expand Up @@ -219,7 +220,8 @@ module.exports = {
importers: [],
isEntry: true,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
});
assert.deepStrictEqual(JSON.parse(JSON.stringify(this.getModuleInfo(ID_DEP))), {
ast: {
Expand Down Expand Up @@ -312,7 +314,8 @@ module.exports = {
importers: [],
isEntry: false,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
});
}
}
Expand Down
Expand Up @@ -75,7 +75,8 @@ module.exports = {
importers: [],
isEntry: true,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
});
assert.deepStrictEqual(JSON.parse(JSON.stringify(this.getModuleInfo(ID_DEP))), {
ast: {
Expand Down Expand Up @@ -133,7 +134,8 @@ module.exports = {
importers: [],
isEntry: false,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
});
},
generateBundle(options, bundle) {
Expand Down
12 changes: 8 additions & 4 deletions test/function/samples/deprecated/manual-chunks-info/_config.js
Expand Up @@ -111,7 +111,8 @@ module.exports = {
importers: [],
isEntry: true,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
},
{
ast: null,
Expand All @@ -126,7 +127,8 @@ module.exports = {
importers: [getId('main')],
isEntry: false,
isExternal: true,
meta: {}
meta: {},
syntheticNamedExports: false
},
{
ast: {
Expand Down Expand Up @@ -154,7 +156,8 @@ module.exports = {
importers: [getId('dynamic'), getId('main')],
isEntry: false,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
},
{
ast: {
Expand Down Expand Up @@ -226,7 +229,8 @@ module.exports = {
importers: [],
isEntry: false,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
}
]
);
Expand Down
12 changes: 8 additions & 4 deletions test/function/samples/manual-chunks-info/_config.js
Expand Up @@ -110,7 +110,8 @@ module.exports = {
importers: [],
isEntry: true,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
},
{
ast: null,
Expand All @@ -125,7 +126,8 @@ module.exports = {
importers: [getId('main')],
isEntry: false,
isExternal: true,
meta: {}
meta: {},
syntheticNamedExports: false
},
{
ast: {
Expand Down Expand Up @@ -153,7 +155,8 @@ module.exports = {
importers: [getId('dynamic'), getId('main')],
isEntry: false,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
},
{
ast: {
Expand Down Expand Up @@ -225,7 +228,8 @@ module.exports = {
importers: [],
isEntry: false,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
}
]
);
Expand Down
6 changes: 4 additions & 2 deletions test/function/samples/module-parsed-hook/_config.js
Expand Up @@ -58,7 +58,8 @@ module.exports = {
importers: [],
isEntry: true,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
},
{
ast: {
Expand Down Expand Up @@ -102,7 +103,8 @@ module.exports = {
importers: [ID_MAIN],
isEntry: false,
isExternal: false,
meta: {}
meta: {},
syntheticNamedExports: false
}
]);
}
Expand Down

0 comments on commit 9540051

Please sign in to comment.