Closed ueokande closed 9 months ago
hey, this is a good catch.
esbuild TS loader respects the tsconfig.json
file and by default elides imports that are not referenced in the body of the importing module. StyleX babel plugin compiles away any references to stylex
or variable calls:
import stylex from '@stylexjs/stylex'; // unused import
import { colors } from './vars.stylex'; // unused import
window.dummy = {
className: "x9fir3 x1e8mcya"
}
esbuild traverses modules based on the imports of the entry module, and the variables file is not bundled due to the elision. to avoid this, you need to set verbatimModuleSyntax
option to true
in your tsconfig.json
file:
{
"compilerOptions": {
"verbatimModuleSyntax": true
}
}
this should definitely be documented or added as a tsconfig extension in StyleX
EDIT: the better option is mentioned below this comment. I also opened up a PR that sets the treeshakeCompensation
to true
in the esbuild plugin itself, so consumers won't have to do anything extra.
For variables, Stylex has an option called treeshakeCompensation
that should ensure that the imports for variables are not removed due to tree-shaking. Turning that option on, should fix the issue as well.
Describe the issue
When I use variables with the
@stylexjs/esbuild-plugin
, the plugin does not embed variables defined bystylex.defineVars()
into the generated CSS. It works in the ESM build as expected, but it does not work well in the TypeScript build. Here is an example:Built from ESM
Built from TypeScript
Expected behavior
The
@stylexjs/esbuild-plugin
embed variabled defined bystylex.defineVars()
in the TypeScript build.Steps to reproduce
I reproduce it by the following versions:
node build.mjs
build
directoryThe full example project is available here:
Test case
No response
Additional comments
No response