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
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7d0268a
Add eslint-plugin package
nedjulius Dec 8, 2023
94b7407
Add initial test suite
nedjulius Dec 8, 2023
14637f0
Improve CSS handling and tests
nedjulius Dec 9, 2023
6fe9c5f
Address some of the comments
nedjulius Dec 9, 2023
d3a0f5f
Add babel scripts for jsx, flow and ts
nedjulius Dec 9, 2023
cd62e9f
Add package diff
nedjulius Dec 9, 2023
4a287db
Add snapshots and fix nits
nedjulius Dec 10, 2023
7811896
Fix conflicts
nedjulius Dec 10, 2023
17011de
Fix rename bug
nedjulius Dec 10, 2023
eb94b3c
Add esbuild example app
nedjulius Dec 10, 2023
90b06c3
Fix incorrect loaders
nedjulius Dec 10, 2023
083de80
Update package-lock.json
nedjulius Dec 10, 2023
891c561
Remove unnecessary file
nedjulius Dec 10, 2023
bad0b30
Fix some nits in example
nedjulius Dec 10, 2023
9c1ee21
Add additional style to text fixture
nedjulius Dec 11, 2023
0aa6583
Remove ext from import in example
nedjulius Dec 11, 2023
96324ac
Address some of the comments
nedjulius Dec 11, 2023
52caaf3
Update package-lock.json
nedjulius Dec 11, 2023
35f164d
Add missing function call and snapshot
nedjulius Dec 11, 2023
27dcb73
Remove comment
nedjulius Dec 11, 2023
0fcd813
Pluralize function name
nedjulius Dec 11, 2023
922f158
Merge branch 'main' of github.com:nedjulius/stylex into nedjulius/add…
nedjulius Dec 13, 2023
7a788d0
Add types
nedjulius Dec 13, 2023
99da76c
Fix build script
nedjulius Dec 13, 2023
f4ff4da
Add docs
nedjulius Dec 13, 2023
8b9fca8
Fix tabulation
nedjulius Dec 13, 2023
e48090a
Retake snapshots
nedjulius Dec 13, 2023
a12edd2
Format prettier
nedjulius Dec 13, 2023
3e1692f
Add more missing types
nedjulius Dec 13, 2023
2608220
Run formatter
nedjulius Dec 13, 2023
6b25103
Fix formatting
nedjulius Dec 14, 2023
736c1f2
Lock dependency and fix import
nedjulius Dec 14, 2023
9338cb8
Disable unused key
nedjulius Dec 14, 2023
4ff83a5
Add docs
nedjulius Dec 14, 2023
ac9caa0
Merge conflict
nedjulius Dec 17, 2023
a07f5bb
Update package-lock.json
nedjulius Dec 18, 2023
91f6eca
Update package-lock.json
nedjulius Dec 18, 2023
cd1f8b9
Add snapshots
nedjulius Dec 18, 2023
9615a5b
Address comments
nedjulius Dec 18, 2023
6fa1540
Merge branch 'main' into nedjulius/add-esbuild-plugin
nedjulius Dec 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
.*/lib/.*
.*/malformed_package_json/.*
.*/next-example/.*
.*/esbuild-example/.*

[include]

Expand Down
44 changes: 43 additions & 1 deletion apps/docs/docs/learn/03-installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ StyleX plugin. See the API reference for more details on the
`@stylexjs/babel-plugin` API.

To make this easier for commonly used packages and meta-frameworks, StyleX
provides plugins for Webpack, Rollup, and Next.js.
provides plugins for Webpack, Rollup, Next.js and esbuild.

<details>
<summary>Rollup</summary>
Expand Down Expand Up @@ -197,6 +197,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.

<summary>esbuild</summary>

<DevInstallExample dev={[`@stylexjs/esbuild-plugin`]} />

```tsx title="build.mjs"
import esbuild from 'esbuild';
import stylexPlugin from '@stylexjs/stylex';
import path from 'path';
import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

esbuild.build({
entryPoints: ['src/index.js'],
bundle: true,
outfile: './build/bundle.js',
minify: true,
plugins: [
stylexPlugin({
// If set to 'true', bundler will inject styles in-line
// Do not use in production
dev: false,
// Required. File path for the generated CSS file
generatedCSSFileName: path.resolve(__dirname, 'build/stylex.css'),
// Aliases for StyleX package imports
// default: '@stylexjs/stylex'
stylexImports: ['@stylexjs/stylex'],
// Required for CSS variable support
unstable_moduleResolution: {
// type: 'commonJS' | 'ESModules' | 'haste'
// default: 'commonJS'
type: 'commonJS',
// The absolute path to the root of your project
rootDir: __dirname,
},
}),
],
})
```
</details>

