stylex-webpack 0.3.1 → 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 +11 -13
- package/dist/index.js +7 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,31 +1,29 @@
|
|
|
1
1
|
# stylex-webpack
|
|
2
2
|
|
|
3
|
-
[First introduced at React Conf 2020
|
|
4
|
-
|
|
5
|
-
`stylex-webpack` is an alternative webpack plugin and loader for stylex.
|
|
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.
|
|
6
4
|
|
|
7
5
|
## Motivation
|
|
8
6
|
|
|
9
|
-
stylex
|
|
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.
|
|
10
8
|
|
|
11
|
-
|
|
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:
|
|
12
10
|
|
|
13
|
-
-
|
|
14
|
-
-
|
|
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.
|
|
15
13
|
|
|
16
|
-
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
|
|
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.
|
|
17
15
|
|
|
18
|
-
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.
|
|
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.
|
|
19
17
|
|
|
20
18
|
## Installation
|
|
21
19
|
|
|
22
20
|
```sh
|
|
23
21
|
# npm
|
|
24
|
-
npm i
|
|
22
|
+
npm i stylex-webpack
|
|
25
23
|
# Yarn
|
|
26
|
-
yarn add
|
|
24
|
+
yarn add stylex-webpack
|
|
27
25
|
# pnpm
|
|
28
|
-
pnpm add
|
|
26
|
+
pnpm add stylex-webpack
|
|
29
27
|
```
|
|
30
28
|
|
|
31
29
|
## Usage
|
|
@@ -66,7 +64,7 @@ module.exports = {
|
|
|
66
64
|
// next.config.js
|
|
67
65
|
const { withStyleX } = require('stylex-webpack/next');
|
|
68
66
|
|
|
69
|
-
module.exports =
|
|
67
|
+
module.exports = withStyleX({
|
|
70
68
|
// stylex-webpack options goes here, see the following section for more details
|
|
71
69
|
})({
|
|
72
70
|
// Your Next.js config goes here.
|
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
|
}
|