Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the error prompt and output the error file name #4421

Merged
merged 3 commits into from Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Chunk.ts
Expand Up @@ -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<string>();
for (const module of this.orderedModules) {
if (module.usesTopLevelAwait) {
usesTopLevelAwait = true;
topLevelAwaitModule = module.id;
}
const accessedGlobalVariables = this.accessedGlobalsByScope.get(module.scope);
if (accessedGlobalVariables) {
Expand All @@ -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.`
});
}
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion 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')
}
};