reimplement module aliasing with esbuild plugin#6482
Conversation
🦋 Changeset detectedLatest commit: d6a2a3e The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
A wrangler prerelease is available for testing. You can install this latest build in your project with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-wrangler-6482You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/6482/npm-package-wrangler-6482Or you can use npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-wrangler-6482 dev path/to/script.jsAdditional artifacts:npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-create-cloudflare-6482 --no-auto-updatenpm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-cloudflare-kv-asset-handler-6482npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-miniflare-6482npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-cloudflare-pages-shared-6482npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-cloudflare-vitest-pool-workers-6482npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-cloudflare-workers-editor-shared-6482npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-cloudflare-workers-shared-6482Note that these links will no longer work once the GitHub Actions artifact expires.
Please ensure constraints are pinned, and |
cannot use ":" in filenames on windows
| // resolve with node resolution | ||
| path: require.resolve(aliasPath, { | ||
| // From the esbuild alias docs: "Note that when an import path is substituted using an alias, the resulting import path is resolved in the working directory instead of in the directory containing the source file with the import path." | ||
| // https://esbuild.github.io/api/#alias:~:text=Note%20that%20when%20an%20import%20path%20is%20substituted%20using%20an%20alias%2C%20the%20resulting%20import%20path%20is%20resolved%20in%20the%20working%20directory%20instead%20of%20in%20the%20directory%20containing%20the%20source%20file%20with%20the%20import%20path. |
There was a problem hiding this comment.
nit: perhaps just link to https://esbuild.github.io/api/#alias? We already have the relevant text in the comment above this one
| ...(loader || {}), | ||
| }, | ||
| plugins: [ | ||
| aliasPlugin, |
There was a problem hiding this comment.
what's the order in which these plugins are executed? Is first to last in the plugins array, or the other way around? I can't find that mentioned in the esbuild docs
There was a problem hiding this comment.
First to last. I confirmed by playing with my repro linked in the issue.
There was a problem hiding this comment.
in that case, what happens if aliasPlugin and the unenv one are trying to alias the same module? In that case, won't unenv override the user alias? which is not what we want, unless I'm misunderstanding smth
There was a problem hiding this comment.
Good question. esbuild runs the onResolve hooks on each plugin in order until one returns a path. It only runs the next plugin's onResolve hook if it receives undefined from the current plugin.
There was a problem hiding this comment.
TIL! thx for clarifying that 🙏
There was a problem hiding this comment.
not sure if we want to capture this use case in a test, I'll leave that up to you
|
Thank you @RamIdeas! |
What this PR solves / how to test
Fixes #6448
Author has addressed the following