stylex-webpack 0.3.1-beta.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +226 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +7 -3
- package/dist/next.d.ts +5 -0
- package/dist/next.js +3 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# stylex-webpack
|
|
2
|
+
|
|
3
|
+
[First introduced by Frank Yan at React Conf 2020](https://www.youtube.com/watch?v=9JZHodNR184), [StyleX](https://stylexjs.com/) framework agnostic CSS-in-JS system with near-zero runtime, ahead-of-time compiler, atomic CSS extraction that powers Facebook and Instagram.
|
|
4
|
+
|
|
5
|
+
## Motivation
|
|
6
|
+
|
|
7
|
+
stylex offers a CSS-in-JS compiler, allowing you to write CSS in your JavaScript/JSX/TSX. However, unlike other CSS-in-JS solutions that gather and process styles within the browser, stylex will read your source code, collect your style and transform your JS/JSX/TSX, stripping runtime calls as much as possible (making the value of `className` a static string literal), and output CSS elsewhere.
|
|
8
|
+
|
|
9
|
+
StyleX does provide a webpack plugin. Under the hood, it will traverse through the source code, collect styles, and emit a new CSS asset during the webpack compilation. However, it does come with some limitations:
|
|
10
|
+
|
|
11
|
+
- StyleX's official Next.js setup requires a `.babelrc` file, which disables Next.js' built-in SWC compiler.
|
|
12
|
+
- StyleX's official Next.js plugin requires a CSS asset to pre-exist so that it can append the extracted CSS to it.
|
|
13
|
+
|
|
14
|
+
I start this project as a Proof of Concept, to see if it is possible to make a webpack plugin for ststylex that doesn't disable Next.js' SWC compiler. I have already made [a similar webpack plugin for style9](https://github.com/sukkaw/style9-webpack), which is also an AoT atomic CSS-in-JS system that is inspired by StyleX.
|
|
15
|
+
|
|
16
|
+
Unlike stylex's official webpack plugin, `stylex-webpack` requires you have setup `css-loader` and `MiniCssExtractPlugin` in your webpack configuration, just like your normal CSS based webpack project. `stylex-webpack`'s built-in loader will generate a virtual CSS import containing a dummy CSS rule. This allows the `MiniCssExtractPlugin` to collect those virtual CSS imports and emit a CSS asset, which `stylex-webpack` will later inject the actual extracted CSS into at the `processAssets` stage.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
# npm
|
|
22
|
+
npm i stylex-webpack
|
|
23
|
+
# Yarn
|
|
24
|
+
yarn add stylex-webpack
|
|
25
|
+
# pnpm
|
|
26
|
+
pnpm add stylex-webpack
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### Webpack
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
// webpack.config.js
|
|
35
|
+
const { StyleXPlugin } = require('stylex-webpack');
|
|
36
|
+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
|
37
|
+
|
|
38
|
+
module.exports = {
|
|
39
|
+
module: {
|
|
40
|
+
rules: [
|
|
41
|
+
// Just like your normal CSS setup, a css-loader and MiniCssExtractPlugin.loader
|
|
42
|
+
{
|
|
43
|
+
test: /\.css$/i,
|
|
44
|
+
use: [MiniCssExtractPlugin.loader, 'css-loader']
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
},
|
|
48
|
+
plugins: [
|
|
49
|
+
new StyleXPlugin({
|
|
50
|
+
// stylex-webpack options goes here, see the following section for more details
|
|
51
|
+
}),
|
|
52
|
+
new MiniCssExtractPlugin(),
|
|
53
|
+
new CssMinimizerPlugin()
|
|
54
|
+
// You can also use `LightningCssMinifyPlugin` from `lightningcss-loader`
|
|
55
|
+
// to replace CssMinimizerPlugin for faster CSS minification
|
|
56
|
+
// https://github.com/fz6m/lightningcss-loader
|
|
57
|
+
]
|
|
58
|
+
};
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Next.js
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
// next.config.js
|
|
65
|
+
const { withStyleX } = require('stylex-webpack/next');
|
|
66
|
+
|
|
67
|
+
module.exports = withStyleX({
|
|
68
|
+
// stylex-webpack options goes here, see the following section for more details
|
|
69
|
+
})({
|
|
70
|
+
// Your Next.js config goes here.
|
|
71
|
+
reactStrictMode: true
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Options
|
|
76
|
+
|
|
77
|
+
### webpack
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
new StyleXPlugin({
|
|
81
|
+
// stylex-webpack options
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* stylex options passed to stylex babel plugin
|
|
85
|
+
*
|
|
86
|
+
* @see https://stylexjs.com/docs/api/configuration/babel-plugin/
|
|
87
|
+
*/
|
|
88
|
+
stylexOption: {
|
|
89
|
+
dev: process.env.NODE_ENV === 'development',
|
|
90
|
+
test: process.env.NODE_ENV === 'test'
|
|
91
|
+
// Check the stylex documentation for more options
|
|
92
|
+
},
|
|
93
|
+
/* Specify where stylex will be imported from
|
|
94
|
+
* This overrides `importSources` in the `stylexOption` above
|
|
95
|
+
*
|
|
96
|
+
* @default ['stylex', '@stylexjs/stylex']
|
|
97
|
+
*/
|
|
98
|
+
stylexImports: ['stylex', '@stylexjs/stylex'],
|
|
99
|
+
/**
|
|
100
|
+
* Whether to use CSS layers
|
|
101
|
+
*
|
|
102
|
+
* @default false
|
|
103
|
+
*/
|
|
104
|
+
useCSSLayers?: boolean,
|
|
105
|
+
/**
|
|
106
|
+
* Enable other CSS transformation
|
|
107
|
+
*
|
|
108
|
+
* Since stylex-webpack's loader only emit virtual CSS imports with dummy rules,
|
|
109
|
+
* while the actual CSS is injected by the plugin after all loaders, you can not
|
|
110
|
+
* use postcss-loader + PostCSS plugins. You can manually transform the CSS here.
|
|
111
|
+
*/
|
|
112
|
+
transformCss(css) {
|
|
113
|
+
const postcss = require('postcss');
|
|
114
|
+
const autoprefixer = require('autoprefixer');
|
|
115
|
+
/**
|
|
116
|
+
* It is a known issue that stylex won't sort your at-rules and media queries.
|
|
117
|
+
*
|
|
118
|
+
* https://github.com/facebook/stylex/issues/455
|
|
119
|
+
* https://github.com/facebook/stylex/issues/517
|
|
120
|
+
*
|
|
121
|
+
* For now, it is recommended to use postcss-sort-media-queries as a workaround.
|
|
122
|
+
*/
|
|
123
|
+
const sortMediaQueries = require('postcss-sort-media-queries');
|
|
124
|
+
|
|
125
|
+
return postcss([
|
|
126
|
+
autoprefixer({
|
|
127
|
+
// autoprefixer options
|
|
128
|
+
}),
|
|
129
|
+
sortMediaQueries({
|
|
130
|
+
sort: 'mobile-first'
|
|
131
|
+
})
|
|
132
|
+
]).process(css, { from: undefined }).css;
|
|
133
|
+
|
|
134
|
+
// If you don't use custom PostCSS plugins (like `postcss-sort-media-queries`
|
|
135
|
+
// mentioned above), only downleveling CSS syntax using autoprefixer, you can
|
|
136
|
+
// also use LightningCSS. It is a Rust-based CSS transformer and minifier that
|
|
137
|
+
// has built-in downleveling support.
|
|
138
|
+
const browserslist = require('browserslist');
|
|
139
|
+
const { transform, browserslistToTargets } = require('lightningcss');
|
|
140
|
+
return transform({
|
|
141
|
+
code: Buffer.from(css),
|
|
142
|
+
targets: browserslistToTargets(browserslist('>= 0.25%'))
|
|
143
|
+
}).code;
|
|
144
|
+
|
|
145
|
+
// If you don't need to transform CSS at all, you can just return the input as-is as well.
|
|
146
|
+
return css;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Next.js
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
withStyleX({
|
|
155
|
+
// The same options as the webpack plugin, but with a few differences
|
|
156
|
+
stylexOption: {
|
|
157
|
+
/**
|
|
158
|
+
* You don't have to specify `dev` here. `stylex-webpack` will automatically read
|
|
159
|
+
* Next.js building mode and set `dev` accordingly.
|
|
160
|
+
*/
|
|
161
|
+
// dev: process.env.NODE_ENV === 'development',
|
|
162
|
+
},
|
|
163
|
+
/**
|
|
164
|
+
* You don't have to specify `transformCss` here. `stylex-webpack` will automatically
|
|
165
|
+
* read your PostCSS configuration and apply it here, just like how Next.js does.
|
|
166
|
+
*
|
|
167
|
+
* Under the hood, `withStyleX` uses Next.js built-in PostCSS config reader to
|
|
168
|
+
* maintain the consistency with Next.js' built-in PostCSS support.
|
|
169
|
+
*/
|
|
170
|
+
// transformCss(css) {}
|
|
171
|
+
})
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
It is recommended to use `postcss-sort-media-queries` as a workaround for stylex's known issue with sorting at-rules and media queries. You can configure it in your PostCSS configuration file, and `stylex-webpack` will automatically apply your PostCSS configuration to the extracted CSS just like Next.js' built-in PostCSS support.
|
|
175
|
+
|
|
176
|
+
```js
|
|
177
|
+
// postcss.config.js
|
|
178
|
+
|
|
179
|
+
/** @type {Record<'plugins', import('postcss').AcceptedPlugin[]>} */
|
|
180
|
+
module.exports = {
|
|
181
|
+
plugins: [
|
|
182
|
+
[
|
|
183
|
+
require.resolve('postcss-sort-media-queries'),
|
|
184
|
+
{
|
|
185
|
+
sort: 'mobile-first' // default value
|
|
186
|
+
}
|
|
187
|
+
],
|
|
188
|
+
|
|
189
|
+
// Next.js will disable its built-in default PostCSS configuration you
|
|
190
|
+
// create `postcss.config.js`, which you can add it back:
|
|
191
|
+
|
|
192
|
+
/* --- Start of Next.js built-in default PostCSS configuration --- */
|
|
193
|
+
require.resolve('next/dist/compiled/postcss-flexbugs-fixes'),
|
|
194
|
+
[
|
|
195
|
+
require.resolve('next/dist/compiled/postcss-preset-env'),
|
|
196
|
+
{
|
|
197
|
+
browsers: ['defaults'],
|
|
198
|
+
autoprefixer: {
|
|
199
|
+
// Disable legacy flexbox support
|
|
200
|
+
flexbox: 'no-2009'
|
|
201
|
+
},
|
|
202
|
+
// Enable CSS features that have shipped to the
|
|
203
|
+
// web platform, i.e. in 2+ browsers unflagged.
|
|
204
|
+
stage: 3,
|
|
205
|
+
features: {
|
|
206
|
+
'custom-properties': false
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
]
|
|
210
|
+
/* --- End of Next.js built-in default PostCSS configuration --- */
|
|
211
|
+
]
|
|
212
|
+
};
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Author
|
|
216
|
+
|
|
217
|
+
**stylex-webpack** © [Sukka](https://github.com/SukkaW), Released under the [MIT](./LICENSE) License.<br>
|
|
218
|
+
Authored and maintained by Sukka with help from contributors ([list](https://github.com/SukkaW/stylex-webpack/graphs/contributors)).
|
|
219
|
+
|
|
220
|
+
> [Personal Website](https://skk.moe) · [Blog](https://blog.skk.moe) · GitHub [@SukkaW](https://github.com/SukkaW) · Telegram Channel [@SukkaChannel](https://t.me/SukkaChannel) · Twitter [@isukkaw](https://twitter.com/isukkaw) · Mastodon [@sukka@acg.mn](https://acg.mn/@sukka) · Keybase [@sukka](https://keybase.io/sukka)
|
|
221
|
+
|
|
222
|
+
<p align="center">
|
|
223
|
+
<a href="https://github.com/sponsors/SukkaW/">
|
|
224
|
+
<img src="https://sponsor.cdn.skk.moe/sponsors.svg"/>
|
|
225
|
+
</a>
|
|
226
|
+
</p>
|
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,11 @@ interface StyleXLoaderOptions {
|
|
|
9
9
|
|
|
10
10
|
type CSSTransformer = (css: string) => string | Buffer | Promise<string | Buffer>;
|
|
11
11
|
interface StyleXPluginOption {
|
|
12
|
+
/**
|
|
13
|
+
* stylex options passed to stylex babel plugin
|
|
14
|
+
*
|
|
15
|
+
* @see https://stylexjs.com/docs/api/configuration/babel-plugin/
|
|
16
|
+
*/
|
|
12
17
|
stylexOption?: Partial<Options>;
|
|
13
18
|
/**
|
|
14
19
|
* Specify where stylex will be imported from
|
package/dist/index.js
CHANGED
|
@@ -128,16 +128,20 @@ class StyleXPlugin {
|
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
// Let's find the css file that belongs to the stylex chunk
|
|
131
|
-
const cssAssetDetails = Object.entries(assets).
|
|
132
|
-
if (
|
|
131
|
+
const cssAssetDetails = Object.entries(assets).filter(([assetName])=>stylexChunk.files.has(assetName) && assetName.endsWith('.css'));
|
|
132
|
+
if (cssAssetDetails.length === 0) {
|
|
133
133
|
return;
|
|
134
134
|
}
|
|
135
|
+
if (cssAssetDetails.length > 1) {
|
|
136
|
+
console.warn('[stylex-webpack] Multiple CSS assets found for the stylex chunk. This should not happen. Please report this issue.');
|
|
137
|
+
}
|
|
138
|
+
const stylexAsset = cssAssetDetails[0];
|
|
135
139
|
const stylexCSS = getStyleXRules(this.stylexRules, this.useCSSLayers);
|
|
136
140
|
if (stylexCSS == null) {
|
|
137
141
|
return;
|
|
138
142
|
}
|
|
139
143
|
const finalCss = await this.transformCss(stylexCSS);
|
|
140
|
-
compilation.updateAsset(
|
|
144
|
+
compilation.updateAsset(stylexAsset[0], (source)=>new ConcatSource(source, new RawSource(finalCss)));
|
|
141
145
|
});
|
|
142
146
|
});
|
|
143
147
|
}
|
package/dist/next.d.ts
CHANGED
|
@@ -3,6 +3,11 @@ import { Options } from '@stylexjs/babel-plugin';
|
|
|
3
3
|
|
|
4
4
|
type CSSTransformer = (css: string) => string | Buffer | Promise<string | Buffer>;
|
|
5
5
|
interface StyleXPluginOption {
|
|
6
|
+
/**
|
|
7
|
+
* stylex options passed to stylex babel plugin
|
|
8
|
+
*
|
|
9
|
+
* @see https://stylexjs.com/docs/api/configuration/babel-plugin/
|
|
10
|
+
*/
|
|
6
11
|
stylexOption?: Partial<Options>;
|
|
7
12
|
/**
|
|
8
13
|
* Specify where stylex will be imported from
|
package/dist/next.js
CHANGED
|
@@ -144,6 +144,9 @@ const withStyleX = (pluginOptions)=>(nextConfig = {})=>{
|
|
|
144
144
|
async transformCss (css) {
|
|
145
145
|
const { postcssWithPlugins } = await postcss();
|
|
146
146
|
const result = await postcssWithPlugins.process(css);
|
|
147
|
+
if (pluginOptions?.transformCss) {
|
|
148
|
+
return pluginOptions.transformCss(result.css);
|
|
149
|
+
}
|
|
147
150
|
return result.css;
|
|
148
151
|
}
|
|
149
152
|
}));
|