Skip to content

[postcss-plugin] Implement custom importSources for React Strict DOM compatibility#1063

Merged
necolas merged 1 commit into
facebook:mainfrom
javascripter:postcss-plugin-import-sources
May 16, 2025
Merged

[postcss-plugin] Implement custom importSources for React Strict DOM compatibility#1063
necolas merged 1 commit into
facebook:mainfrom
javascripter:postcss-plugin-import-sources

Conversation

@javascripter

@javascripter javascripter commented May 16, 2025

Copy link
Copy Markdown
Contributor

What changed / motivation ?

This PR adds importSources option to be configurable in order to allow reusing the plugin for react-strict-dom, and sets platform: "web" as babel.caller as required by RSD babel preset.

react-strict-dom currently relies on a community-maintained package postcss-react-strict-dom that I maintain to extract styles statically.

I'm intending to upstream the PostCSS plugin to RSD but the initial attempt required forking the plugin instead of simply re-exporting the plugin with pre-defined config because shouldTransform() function currently skips files that do not match stylex. This leads to additional maintenance burden and is not ideal.

By allowing importSources to be configurable, RSD users can now use this plugin with the following config:

module.exports = {
  plugins: {
    '@stylexjs/postcss-plugin': {
      include: [
        require.resolve('react-strict-dom'),
        // Include source files to watch for style changes
        'src/**/*.{js,jsx,mjs,ts,tsx}',
      ],
      importSources: ['stylex', 'react-strict-dom']
  }
}

After this PR gets merged, on RSD side we can create react-strict-dom/postcss-plugin that re-exports @stylexjs/postcss-plugin so that additional config remains implementation detail for the user.

This strategy seems to be aligned with the strategy necolas is considering for RSD's eslint plugin as well.

Linked PR/Issues

react/react-strict-dom#281 (comment)
react/react-strict-dom#282

Additional Context

I checked this PR makes the plugin compatible with RSD by:

  1. Install the plugin from this branch to the examples:
cd react-strict-dom/apps/examples
npm add "file:///PATH_TO_REPO/stylex/packages/@stylexjs/postcss-plugin"

Then, modify postcss.config.js to use this plugin:

module.exports = {
  plugins: {
    '@stylexjs/postcss-plugin': {
      include: [
        require.resolve('react-strict-dom'),
        'src/**/*.{js,jsx,mjs,ts,tsx}'
      ],
      impotSources: ['stylex', 'react-strict-dom']
    },
    autoprefixer: {}
  }
};

The example now renders correctly using the official @stylexjs/postcss-plugin:

npm run dev:web
Screenshot 2025-05-17 at 1 20 17

Pre-flight checklist

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 16, 2025
include,
exclude,
useCSSLayers = false,
importSources = ['@stylexjs/stylex', 'stylex'],

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Strictly speaking, we only need "stylex" to maintain backward compatibility as that was the default. In other places like eslint plugin, we use @stylexjs/stylex as the default value so I added both @stylexjs/stylex and stylex to reduce confusion.

If we're okay with breaking some backward compatibility, we can just use @stylexjs/stylex to align.

filename: id,
caller: {
name: '@stylexjs/postcss-plugin',
platform: 'web',

@javascripter javascripter May 16, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

https://facebook.github.io/react-strict-dom/learn/setup/#babel-configuration
RSD instructs users to configure babel.config.js which extract api.caller(caller => caller && caller.platform), so this just passes the correct value.

@necolas necolas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks!

Comment thread packages/@stylexjs/postcss-plugin/README.md
@necolas necolas force-pushed the postcss-plugin-import-sources branch from c1c6a10 to 061bb87 Compare May 16, 2025 18:05
@necolas necolas merged commit 0f92177 into facebook:main May 16, 2025
7 of 9 checks passed
@javascripter javascripter deleted the postcss-plugin-import-sources branch May 16, 2025 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants