Skip to content

feat: add wasmBaseUrl option and migrate the build from tsup to tsdown - #26

Merged
balintbrews merged 13 commits into
balintbrews:mainfrom
harumijang:tsdown-migration
Oct 20, 2026
Merged

balintbrews merged 13 commits into
balintbrews:mainfrom
harumijang:tsdown-migration

Conversation

@harumijang

@harumijang harumijang commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

1. Migrate from tsup to tsdown

  • Replace tsup.config.ts with tsdown.config.ts, keeping the same output: a single ESM browser bundle with type declarations, and css-tree, lightningcss-wasm, tailwindcss-v3 and tailwindcss-v4 bundled in.
  • Add a small raw-imports plugin so the ?raw Tailwind CSS imports are inlined verbatim. Rolldown doesn't support ?raw, and tsdown's CSS handling would otherwise process the files. The plugin resolves each file to a \0-prefixed virtual id (the Rolldown convention, so other plugins skip it) and loads its contents as text.
  • Declare the ?raw Tailwind CSS imports explicitly in src/types/tailwindcss-v4.d.ts.

2. Remove the WASM workaround now that Vite resolves literal asset URLs

The Lightning CSS WASM was loaded with new URL(variable, import.meta.url), which consumers' bundlers can't detect. A banner ?url import forced them to copy the file, and Vite users also needed optimizeDeps.exclude: ['tailwindcss-in-browser'] in dev. With vitejs/vite#21434 (Vite 8.3.3+), Vite resolves literal new URL() asset references, including in pre-bundled dependencies, so the workaround can go.

  • Remove the banner ?url import and the version-pinned assetNames.
  • Reference the WASM with a literal new URL() so consumers' bundlers detect it and rewrite the URL themselves. The lightningcss-wasm version is inlined at build time via define, so dist/index.js contains a plain string, e.g. new URL("./lightningcss_node-1.30.1.wasm", import.meta.url).
  • Ship the WASM with copy, renamed to the same versioned file name (lightningcss_node-1.30.1.wasm, as in 0.6.0). The version is read once from lightningcss-wasm in tsdown.config.ts and used for both, so the URL and the file name can't drift apart, and the file name changes when the binary does, for cache busting.
  • Add a wasmBaseUrl option to transformCss() for apps that serve assets from a different path than their bundler assumes, e.g. Drupal Canvas serving them from a module directory. Only the directory is replaced: the library keeps the default file name (including the version, or any hash added by the consumer's bundler) and appends it to the base URL.
  • Misc: Bump the demo's Vite to ^8.3.3.

Testing instructions with Canvas MR: https://git.drupalcode.org/project/canvas/-/merge_requests/1812, using a packed build (npm pack) instead of a link:

  • Dev mode (Vite 8.3.3, no optimizeDeps.exclude): the package is pre-bundled into .vite/deps, and the WASM loads from node_modules/tailwindcss-in-browser/dist/lightningcss_node-1.30.1.wasm.
  • Production build: ui/dist/assets/ contains lightningcss_node-1.30.1.wasm, and with wasmBaseUrl the bundle resolves to the same path under the module directory.

tsup is unmaintained; tsdown is its actively-developed successor built on
Rolldown. Port the build config, mapping tsup options to their tsdown
equivalents: `noExternal` to `deps.alwaysBundle`, the esbuild
`esbuildOptions.assetNames` override to Rolldown's
`outputOptions.assetFileNames`, and the `loader` map to Rolldown module types
(`.wasm` via `asset`).

The Tailwind CSS assets are imported with a `?raw` suffix (a Vite convention
added for the Vitest browser tests). Rolldown doesn't resolve query suffixes,
and tsdown's CSS handling claims modules by their `.css` id (requiring
`@tsdown/css`, which would also preprocess the CSS). Since these strings must
stay verbatim — they're fed as `@import` sources into the browser-side
Tailwind compiler — add a small `raw-imports` plugin that resolves the real
file and loads its contents as a text module through a virtual id. Update the
`tailwindcss-v4` type declaration to match the `?raw` specifiers used in the
source.

Build output is unchanged: the same versioned WASM file (byte-identical), the
`?url` banner import, and the same exports and type declarations.
… resolve it

The Lightning CSS WASM file was loaded via
`new URL(lightningCssWasm, import.meta.url)`, where `lightningCssWasm` is an
import binding that minifies to a variable. Static-analysis-based bundlers
resolve `new URL` asset references only when the first argument is a string
literal (e.g. Vite's dependency optimizer), so a variable is left pointing at
the bundled module's own location. When a consumer pre-bundles this library
into a cache directory, the WASM URL resolves there instead of next to the
shipped file, and instantiation fails. Consumers currently work around this by
excluding the library from dependency optimization.

Reference the file with a relative literal,
`new URL("./lightningcss_node.wasm", import.meta.url)`, and ship the WASM
file in the package by copying it verbatim from `lightningcss-wasm` (it is no
longer emitted via a module import). This lets bundlers rewrite the reference to
the real file, so consumers no longer need the optimization workaround.

The `banner` `?url` import and the `assetFileNames` version-pinning existed
only to make the WASM filename predictable for that hardcoded import; with the
literal reference in place they are removed.
@harumijang harumijang changed the title build: migrate from tsup to tsdown feat: add wasmBaseUrl option and migrate the build from tsup to tsdown Oct 16, 2026
@harumijang
harumijang marked this pull request as ready for review October 16, 2026 21:53
Comment thread README.md Resolved
@balintbrews
balintbrews merged commit fd4042b into balintbrews:main Oct 20, 2026
2 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.

2 participants