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 esbuild plugin #113

Merged
merged 40 commits into from
Jan 7, 2024

Conversation

nedjulius
Copy link
Contributor

@nedjulius nedjulius commented Dec 9, 2023

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:

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

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 9, 2023
@nedjulius nedjulius changed the title Nedjulius/add esbuild plugin feat: add esbuild plugin Dec 9, 2023

return {
contents: code,
loader: currFileName.endsWith('.js') ? 'js' : 'tsx',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tsx loader can handle ts, tsx and jsx files

Copy link
Contributor Author

@nedjulius nedjulius Dec 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update: turns out this wasn't entirely correct. encountered some import resolution problems when using tsx loader for JSX files, even though the esbuild documentation said that either loader is fine. for the sake of simplicity, I just match extension to loader. we can rely on extensions because of the filter guarantees.

I could also create a dictionary and map extension to loader in case esbuild changes the loader names or we want to use different loader for a given extension. lmk what you think.

@nmn
Copy link
Contributor

nmn commented Dec 9, 2023

Looks promising so far!

@nmn nmn added the enhancement New feature or request label Dec 9, 2023
@@ -196,6 +196,48 @@ provides plugins for Webpack, Rollup, and Next.js.

</details>

<details>
Copy link
Contributor Author

@nedjulius nedjulius Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added esbuild plugin example to the installations page in docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be patient as it may take me a few days to review this properly.

@nmn
Copy link
Contributor

nmn commented Dec 18, 2023

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

@nedjulius
Copy link
Contributor Author

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

flow-typed/node-libraries.js Outdated Show resolved Hide resolved
Copy link
Contributor

@nmn nmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's almost there! I've left a few comments. Please address those and I'll test it merge it within a few days.

packages/esbuild-plugin/src/index.js Outdated Show resolved Hide resolved
packages/esbuild-plugin/src/index.js Show resolved Hide resolved
packages/esbuild-plugin/src/index.js Show resolved Hide resolved
@nedjulius
Copy link
Contributor Author

hey @nmn are you still considering adding this?

@nmn
Copy link
Contributor

nmn commented Jan 7, 2024

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

@nmn nmn changed the base branch from main to release/v0.5.0 January 7, 2024 13:43
@nmn nmn merged commit 44076e8 into facebook:release/v0.5.0 Jan 7, 2024
9 checks passed
nmn pushed a commit that referenced this pull request Jan 10, 2024
* Add eslint-plugin package

* Add initial test suite

* Improve CSS handling and tests

* Address some of the comments

* Add babel scripts for jsx, flow and ts

* Add package diff

* Add snapshots and fix nits

* Fix rename bug

* Add esbuild example app

* Fix incorrect loaders

* Update package-lock.json

* Remove unnecessary file

* Fix some nits in example

* Add additional style to text fixture

* Remove ext from import in example

* Address some of the comments

* Update package-lock.json

* Add missing function call and snapshot

* Remove comment

* Pluralize function name

* Add types

* Fix build script

* Add docs

* Fix tabulation

* Retake snapshots

* Format prettier

* Add more missing types

* Run formatter

* Fix formatting

* Lock dependency and fix import

* Disable unused key

* Add docs

* Update package-lock.json

* Update package-lock.json

* Add snapshots

* Address comments
nmn pushed a commit that referenced this pull request Jan 25, 2024
* Add eslint-plugin package

* Add initial test suite

* Improve CSS handling and tests

* Address some of the comments

* Add babel scripts for jsx, flow and ts

* Add package diff

* Add snapshots and fix nits

* Fix rename bug

* Add esbuild example app

* Fix incorrect loaders

* Update package-lock.json

* Remove unnecessary file

* Fix some nits in example

* Add additional style to text fixture

* Remove ext from import in example

* Address some of the comments

* Update package-lock.json

* Add missing function call and snapshot

* Remove comment

* Pluralize function name

* Add types

* Fix build script

* Add docs

* Fix tabulation

* Retake snapshots

* Format prettier

* Add more missing types

* Run formatter

* Fix formatting

* Lock dependency and fix import

* Disable unused key

* Add docs

* Update package-lock.json

* Update package-lock.json

* Add snapshots

* Address comments
nmn pushed a commit that referenced this pull request Jan 26, 2024
* Add eslint-plugin package

* Add initial test suite

* Improve CSS handling and tests

* Address some of the comments

* Add babel scripts for jsx, flow and ts

* Add package diff

* Add snapshots and fix nits

* Fix rename bug

* Add esbuild example app

* Fix incorrect loaders

* Update package-lock.json

* Remove unnecessary file

* Fix some nits in example

* Add additional style to text fixture

* Remove ext from import in example

* Address some of the comments

* Update package-lock.json

* Add missing function call and snapshot

* Remove comment

* Pluralize function name

* Add types

* Fix build script

* Add docs

* Fix tabulation

* Retake snapshots

* Format prettier

* Add more missing types

* Run formatter

* Fix formatting

* Lock dependency and fix import

* Disable unused key

* Add docs

* Update package-lock.json

* Update package-lock.json

* Add snapshots

* Address comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants