Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/@stylexjs/postcss-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,14 @@ useCSSLayers: boolean; // Default: false

Enabling this option switches Stylex from using `:not(#\#)` to using `@layers`
for handling CSS specificity.

---

### importSources
Comment thread
javascripter marked this conversation as resolved.

```js
importSources: Array<string | { from: string, as: string }>; // Default: ['@stylexjs/stylex', 'stylex']
```

Possible strings where you can import stylex from. Files that do not match the
import sources may be skipped from being processed to speed up compilation.
5 changes: 3 additions & 2 deletions packages/@stylexjs/postcss-plugin/src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ function createBuilder() {

// Transforms the included files, bundles the CSS, and returns the result.
async function build({ shouldSkipTransformError }) {
const { cwd, babelConfig, useCSSLayers, isDev } = getConfig();
const { cwd, babelConfig, useCSSLayers, importSources, isDev } =
getConfig();

const files = getFiles();
const filesToTransform = [];
Expand Down Expand Up @@ -140,7 +141,7 @@ function createBuilder() {
filesToTransform.map((file) => {
const filePath = path.resolve(cwd, file);
const contents = fs.readFileSync(filePath, 'utf-8');
if (!bundler.shouldTransform(contents)) {
if (!bundler.shouldTransform(contents, { importSources })) {
return;
}
return bundler.transform(filePath, contents, babelConfig, {
Expand Down
12 changes: 10 additions & 2 deletions packages/@stylexjs/postcss-plugin/src/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ module.exports = function createBundler() {
const styleXRulesMap = new Map();

// Determines if the source code should be transformed based on the presence of StyleX imports.
function shouldTransform(sourceCode) {
return sourceCode.includes('stylex');
function shouldTransform(sourceCode, options) {
const { importSources } = options;

return importSources.some((importSource) => {
if (typeof importSource === 'string') {
return sourceCode.includes(importSource);
}
return importSource.includes(sourceCode.from);
});
}

// Transforms the source code using Babel, extracting StyleX rules and storing them.
Expand All @@ -25,6 +32,7 @@ module.exports = function createBundler() {
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.

isDev,
},
...babelConfig,
Expand Down
2 changes: 2 additions & 0 deletions packages/@stylexjs/postcss-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const plugin = ({
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.

}) => {
exclude = [
// Exclude type declaration files by default because it never contains any CSS rules.
Expand Down Expand Up @@ -49,6 +50,7 @@ const plugin = ({
cwd,
babelConfig,
useCSSLayers,
importSources,
isDev,
});

Expand Down
11 changes: 11 additions & 0 deletions packages/docs/docs/api/configuration/postcss-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ Array of paths or glob patterns to exclude from compilation. Paths in `exclude`

---

## importSources

```js
importSources: Array<string | { from: string, as: string }>; // Default: ['@stylexjs/stylex', 'stylex']
```

Possible strings where you can import stylex from. Files that do not match the
import sources may be skipped from being processed to speed up compilation.

---

### include

```js
Expand Down