From 433225d012077013f93b0770e7303e72e4bd7eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Tue, 1 Mar 2022 14:31:56 +0800 Subject: [PATCH 1/2] Improve the error prompt and output the error file name --- src/Chunk.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Chunk.ts b/src/Chunk.ts index 42a0ee0c7a9..5a492295ea2 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -709,10 +709,12 @@ export default class Chunk { ); let usesTopLevelAwait = false; + let usesTopLevelAwaitId: string; const accessedGlobals = new Set(); for (const module of this.orderedModules) { if (module.usesTopLevelAwait) { usesTopLevelAwait = true; + usesTopLevelAwaitId = module.id; } const accessedGlobalVariables = this.accessedGlobalsByScope.get(module.scope); if (accessedGlobalVariables) { @@ -725,7 +727,8 @@ export default class Chunk { if (usesTopLevelAwait && format !== 'es' && format !== 'system') { return error({ code: 'INVALID_TLA_FORMAT', - message: `Module format ${format} does not support top-level await. Use the "es" or "system" output formats rather.` + message: `Module format ${format} does not support top-level await. Use the "es" or "system" output formats rather.`, + id: usesTopLevelAwaitId! }); } From 1aa6f33b5768f93abb6fd129bb4b165eb8d23d10 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Wed, 2 Mar 2022 07:26:11 +0100 Subject: [PATCH 2/2] Fix test --- src/Chunk.ts | 14 ++++++-------- .../samples/invalid-top-level-await/_config.js | 5 ++++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Chunk.ts b/src/Chunk.ts index 5a492295ea2..90e53d1c43d 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -708,13 +708,11 @@ export default class Chunk { dep => (dep.reexports && dep.reexports.length !== 0)! ); - let usesTopLevelAwait = false; - let usesTopLevelAwaitId: string; + let topLevelAwaitModule: string | null = null; const accessedGlobals = new Set(); for (const module of this.orderedModules) { if (module.usesTopLevelAwait) { - usesTopLevelAwait = true; - usesTopLevelAwaitId = module.id; + topLevelAwaitModule = module.id; } const accessedGlobalVariables = this.accessedGlobalsByScope.get(module.scope); if (accessedGlobalVariables) { @@ -724,11 +722,11 @@ export default class Chunk { } } - if (usesTopLevelAwait && format !== 'es' && format !== 'system') { + if (topLevelAwaitModule !== null && format !== 'es' && format !== 'system') { return error({ code: 'INVALID_TLA_FORMAT', - message: `Module format ${format} does not support top-level await. Use the "es" or "system" output formats rather.`, - id: usesTopLevelAwaitId! + id: topLevelAwaitModule, + message: `Module format ${format} does not support top-level await. Use the "es" or "system" output formats rather.` }); } @@ -754,7 +752,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') } };