Please refer to the
[StyleX examples apps](https://github.com/facebook/stylex/tree/main/apps)
for demonstrations on how to use each of these plugins.
Expand Down
18 changes: 18 additions & 0 deletions apps/esbuild-example/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
*/

module.exports = {
plugins: ['@stylexjs'],
rules: {
'@stylexjs/valid-styles': 'error',
'ft-flow/space-after-type-colon': 0,
'ft-flow/no-types-missing-file-annotation': 0,
'ft-flow/generic-spacing': 0,
},
};
22 changes: 22 additions & 0 deletions apps/esbuild-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

added an example package that provides a simple build script example for a React application. it is also easier to develop the plugin this way.

"name": "esbuild-example",
"version": "0.0.0",
"description": "Simple esbuild example for @stylexjs/esbuild-plugin",
"main": "src/App.jsx",
"scripts": {
"build": "node scripts/build.mjs",
"lint": "eslint \"**/*.{js,jsx}\""
},
"license": "MIT",
"dependencies": {
"@stylexjs/stylex": "^0.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@stylexjs/esbuild-plugin": "^0.0.0",
nedjulius marked this conversation as resolved.
Show resolved Hide resolved
"@stylexjs/eslint-plugin": "^0.3.0",
"esbuild": "^0.19.9",
"eslint": "^8.55.0"
}
}
21 changes: 21 additions & 0 deletions apps/esbuild-example/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!doctype html>
<html>
<head>
<title>@stylexjs/esbuild-plugin</title>
<meta charset="utf-8" />
<style>
@layer reset {
body {
box-sizing: border-box;
padding: 0;
margin: 0;
}
}
</style>
<link rel="stylesheet" type="text/css" href="./dist/stylex.css" />
</head>
<body>
<div id="root"></div>
<script src="./dist/bundle.js"></script>
</body>
</html>
44 changes: 44 additions & 0 deletions apps/esbuild-example/scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
*/

import path from 'path';
import { fileURLToPath } from 'url';
import esbuild from 'esbuild';
import stylexPlugin from '@stylexjs/esbuild-plugin';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const BUILD_DIR_NAME = 'public/dist';
const OUTFILE = `${BUILD_DIR_NAME}/bundle.js`;
const STYLEX_BUNDLE_PATH = path.resolve(
__dirname,
'..',
`${BUILD_DIR_NAME}/stylex.css`,
);

esbuild
.build({
entryPoints: [path.resolve(__dirname, '..', 'src/App.jsx')],
bundle: true,
outfile: OUTFILE,
minify: true,
plugins: [
stylexPlugin({
useCSSLayers: true,
generatedCSSFileName: STYLEX_BUNDLE_PATH,
stylexImports: ['@stylexjs/stylex'],
unstable_moduleResolution: {
type: 'commonJS',
rootDir: path.resolve(__dirname, '../../..'),
},
}),
],
})
.catch(() => process.exit(1));
50 changes: 50 additions & 0 deletions apps/esbuild-example/src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
*/

'use strict';

import * as React from 'react';
import ReactDOM from 'react-dom';
import * as stylex from '@stylexjs/stylex';
import { colors } from '@stylexjs/open-props/lib/colors.stylex';
import { sizes } from '@stylexjs/open-props/lib/sizes.stylex';
import { fonts } from '@stylexjs/open-props/lib/fonts.stylex';

const styles = stylex.create({
main: {
width: '100vw',
height: '100vh',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: colors.pink7,
},
card: {
backgroundColor: colors.blue9,
padding: sizes.spacing5,
borderRadius: sizes.spacing2,
justifyContent: 'center',
display: 'flex',
alignItems: 'center',
color: colors.gray0,
fontFamily: fonts.mono,
},
});

function App() {
return (
<div {...stylex.props(styles.main)}>
<div {...stylex.props(styles.card)}>
<span>Blue rounded rectangle</span>
</div>
</div>
);
}

ReactDOM.render(<App />, document.getElementById('root'));
4 changes: 4 additions & 0 deletions flow-typed/babel-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ declare module '@babel/plugin-syntax-flow' {
declare module '@babel/plugin-syntax-jsx' {
declare module.exports: any;
}

declare module 'babel-plugin-syntax-hermes-parser' {
declare module.exports: any;
}
15 changes: 15 additions & 0 deletions flow-typed/node-libraries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

/* eslint-disable ft-flow/no-types-missing-file-annotation */
/* eslint-disable no-unused-vars */

declare module 'fs/promises' {
nedjulius marked this conversation as resolved.
Show resolved Hide resolved
declare module.exports: any;
}
Loading
Loading