Skip to content

feat: support direct ESM imports in wasm - #55

Merged
pi0 merged 8 commits into
unjs:mainfrom
brendandahl:esm-imports
Aug 18, 2025
Merged

pi0 merged 8 commits into
unjs:mainfrom
brendandahl:esm-imports

Conversation

@brendandahl

Copy link
Copy Markdown
Contributor

Directly import from an ES module following the ESM integration proposal.

e.g.

(import "./add-esmi-deps.mjs" "getValue" (func $getValue (result i32)))

This adds wabt as a dependency to build the test example and test case .wat file.

resolves #54

Directly import from an ES module following the ESM integration proposal.

e.g.
`(import "./add-esmi-deps.mjs" "getValue" (func $getValue (result i32)))`
@codecov

codecov Bot commented Apr 3, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.02%. Comparing base (67a94a9) to head (b5ae1c3).
⚠️ Report is 35 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #55      +/-   ##
==========================================
- Coverage   90.80%   89.02%   -1.78%     
==========================================
  Files           6        8       +2     
  Lines         500      328     -172     
  Branches       53       68      +15     
==========================================
- Hits          454      292     -162     
+ Misses         45       36       -9     
+ Partials        1        0       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread src/plugin/runtime/imports.ts Outdated
if (pkgImport && typeof pkgImport === "string") {
importFound = true;
imports.push(genImport(pkgImport, { name: "*", as: importName }));
} else if (fs.existsSync(path.resolve(directory, moduleName))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps we could use esm resolution to support node_modules and import maps and also fail if ESM import in wasm cannot be resolved?

(we can use exsolve for this)

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.

Nice, I'll switch to that.

There's currently the path where imports don't have to be resolved (e.g. resolved = false). If we error out I think we'll break that. Do we want a mode where imports have to be resolved or is there some other way you are thinking we should implement that?

@pi0 pi0 Apr 11, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could use exsolve.resolve(id, { try: true }. Is there a case that non resolvable ids might be safe?

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.

You could have a non-resolved id and then supply it in the imports. e.g.
importObj = { 'someFile.js': { foo: ....} }. I don't know how useful this feature is though. I'd prefer unwasm to error out if there's an unresolvable ESM import, but I think that will break the current behavior where resolving is optional.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Added TODO to make it throwing in next major 487fdb1

@jkomyno

jkomyno commented Jul 25, 2025

Copy link
Copy Markdown

Hey there! Is there anything missing in this PR that stops it from being merged?

@pi0 pi0 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks and sorry for late review ❤️

@pi0
pi0 merged commit 53e548e into unjs:main Aug 18, 2025
4 of 5 checks passed
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.

Support ESM imports in Wasm

3 participants