postcss-react-strict-dom is a PostCSS plugin that enables static CSS extraction with React Strict DOM, compatible with various bundlers like Next.js and Expo Web.
Install the plugin via npm or yarn:
npm install postcss-react-strict-dom --save-dev
# or
yarn add postcss-react-strict-dom --dev
Configure postcss.config.js
to include postcss-react-strict-dom
in your project.
Below is an example postcss.config.js
setup for this plugin. By default, the plugin will reuse your project's Babel configuration for static CSS extraction.
// postcss.config.js
module.exports = {
plugins: {
'postcss-react-strict-dom': {
include: [
'src/**/*.{js,jsx,ts,tsx}',
],
},
autoprefixer: {},
},
}
type Options = {
include: string[]
exclude?: string[]
cwd: string
babelConfig: babel.TransformOptions
}
- include: Array of paths or glob patterns to compile.
- exclude: Array of paths or glob patterns to exclude from compilation. Paths in
exclude
take precedence overinclude
. - cwd: Working directory for the plugin; defaults to
process.cwd()
. - babelConfig: Options for Babel configuration. By default, the plugin reads from
babel.config.js
in your project. For custom configurations, setbabelrc: false
and specify desired options. Refer to Babel Config Options for available options.
A separate Babel configuration file (babel.config.js
) is required to support React Strict DOM. Configure it as shown below, referencing the react-strict-dom
Babel preset.
Reference: React Strict DOM Babel Preset
// babel.config.js
import reactStrictBabelPreset from 'react-strict-dom/babel-preset'
export default function babelConfig() {
return {
presets: [[reactStrictBabelPreset, { rootDir: process.cwd() }]],
}
}
Create a global.css
in your project:
/* src/global.css */
@stylex;
Then, import the file in your root layout file:
import '@/global.css'
The @stylex
directive will be automatically replaced with the statically extracted CSS by this plugin.
Once configured, the plugin will automatically process and extract static CSS based on your defined include
and exclude
options. This setup supports web-only projects like Next.js and Remix, and universal platforms with support for native apps like Expo.
- Expo Web Integration: Supports Expo Web with support for DOM components.
- Next.js Integration: Next.js demo. Uses the official Babel preset under the hood.
This project is licensed under the MIT License. See the LICENSE file for details.
Special thanks to the contributors of React Strict DOM and StyleX for their foundational work in CSS-in-JS and static extraction patterns.