-
Notifications
You must be signed in to change notification settings - Fork 786
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
fix: improve the rendering of build errors when bundling #6894
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
fix: improve the rendering of build errors when bundling |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -468,7 +468,11 @@ export async function bundleWorker( | |
await ctx.watch(); | ||
result = await initialBuildResultPromise; | ||
if (result.errors.length > 0) { | ||
throw new UserError("Failed to build"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We were just swallowing the errors and warnings here and relying upon them being logged "directly" to stdout by another instance of esbuild that is doing the format sniffing. See below. |
||
throw new BuildFailure( | ||
"Initial build failed.", | ||
result.errors, | ||
result.warnings | ||
); | ||
} | ||
|
||
stop = async function () { | ||
|
@@ -536,3 +540,13 @@ export async function bundleWorker( | |
}, | ||
}; | ||
} | ||
|
||
class BuildFailure extends Error { | ||
constructor( | ||
message: string, | ||
readonly errors: esbuild.Message[], | ||
readonly warnings: esbuild.Message[] | ||
) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ export function esbuildAliasExternalPlugin( | |
aliases: Record<string, string> | ||
): Plugin { | ||
return { | ||
name: "external alias imports plugin", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When writing to the logs esbuild already prefixes these names with |
||
name: "external-alias-imports", | ||
setup(build) { | ||
build.onResolve({ filter: /.*/g }, (args) => { | ||
// If it's the entrypoint, let it be as is | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import type { Plugin } from "esbuild"; | |
* An esbuild plugin that will mark any `cloudflare:...` imports as external. | ||
*/ | ||
export const cloudflareInternalPlugin: Plugin = { | ||
name: "Cloudflare internal imports plugin", | ||
name: "cloudflare-internal-imports", | ||
setup(pluginBuild) { | ||
const paths = new Set(); | ||
pluginBuild.onStart(() => paths.clear()); | ||
|
@@ -23,14 +23,19 @@ export const cloudflareInternalPlugin: Plugin = { | |
.map((p) => `"${p}"`) | ||
.sort() | ||
); | ||
throw new Error( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It turns out that throwing an error causes esbuild to think that something bad went wrong in the plugin and dumps the whole stack trace to the plugin out. |
||
dedent` | ||
Unexpected external import of ${pathList}. Imports are not valid in a Service Worker format Worker. | ||
Did you mean to create a Module Worker? | ||
If so, try adding \`export default { ... }\` in your entry-point. | ||
See https://developers.cloudflare.com/workers/reference/migrate-to-module-workers/. | ||
` | ||
); | ||
return { | ||
errors: [ | ||
{ | ||
text: dedent` | ||
Unexpected external import of ${pathList}. | ||
Your worker has no default export, which means it is assumed to be a Service Worker format Worker. | ||
Did you mean to create a ES Module format Worker? | ||
If so, try adding \`export default { ... }\` in your entry-point. | ||
See https://developers.cloudflare.com/workers/reference/migrate-to-module-workers/. | ||
`, | ||
}, | ||
], | ||
}; | ||
} | ||
}); | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this "test hack" any more.