diff --git a/src/Chunk.ts b/src/Chunk.ts index 4e243637654..4ef5032193d 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -710,11 +710,11 @@ export default class Chunk { dep => (dep.reexports && dep.reexports.length !== 0)! ); - let usesTopLevelAwait = false; + let topLevelAwaitModule: string | null = null; const accessedGlobals = new Set(); for (const module of this.orderedModules) { if (module.usesTopLevelAwait) { - usesTopLevelAwait = true; + topLevelAwaitModule = module.id; } const accessedGlobalVariables = this.accessedGlobalsByScope.get(module.scope); if (accessedGlobalVariables) { @@ -724,9 +724,10 @@ export default class Chunk { } } - if (usesTopLevelAwait && format !== 'es' && format !== 'system') { + if (topLevelAwaitModule !== null && format !== 'es' && format !== 'system') { return error({ code: 'INVALID_TLA_FORMAT', + id: topLevelAwaitModule, message: `Module format ${format} does not support top-level await. Use the "es" or "system" output formats rather.` }); } @@ -753,7 +754,7 @@ export default class Chunk { namedExportsMode: this.exportMode !== 'default', outro: addons.outro, snippets, - usesTopLevelAwait, + usesTopLevelAwait: topLevelAwaitModule !== null, warn: this.inputOptions.onwarn }, options diff --git a/test/function/samples/invalid-top-level-await/_config.js b/test/function/samples/invalid-top-level-await/_config.js index 57540628b06..f7ed11adada 100644 --- a/test/function/samples/invalid-top-level-await/_config.js +++ b/test/function/samples/invalid-top-level-await/_config.js @@ -1,8 +1,11 @@ +const path = require('path'); + module.exports = { description: 'throws for invalid top-level-await format', generateError: { code: 'INVALID_TLA_FORMAT', message: - 'Module format cjs does not support top-level await. Use the "es" or "system" output formats rather.' + 'Module format cjs does not support top-level await. Use the "es" or "system" output formats rather.', + id: path.join(__dirname, 'main.js') } };