From 2625f5c91fdb97decd5a38e60410bded2e95d3cf Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 13 May 2019 07:12:44 +0200 Subject: [PATCH] Provide "isEntry" information already in the load hook. --- src/Module.ts | 5 +- src/ModuleLoader.ts | 19 +++-- src/utils/executionOrder.ts | 1 - .../plugin-module-information/_config.js | 77 ++++++++++--------- 4 files changed, 58 insertions(+), 44 deletions(-) diff --git a/src/Module.ts b/src/Module.ts index 87137446bcb..afd720b00dc 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -188,7 +188,7 @@ export default class Module { importDescriptions: { [name: string]: ImportDescription } = Object.create(null); importMetas: MetaProperty[] = []; imports = new Set(); - isEntryPoint = false; + isEntryPoint: boolean; isExecuted = false; isExternal: false; isUserDefinedEntryPoint = false; @@ -213,12 +213,13 @@ export default class Module { private namespaceVariable: NamespaceVariable = undefined; private transformDependencies: string[]; - constructor(graph: Graph, id: string, moduleSideEffects: boolean) { + constructor(graph: Graph, id: string, moduleSideEffects: boolean, isEntry: boolean) { this.id = id; this.graph = graph; this.excludeFromSourcemap = /\0/.test(id); this.context = graph.getModuleContext(id); this.moduleSideEffects = moduleSideEffects; + this.isEntryPoint = isEntry; } basename() { diff --git a/src/ModuleLoader.ts b/src/ModuleLoader.ts index ea300ccc28b..63e0b10ecce 100644 --- a/src/ModuleLoader.ts +++ b/src/ModuleLoader.ts @@ -275,14 +275,21 @@ export class ModuleLoader { ).then(() => fetchDynamicImportsPromise); } - private fetchModule(id: string, importer: string, moduleSideEffects: boolean): Promise { + private fetchModule( + id: string, + importer: string, + moduleSideEffects: boolean, + isEntry: boolean + ): Promise { const existingModule = this.modulesById.get(id); if (existingModule) { - if (existingModule.isExternal) throw new Error(`Cannot fetch external module ${id}`); - return Promise.resolve(existingModule); + if (existingModule instanceof ExternalModule) + throw new Error(`Cannot fetch external module ${id}`); + existingModule.isEntryPoint = existingModule.isEntryPoint || isEntry; + return Promise.resolve(existingModule); } - const module: Module = new Module(this.graph, id, moduleSideEffects); + const module: Module = new Module(this.graph, id, moduleSideEffects, isEntry); this.modulesById.set(id, module); const manualChunkAlias = this.getManualChunk(id); if (typeof manualChunkAlias === 'string') { @@ -374,7 +381,7 @@ export class ModuleLoader { } return Promise.resolve(externalModule); } else { - return this.fetchModule(resolvedId.id, importer, resolvedId.moduleSideEffects); + return this.fetchModule(resolvedId.id, importer, resolvedId.moduleSideEffects, false); } } @@ -409,7 +416,7 @@ export class ModuleLoader { : resolveIdResult; if (typeof id === 'string') { - return this.fetchModule(id, undefined, true).then(module => { + return this.fetchModule(id, undefined, true, true).then(module => { if (alias !== null) { if (module.chunkAlias !== null && module.chunkAlias !== alias) { error(errCannotAssignModuleToChunk(module.id, alias, module.chunkAlias)); diff --git a/src/utils/executionOrder.ts b/src/utils/executionOrder.ts index c09f259472f..fb02212b2ae 100644 --- a/src/utils/executionOrder.ts +++ b/src/utils/executionOrder.ts @@ -53,7 +53,6 @@ export function analyseModuleExecution(entryModules: Module[]) { }; for (const curEntry of entryModules) { - curEntry.isEntryPoint = true; if (!parents[curEntry.id]) { parents[curEntry.id] = null; analyseModule(curEntry); diff --git a/test/function/samples/plugin-module-information/_config.js b/test/function/samples/plugin-module-information/_config.js index d0d85651590..6a9a20b4734 100644 --- a/test/function/samples/plugin-module-information/_config.js +++ b/test/function/samples/plugin-module-information/_config.js @@ -12,42 +12,49 @@ module.exports = { description: 'provides module information on the plugin context', options: { external: ['path'], - plugins: [ - { - renderStart() { - rendered = true; - assert.deepEqual(Array.from(this.moduleIds), [ID_MAIN, ID_FOO, ID_NESTED, ID_PATH]); - assert.deepEqual(this.getModuleInfo(ID_MAIN), { - hasModuleSideEffects: true, - id: ID_MAIN, - importedIds: [ID_FOO, ID_NESTED], - isEntry: true, - isExternal: false - }); - assert.deepEqual(this.getModuleInfo(ID_FOO), { - hasModuleSideEffects: true, - id: ID_FOO, - importedIds: [ID_PATH], - isEntry: false, - isExternal: false - }); - assert.deepEqual(this.getModuleInfo(ID_NESTED), { - hasModuleSideEffects: true, - id: ID_NESTED, - importedIds: [ID_FOO], - isEntry: false, - isExternal: false - }); - assert.deepEqual(this.getModuleInfo(ID_PATH), { - hasModuleSideEffects: true, - id: ID_PATH, - importedIds: [], - isEntry: false, - isExternal: true - }); - } + plugins: { + load(id) { + assert.deepStrictEqual(this.getModuleInfo(id), { + hasModuleSideEffects: true, + id, + importedIds: [], + isEntry: id === ID_MAIN, + isExternal: false + }); + }, + renderStart() { + rendered = true; + assert.deepStrictEqual(Array.from(this.moduleIds), [ID_MAIN, ID_FOO, ID_NESTED, ID_PATH]); + assert.deepStrictEqual(this.getModuleInfo(ID_MAIN), { + hasModuleSideEffects: true, + id: ID_MAIN, + importedIds: [ID_FOO, ID_NESTED], + isEntry: true, + isExternal: false + }); + assert.deepStrictEqual(this.getModuleInfo(ID_FOO), { + hasModuleSideEffects: true, + id: ID_FOO, + importedIds: [ID_PATH], + isEntry: false, + isExternal: false + }); + assert.deepStrictEqual(this.getModuleInfo(ID_NESTED), { + hasModuleSideEffects: true, + id: ID_NESTED, + importedIds: [ID_FOO], + isEntry: false, + isExternal: false + }); + assert.deepStrictEqual(this.getModuleInfo(ID_PATH), { + hasModuleSideEffects: true, + id: ID_PATH, + importedIds: [], + isEntry: false, + isExternal: true + }); } - ] + } }, bundle() { assert.ok(rendered);