stylex icon indicating copy to clipboard operation
stylex copied to clipboard

feat: add `esbuild` plugin

Open nedjulius opened this issue 2 years ago • 9 comments

Addresses issue #83

Adds a StyleX plugin for esbuild bundler. There are some caveats with how the esbuild plugin API works, and it seems that the consumer will have to provide a full path to the build directory directly to the plugin, because there's no other way to access the build path in the onEnd callback. I am using the onEnd callback in this case (which runs at the end of the build, implied by the name), as we first have to transform each ts[x]/js[x] before we can process the StyleX rules. Let me know if you have simpler ideas on how to solve the bundling of CSS.

An example of how consuming the plugin would look like:

await esbuild.build({
  entryPoints: [path.resolve(__dirname, 'src/index.js')],
  outdir: 'dist',
  outfile: 'bundled.js',
  plugins: [stylexPlugin({ absoluteFilePath: path.resolve(__dirname, 'dist/stylex.css') })],
});

Some remaining things to do:

  • [x] Improve tests, specifically test for injections in dev and for the output in non-dev build
  • [x] Add CSS writes to disk

nedjulius avatar Dec 09 '23 01:12 nedjulius

Looks promising so far!

nmn avatar Dec 09 '23 01:12 nmn

@nedjulius Do you consider this PR ready for review?

nmn avatar Dec 11 '23 00:12 nmn

@nmn yeah, I think we can give it a go!

nedjulius avatar Dec 11 '23 00:12 nedjulius

@nmn I addressed all of your comments. the only issue left now is that flow complains about not being able to resolve some modules. not sure why though, but I assume it has something to do with those modules not having flow types (?). I saw that you created flow_modules file with type definitions for rollup and some babel plugins in the rollup-plugin -- is there a way to generate that somehow?

nedjulius avatar Dec 13 '23 17:12 nedjulius

is there a way to generate that somehow?

There are some scripts online for it as well, but I manually translate the Typescript types for them.

nmn avatar Dec 13 '23 19:12 nmn

@nmn got you, thanks!

I added translations for esbuild and also ported babel types from the rollup-plugin. perhaps @babel/core definition should be placed in flow-typed as it is now used in two packages?

besides that, I also added node-libraries.js file to flow-typed for node module declarations as I was getting type resolution issues with filesystem module.

nedjulius avatar Dec 14 '23 00:12 nedjulius

Lots of failing tests, so please work on that as I make my way through the PR.

nmn avatar Dec 18 '23 00:12 nmn

@nmn yup, looking into it. probably related to package lock merge conflicts that I tried to resolve yesterday.

nedjulius avatar Dec 18 '23 00:12 nedjulius

hey @nmn are you still considering adding this?

nedjulius avatar Jan 06 '24 17:01 nedjulius

@nedjulius Yes. Still planning to merge this for the next release.

nmn avatar Jan 07 '24 04:01 nmn