-
Notifications
You must be signed in to change notification settings - Fork 769
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add support for node-like module resolution (#4748)
* feat: add support for node-like module resolution * chore: add changeset * Apply suggestions from code review Co-authored-by: Pete Bacon Darwin <pete@bacondarwin.com> * chore: changeset typo * Update .changeset/sour-plums-pull.md --------- Co-authored-by: Pete Bacon Darwin <pete@bacondarwin.com>
- Loading branch information
1 parent
ccf14dd
commit 3603a60
Showing
10 changed files
with
90 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
fix: resolve imports in a more node-like fashion for packages that do not declare exports | ||
|
||
Previously, trying to import a file that wasn't explicitly exported from a module would result in an error, but now, better attempts are made to resolve the import using node's module resolution algorithm. It's now possible to do things like this: | ||
|
||
```js | ||
import JPEG_DEC_WASM from "@jsquash/jpeg/codec/dec/mozjpeg_dec.wasm"; | ||
``` | ||
|
||
This works even if the `mozjpeg_dec.wasm` file isn't explicitly exported from the `@jsquash/jpeg` module. | ||
|
||
Fixes #4726 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# import-wasm-example | ||
|
||
`import-wasm-example` is a test fixture that imports a `wasm` file from `import-wasm-static`, testing npm module resolution with wrangler imports. | ||
It also imports a file that isn't technically exported from `import-wasm-static`, testing more traditional node module resolution. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
// this is from the `import-wasm-static` fixture defined above | ||
// and setup inside package.json to mimic an npm package | ||
import multiply from "import-wasm-static/multiply.wasm"; | ||
import otherMultiple from "import-wasm-static/wasm/not-exported.wasm"; | ||
|
||
export default { | ||
async fetch(request) { | ||
// just instantiate and return something | ||
// we're really just testing the import at the top of this file | ||
// we're really just testing the imports at the top of this file | ||
const multiplyModule = await WebAssembly.instantiate(multiply); | ||
return new Response(`${multiplyModule.exports.multiply(7, 3)}`); | ||
const otherModule = await WebAssembly.instantiate(otherMultiple); | ||
|
||
const results = [ | ||
multiplyModule.exports.multiply(7, 3), | ||
otherModule.exports.multiply(7, 3), | ||
]; | ||
return new Response(results.join(", ")); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# import-wasm-static | ||
|
||
`import-wasm-static` is a fixture that simply exports a `wasm` file via `package.json` exports to be used and imported in other fixtures, to test npm module resolution. | ||
|
||
It also provides a `not-exported.wasm` file that is not exported via `package.json` exports, to test node module resolution. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
(module | ||
(func $multiply (param $p1 i32) (param $p2 i32) (result i32) | ||
local.get $p1 | ||
local.get $p2 | ||
i32.mul) | ||
(export "multiply" (func $multiply)) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.