-
Notifications
You must be signed in to change notification settings - Fork 690
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
feat: support npm resolution for file imports #4135
Conversation
🦋 Changeset detectedLatest commit: 9977e9c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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/6627685921/npm-package-wrangler-4135 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/6627685921/npm-package-wrangler-4135 Or you can use npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/6627685921/npm-package-wrangler-4135 dev path/to/script.js Additional artifacts:npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/6627685921/npm-package-cloudflare-pages-shared-4135 Note that these links will no longer work once the GitHub Actions artifact expires.
| Please ensure constraints are pinned, and |
Codecov Report
@@ Coverage Diff @@
## main #4135 +/- ##
=======================================
Coverage 75.36% 75.36%
=======================================
Files 223 223
Lines 12259 12280 +21
Branches 3171 3177 +6
=======================================
+ Hits 9239 9255 +16
- Misses 3020 3025 +5
|
4626169
to
d0eb1f6
Compare
Can this be done more simply using the node.js Disclaimer: I haven't checked this at all! For example something like: let filePath = path.join(args.resolveDir, args.path);
if (!path.isAbsolute(args.path) && !args.path.startsWith(".")) {
try {
filePath = require.resolve(args.path)
} catch {
// Do nothing
} Note that your current checks for package like paths would break on Windows. |
Unfortunately not from my testing. Running
Can you expand here? My checks are purely checking for an included If I try and import like
Supporting more windows orientated backslashes seems like a general issue here, and not really in the scope of this PR if I'm understanding correctly. It's also extremely uncommon, even on Windows from my experience, to import using backslashes in the path, especially with npm modules. This would also explain why no one has reported it as a bug. |
Yeah that is a good spot. The rules themselves wouldn't match in the first place. |
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.
Could do with another review from a Wrangler developer, though.
All of the E2E test failures look unrelated, failing due to |
Yeah that's because they don't run on collaborator forks only on the Cloudflare owned fork. No worries. |
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.
This is looking great! I would like to see some tests of this behaviour: perhaps similarly to the worker-app
example, which imports from another fixture, you could write a fixture which imports a wasm file from another fixture?
Thanks! I've added two test fixtures now - one just exporting a simple Let me know if there's any further changes needed here. |
The failing tests seem to be in EDIT: merged from upstream - seems this was fixed in a recent commit. EDIT2: seems the |
Updated from |
Nevermind, something in |
Fixes #4098.
What this PR solves / how to test:
Previously when importing static files (such as wasm) from an npm package, you had to path directly to it, such as :
While this works file, the DX isn't ideal, so this PR adds proper package resolution for these statements, allowing you to do:
This uses
resolve.exports
to look at and analyse the package'sexports
field, and resolve to the applicable file. This does add an additional dependency, but this is a very common one used by so much build tooling includingvite
,jest
,svelte
, and more.If for any reason this fails, it falls back to the previous behaviour and will just do a path reference, and then end up throwing an
ENOENT
as per previous functionality.Notes:
/
and not starting with.
. It seems to work remarkebly well in practice across the many projects I've tested this against, but I'm happy to improve this if someone has a cleaner idea.node_modules
folder, and dummy package, which I don't think there's any prior art for in the repo today. Happy to add these if someone can guide me on the best way to approach this.Author has included the following, where applicable:
Reviewer is to perform the following, as applicable:
Note for PR author:
We want to celebrate and highlight awesome PR review! If you think this PR received a particularly high-caliber review, please assign it the label
highlight pr review
so future reviewers can take inspiration and learn from it.