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

fix : Handle absolute paths for custom inject source #222

Open
wants to merge 10 commits into
base: feat/abs-paths
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
2 changes: 1 addition & 1 deletion apps/docs/docs/api/javascript/createTheme.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function createTheme(

```ts
import * as stylex from '@stylexjs/stylex';
import {colors} from './vars.stylex.js';
import { colors } from './vars.stylex.js';

const theme = stylex.createTheme(colors, {
accentColor: 'red',
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/docs/api/javascript/defineVars.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ You must define your variables as named exports in a `.stylex.js` (or
```tsx title="vars.stylex.js"
import * as stylex from '@stylexjs/stylex';

const colors = stylex.defineVars({
export const colors = stylex.defineVars({
accent: 'blue',
background: 'white',
line: 'gray',
Expand All @@ -38,7 +38,7 @@ You can then import and use these variables in any `stylex.create` call.

```tsx
import * as stylex from '@stylexjs/stylex';
import {colors} from './vars.stylex.js';
import { colors } from './vars.stylex.js';

const styles = stylex.create({
container: {
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 (experimental) plugins for Webpack, Rollup, and Next.js.
provides (experimental) plugins for Webpack, Rollup, Next.js and esbuild.

<details>
<summary>Rollup</summary>
Expand Down Expand Up @@ -197,6 +197,48 @@ provides (experimental) plugins for Webpack, Rollup, and Next.js.

</details>

<details>
<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
4 changes: 2 additions & 2 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@docusaurus/preset-classic": "2.4.1",
"@mdx-js/react": "^1.6.22",
"@orama/plugin-docusaurus": "^1.2.4",
"@stylexjs/stylex": "0.4.1",
"@stylexjs/stylex": "0.5.0-beta.1",
"@vercel/analytics": "^1.1.1",
"clsx": "^1.2.1",
"react": "^17.0.2",
Expand All @@ -29,7 +29,7 @@
},
"devDependencies": {
"@babel/eslint-parser": "^7.22.15",
"@stylexjs/babel-plugin": "0.4.1",
"@stylexjs/babel-plugin": "0.5.0-beta.1",
"clean-css": "^5.3.2",
"eslint": "^8.56.0",
"eslint-config-airbnb": "^19.0.4",
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 @@
{
"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",
"@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'));