Skip to content
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: add wasm file to exports #186

Merged
merged 1 commit into from
Jan 16, 2023
Merged

feat: add wasm file to exports #186

merged 1 commit into from
Jan 16, 2023

Conversation

hadeeb
Copy link
Contributor

@hadeeb hadeeb commented Jan 4, 2023

require.resolve and bundlers like vite are unable to resolve the wasm file.

Cheers

@vercel
Copy link

vercel bot commented Jan 4, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
resvg-js ✅ Ready (Inspect) Visit Preview Jan 4, 2023 at 0:34AM (UTC)

@yisibl
Copy link
Member

yisibl commented Jan 4, 2023

Thanks!

Does vite have documentation for this?

@hadeeb
Copy link
Contributor Author

hadeeb commented Jan 5, 2023

It's not specific to vite.

// if using "type":"module"
// import { createRequire } from "module";
// const require = createRequire(import.meta.url);

try {
  // This works
  const indexFile = require.resolve("@resvg/resvg-wasm");
  console.log({ indexFile });
  // This does not work
  const wasmFile = require.resolve("@resvg/resvg-wasm/index_bg.wasm");
  console.log({ wasmFile });
} catch (error) {
  console.error({ error });
}

this code works after adding the wasm file to exports.

From nodejs.org

Existing packages introducing the "exports" field will prevent consumers of the package from using any entry points that are not defined, including the package.json (e.g. require('your-package/package.json'). This will likely be a breaking change.

@yisibl
Copy link
Member

yisibl commented Jan 5, 2023

Can you add a test case to __test__/wasm.spec.ts?

@hadeeb
Copy link
Contributor Author

hadeeb commented Jan 16, 2023

It turns out that the exports field is used only when the target file is in node_modules (I might be wrong here).
I could not replicate the same behaviour while importing from "../wasm".

From nodejs.org

The "exports" field allows defining the entry points of a package when imported by name loaded either via a node_modules lookup or a self-reference to its own name.

@yisibl
Copy link
Member

yisibl commented Jan 16, 2023

Thank you very much, let's merge it first.

@yisibl yisibl merged commit 740a991 into thx:main Jan 16, 2023
@jeetiss
Copy link

jeetiss commented Feb 2, 2023

@yisibl I got the same error with webpack! Could you please release this patch?

Module not found: Package path ./index_bg.wasm is not exported from package
/playground/node_modules/@resvg/resvg-wasm (see exports field in
/playground/node_modules/@resvg/resvg-wasm/package.json)

@yisibl
Copy link
Member

yisibl commented Feb 2, 2023

@jeetiss Done https://github.com/yisibl/resvg-js/releases/tag/v2.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants