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

Don't mix ESM import with CJS exports #12

Closed
brandonchinn178 opened this issue Jan 31, 2023 · 1 comment · Fixed by #13
Closed

Don't mix ESM import with CJS exports #12

brandonchinn178 opened this issue Jan 31, 2023 · 1 comment · Fixed by #13

Comments

@brandonchinn178
Copy link

brandonchinn178 commented Jan 31, 2023

index.js currently has both import ... from ... statements and module.exports = ... syntaxes, which are two different import systems.

The current problem I'm facing is I'm trying to bundle a script importing @bjorn3/browser_wasi_shim with webpack, and it fails at buildtime when importing with require() (because @bjorn3/browser_wasi_shim's package.json has an exports field that says index.js is only importable via import statements) and fails at runtime when importing with import (because webpack throws an error if an ESM module tries to set module.exports)

https://stackoverflow.com/a/42450109/4966649

Repro:

mkdir src
echo 'import { WASI } from "@bjorn3/browser_wasi_shim"' > src/index.js
npm install webpack webpack-cli @bjorn3/browser_wasi_shim
npx webpack
node dist/main.js

Expected clean exit, got Error: ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead


Less importantly, it would be great if @bjorn3/browser_wasi_shim could provide a UMD build so that it's easy to just do something like

<script src="//unpkg.com/@bjorn/browser_wasi_shim"></script>
<script>
  const { WASI } = window['@bjorn/browser_wasi_shim']
  new WASI(...)
</script>

For now, I can just bundle the package myself with webpack.

@bjorn3
Copy link
Owner

bjorn3 commented Jan 31, 2023

Thanks for the report! I'm currently rather busy, but I will try to get to this tomorrow or thursday.

bjorn3 added a commit that referenced this issue Feb 1, 2023
CommonJS style exports were previously used in index.js despite being an
ESM module. Switching to the ESM export syntax fixes webpack.

Fixes #12
@bjorn3 bjorn3 mentioned this issue Feb 1, 2023
6 tasks
@bjorn3 bjorn3 closed this as completed in 032766f Feb 1, 2023
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 a pull request may close this issue.

2 participants