-
Notifications
You must be signed in to change notification settings - Fork 412
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
give option target=all which generates pkg supporting commonjs and esm #313
Comments
Hey @ashleygwilliams, is this up for grabs, or underway? Happy to help out. |
@ashleygwilliams seems that #312 is merged. Do you have any plans to fix this? This would be really awesome. Then I'll be able to trash the following script, which packs for both targets and then merges these two packages into one. #!/usr/bin/env bash
set -e
# Check if jq is installed
if ! [ -x "$(command -v jq)" ]; then
echo "jq is not installed" >& 2
exit 1
fi
# Clean previous packages
if [ -d "pkg" ]; then
rm -rf pkg
fi
if [ -d "pkg-node" ]; then
rm -rf pkg-node
fi
# Build for both targets
wasm-pack build -t nodejs -d pkg-node
wasm-pack build -t browser -d pkg
# Get the package name
PKG_NAME=$(jq -r .name pkg/package.json | sed 's/\-/_/g')
# Merge nodejs & browser packages
cp "pkg-node/${PKG_NAME}.js" "pkg/${PKG_NAME}_main.js"
sed "s/require[\(]'\.\/${PKG_NAME}/require\('\.\/${PKG_NAME}_main/" "pkg-node/${PKG_NAME}_bg.js" > "pkg/${PKG_NAME}_bg.js"
jq ".files += [\"${PKG_NAME}_bg.js\"]" pkg/package.json \
| jq ".main = \"${PKG_NAME}_main.js\"" > pkg/temp.json
mv pkg/temp.json pkg/package.json
rm -rf pkg-node |
I should be able to start working on this. |
from #469, @KSXGitHub: When I write an NPM package, I rarely think of bundlers as they can comprehend
This is inconvenient, if I want to support both targets, I have to write my own wrapper that calls Suggestion
Additional SuggestionPlease don't use package name in |
I totally agree with the suggestion to generate multiple outputs as mentioned by @KSXGitHub and @ashleygwilliams ! And think it is definitely a good way forward for a However, I think it'd be good to look at "prior art" here, and take a look at how Rollup handles this same issue (specifying multiple JS targets for a library). Especially since rollup is "the bundler for libraries" (NOTE: The article is a bit outdated, but it drives the point). I think maybe we should follow a file bundling strategy similar to rollup-starter-lib. In which the
Regarding blunders, I think it would be up to the bundler to choose which format to choose when bundling (or up to the project author to choose the correct format for their bundler). Or, we can simply run the This is very similar to what @ashleygwilliams is suggesting above, but I guess I'm just more trying to nail down the convention here 😄 Please let me know if there is gaps in this at all I am missing. Also, it seems that a lot of other people have offered to pick this up, but I'd like to also offer as well. Let me know, thank you! 👍 P.S Glad I found this issue after the rust/wasm WG meeting haha! 😂 EDIT: P.S P.S I have started working on this, on my fork/branch. I've also been giving updates on this at the rust/wasm WG meetings. I'm about 1/2 way done. 😄 |
Just a note from a user here. We encountered the same issue as mentioned by @ashleygwilliams: We are using Typescript sources along with a Rust-based wasm helper. These are built by Webpack, and we would like to use Jest (or another node-based tool) for unit testing. Even by running node with Hence, it would work nicely if |
Same here, did you find any solution or workaround @stephanemagnenat ? |
I ran into this issue in the same way as the Jest users above. @chriamue, my workaround to simply run two wasm-pack commands wasm-pack build --target bundler --out-dir pkg # (the defaults)
wasm-pack build --target nodejs --out-dir pkg_node My web app's package.json has a dependency like "foo_wasm": "file:../wasm/foo/pkg", Then my jest.config.ts contains moduleNameMapper: {
foo_wasm: "<rootDir>/../wasm/foo/pkg_node",
}, |
Having just one package generated that can work with either a bundler, browser, or node will be awesome. Sadly, there is a whole lot of mess in the JS ecosystem about stuff like this, that is: workers, wasm, static files in packages, meant for use in both the browser, node, multiple types of bundlers, etc. So you might to actively try the output in different targets to make sure they all take the correct build or otherwise write instructions on how to make them do so. Worse are the inconsistencies in handling all the top level |
groundwork in #312 once merged, i'll start on it. in the meantime- if we want to bikeshed the target argument, let's
The text was updated successfully, but these errors were encountered: