Skip to content

reimplement module aliasing with esbuild plugin#6482

Merged
RamIdeas merged 5 commits intomainfrom
alias-precedence
Aug 13, 2024
Merged

reimplement module aliasing with esbuild plugin#6482
RamIdeas merged 5 commits intomainfrom
alias-precedence

Conversation

@RamIdeas
Copy link
Copy Markdown
Contributor

@RamIdeas RamIdeas commented Aug 13, 2024

What this PR solves / how to test

Fixes #6448

Author has addressed the following

  • Tests
    • TODO (before merge)
    • Included
    • Not necessary because:
  • E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • I don't know
    • Required / Maybe required
    • Not required because: tested by unit test of BundleController
  • Changeset (Changeset guidelines)
    • TODO (before merge)
    • Included
    • Not necessary because:
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Not necessary because: no public facing change

@RamIdeas RamIdeas requested a review from a team as a code owner August 13, 2024 13:45
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Aug 13, 2024

🦋 Changeset detected

Latest commit: d6a2a3e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
wrangler Patch
@cloudflare/vitest-pool-workers Patch

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Aug 13, 2024

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-6482

You 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-6482

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-wrangler-6482 dev path/to/script.js
Additional artifacts:
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-create-cloudflare-6482 --no-auto-update
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-cloudflare-kv-asset-handler-6482
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-miniflare-6482
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-cloudflare-pages-shared-6482
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-cloudflare-vitest-pool-workers-6482
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-cloudflare-workers-editor-shared-6482
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10373037841/npm-package-cloudflare-workers-shared-6482

Note that these links will no longer work once the GitHub Actions artifact expires.


wrangler@3.70.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20240806.0
workerd 1.20240806.0 1.20240806.0
workerd --version 1.20240806.0 2024-08-06

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First to last. I confirmed by playing with my repro linked in the issue.

Copy link
Copy Markdown
Contributor

@CarmenPopoviciu CarmenPopoviciu Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

@RamIdeas RamIdeas Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL! thx for clarifying that 🙏

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if we want to capture this use case in a test, I'll leave that up to you

@RamIdeas RamIdeas merged commit e24939c into main Aug 13, 2024
@RamIdeas RamIdeas deleted the alias-precedence branch August 13, 2024 20:25
@IgorMinar
Copy link
Copy Markdown
Contributor

Thank you @RamIdeas!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

user-defined aliases should take precedence over unenv polyfills

4 participants