Skip to content

Commit

Permalink
Provide "isEntry" information already in the load hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 13, 2019
1 parent d73d100 commit 2625f5c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 44 deletions.
5 changes: 3 additions & 2 deletions src/Module.ts
Expand Up @@ -188,7 +188,7 @@ export default class Module {
importDescriptions: { [name: string]: ImportDescription } = Object.create(null);
importMetas: MetaProperty[] = [];
imports = new Set<Variable>();
isEntryPoint = false;
isEntryPoint: boolean;
isExecuted = false;
isExternal: false;
isUserDefinedEntryPoint = false;
Expand All @@ -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() {
Expand Down
19 changes: 13 additions & 6 deletions src/ModuleLoader.ts
Expand Up @@ -275,14 +275,21 @@ export class ModuleLoader {
).then(() => fetchDynamicImportsPromise);
}

private fetchModule(id: string, importer: string, moduleSideEffects: boolean): Promise<Module> {
private fetchModule(
id: string,
importer: string,
moduleSideEffects: boolean,
isEntry: boolean
): Promise<Module> {
const existingModule = this.modulesById.get(id);
if (existingModule) {
if (existingModule.isExternal) throw new Error(`Cannot fetch external module ${id}`);
return Promise.resolve(<Module>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') {
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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));
Expand Down
1 change: 0 additions & 1 deletion src/utils/executionOrder.ts
Expand Up @@ -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);
Expand Down
77 changes: 42 additions & 35 deletions test/function/samples/plugin-module-information/_config.js
Expand Up @@ -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);
Expand Down

0 comments on commit 2625f5c

Please sign in to comment.