From 7e82a1a5b90f82489ef16c2b2837ba3d0f525cc3 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Sat, 20 Jun 2020 22:13:01 +0200 Subject: [PATCH] Improve coverage --- src/Chunk.ts | 21 ++++++-------- src/utils/chunkAssignment.ts | 28 ++++++------------- .../_config.js | 25 +++++++++++++++++ .../chunk.js | 1 + .../main.js | 1 + .../_config.js | 25 +++++++++++++++++ .../chunk.js | 1 + .../main.js | 1 + 8 files changed, 72 insertions(+), 31 deletions(-) create mode 100644 test/function/samples/emit-file/chunk-filename-not-available-buildEnd/_config.js create mode 100644 test/function/samples/emit-file/chunk-filename-not-available-buildEnd/chunk.js create mode 100644 test/function/samples/emit-file/chunk-filename-not-available-buildEnd/main.js create mode 100644 test/function/samples/emit-file/chunk-filename-not-available-renderStart/_config.js create mode 100644 test/function/samples/emit-file/chunk-filename-not-available-renderStart/chunk.js create mode 100644 test/function/samples/emit-file/chunk-filename-not-available-renderStart/main.js diff --git a/src/Chunk.ts b/src/Chunk.ts index bf621090a40..a2492d8ca97 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -895,8 +895,7 @@ export default class Chunk { } else { const variable = this.exportsByName![exportName]; if (variable instanceof SyntheticNamedExportVariable) continue; - const module = variable.module; - if (!module) continue; + const module = variable.module!; if (module instanceof Module) { exportChunk = this.chunkByModule.get(module)!; if (exportChunk === this) continue; @@ -1146,16 +1145,14 @@ export default class Chunk { if (variable instanceof SyntheticNamedExportVariable) { variable = variable.getBaseVariable(); } - if (variable.module) { - const chunk = this.chunkByModule.get(variable.module as Module); - if (chunk !== this) { - this.imports.add(variable); - if ( - !(variable instanceof NamespaceVariable && this.outputOptions.preserveModules) && - variable.module instanceof Module - ) { - chunk!.exports.add(variable); - } + const chunk = this.chunkByModule.get(variable.module as Module); + if (chunk !== this) { + this.imports.add(variable); + if ( + !(variable instanceof NamespaceVariable && this.outputOptions.preserveModules) && + variable.module instanceof Module + ) { + chunk!.exports.add(variable); } } } diff --git a/src/utils/chunkAssignment.ts b/src/utils/chunkAssignment.ts index 186cc895ddf..e386ebb7ca8 100644 --- a/src/utils/chunkAssignment.ts +++ b/src/utils/chunkAssignment.ts @@ -21,10 +21,7 @@ export function getChunkAssignments( } const assignedEntryPointsByModule: DependentModuleMap = new Map(); - const { dependentEntryPointsByModule, dynamicEntryModules } = analyzeModuleGraph( - entryModules, - modulesInManualChunks - ); + const { dependentEntryPointsByModule, dynamicEntryModules } = analyzeModuleGraph(entryModules); const dynamicallyDependentEntryPointsByDynamicEntry: DependentModuleMap = getDynamicDependentEntryPoints( dependentEntryPointsByModule, dynamicEntryModules @@ -59,16 +56,16 @@ export function getChunkAssignments( function areEntryPointsContainedOrDynamicallyDependent( entryPoints: Set, - superSet: Set + containedIn: Set ): boolean { const entriesToCheck = new Set(entryPoints); for (const entry of entriesToCheck) { - if (!superSet.has(entry)) { + if (!containedIn.has(entry)) { if (staticEntries.has(entry)) return false; - const dynamicDependentEntryPoints = dynamicallyDependentEntryPointsByDynamicEntry.get( + const dynamicallyDependentEntryPoints = dynamicallyDependentEntryPointsByDynamicEntry.get( entry )!; - for (const dependentEntry of dynamicDependentEntryPoints) { + for (const dependentEntry of dynamicallyDependentEntryPoints) { entriesToCheck.add(dependentEntry); } } @@ -115,8 +112,7 @@ function addStaticDependenciesToManualChunk( } function analyzeModuleGraph( - entryModules: Module[], - modulesInManualChunks: Set + entryModules: Module[] ): { dependentEntryPointsByModule: DependentModuleMap; dynamicEntryModules: Set; @@ -134,20 +130,14 @@ function analyzeModuleGraph( } } for (const { resolution } of module.dynamicImports) { - if ( - resolution instanceof Module && - resolution.includedDynamicImporters.length > 0 && - !modulesInManualChunks.has(resolution) - ) { + if (resolution instanceof Module && resolution.includedDynamicImporters.length > 0) { dynamicEntryModules.add(resolution); entriesToHandle.add(resolution); } } for (const dependency of module.implicitlyLoadedBefore) { - if (!modulesInManualChunks.has(dependency)) { - dynamicEntryModules.add(dependency); - entriesToHandle.add(dependency); - } + dynamicEntryModules.add(dependency); + entriesToHandle.add(dependency); } } } diff --git a/test/function/samples/emit-file/chunk-filename-not-available-buildEnd/_config.js b/test/function/samples/emit-file/chunk-filename-not-available-buildEnd/_config.js new file mode 100644 index 00000000000..95688f451a8 --- /dev/null +++ b/test/function/samples/emit-file/chunk-filename-not-available-buildEnd/_config.js @@ -0,0 +1,25 @@ +let chunkId; + +module.exports = { + description: 'Throws when accessing the filename before it has been generated in buildEnd', + options: { + input: 'main.js', + plugins: { + name: 'test-plugin', + buildStart() { + chunkId = this.emitFile({ type: 'chunk', id: 'chunk.js' }); + }, + buildEnd() { + this.getFileName(chunkId); + } + } + }, + error: { + code: 'PLUGIN_ERROR', + hook: 'buildEnd', + message: + 'Plugin error - Unable to get file name for chunk "chunk.js". Ensure that generate is called first.', + plugin: 'test-plugin', + pluginCode: 'CHUNK_NOT_GENERATED' + } +}; diff --git a/test/function/samples/emit-file/chunk-filename-not-available-buildEnd/chunk.js b/test/function/samples/emit-file/chunk-filename-not-available-buildEnd/chunk.js new file mode 100644 index 00000000000..36b1d61dd25 --- /dev/null +++ b/test/function/samples/emit-file/chunk-filename-not-available-buildEnd/chunk.js @@ -0,0 +1 @@ +console.log('chunk'); diff --git a/test/function/samples/emit-file/chunk-filename-not-available-buildEnd/main.js b/test/function/samples/emit-file/chunk-filename-not-available-buildEnd/main.js new file mode 100644 index 00000000000..c0b933d7b56 --- /dev/null +++ b/test/function/samples/emit-file/chunk-filename-not-available-buildEnd/main.js @@ -0,0 +1 @@ +console.log('main'); diff --git a/test/function/samples/emit-file/chunk-filename-not-available-renderStart/_config.js b/test/function/samples/emit-file/chunk-filename-not-available-renderStart/_config.js new file mode 100644 index 00000000000..8a35d9cfc2e --- /dev/null +++ b/test/function/samples/emit-file/chunk-filename-not-available-renderStart/_config.js @@ -0,0 +1,25 @@ +let chunkId; + +module.exports = { + description: 'Throws when accessing the filename before it has been generated in renderStart', + options: { + input: 'main.js', + plugins: { + name: 'test-plugin', + buildStart() { + chunkId = this.emitFile({ type: 'chunk', id: 'chunk.js' }); + }, + renderStart() { + this.getFileName(chunkId); + } + } + }, + generateError: { + code: 'PLUGIN_ERROR', + hook: 'renderStart', + message: + 'Plugin error - Unable to get file name for chunk "chunk.js". Ensure that generate is called first.', + plugin: 'test-plugin', + pluginCode: 'CHUNK_NOT_GENERATED' + } +}; diff --git a/test/function/samples/emit-file/chunk-filename-not-available-renderStart/chunk.js b/test/function/samples/emit-file/chunk-filename-not-available-renderStart/chunk.js new file mode 100644 index 00000000000..36b1d61dd25 --- /dev/null +++ b/test/function/samples/emit-file/chunk-filename-not-available-renderStart/chunk.js @@ -0,0 +1 @@ +console.log('chunk'); diff --git a/test/function/samples/emit-file/chunk-filename-not-available-renderStart/main.js b/test/function/samples/emit-file/chunk-filename-not-available-renderStart/main.js new file mode 100644 index 00000000000..c0b933d7b56 --- /dev/null +++ b/test/function/samples/emit-file/chunk-filename-not-available-renderStart/main.js @@ -0,0 +1 @@ +console.log('main');