feat: add wasm file to file exports #755
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request adds the wasm file to the file exports. It allows esbuild to import the wasm file without copying it in the src directory, simplifying loading it in the browser when used in a library.
Use case
The tool I help maintain, updates CSS on the fly. Using lightningCSS in the browser was surprisingly easy.
The only true issue I had was init. When I run the init function:
I get an error about invalid URL when creating a URL object. Indeed, since I bundle using esbuild,
import.meta.url
is null. Thewasm
file cannot be resolved.Fortunately the function accepts a parameter to tell it where the asset might be. This url is inside of
node_modules
which should not be accessed by the server directly. But static assets can be nhandled very well in ESBuild using imports. If you import a file that is marked as a static asset it will copy it into the assets directory and update its url in the code.Last step: I needed to import the wasm in my file and "voila", problem solved!...
But... since ESBuild stricty respects esm, given the exports property in the current
package.json
one cannot import the wasm file without getting an error.Hence my very naive PR to add the export to the list.
If you have any question, I'll be happy to answer them here.