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 file exports #755

Merged
merged 1 commit into from
Aug 6, 2024

Conversation

elevatebart
Copy link
Contributor

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:

import initLightningCSS from 'lightningcss-wasm'

async function whereItsUsed(){
  await initLightningCSS()
  // ... the rest
}

I get an error about invalid URL when creating a URL object. Indeed, since I bundle using esbuild, import.meta.url is null. The wasm 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!...

import initLightningCSS from 'lightningcss-wasm'
import lightningCSSurl from 'lightningcss-wasm/lightningcss_node.wasm'

async function whereItsUsed(){
  await initLightningCSS(lightningCSSurl)
  // ... the rest
}

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.

@elevatebart elevatebart changed the title "Add wasm file to file exports" feat: add wasm file to file exports Jun 7, 2024
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.

2 participants