Skip to content

Commit 0f92177

Browse files
javascripternecolas
authored andcommitted
[postcss-plugin] Implement custom importSources to skip compilation
1 parent 532510c commit 0f92177

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

packages/@stylexjs/postcss-plugin/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,14 @@ useCSSLayers: boolean; // Default: false
135135

136136
Enabling this option switches Stylex from using `:not(#\#)` to using `@layers`
137137
for handling CSS specificity.
138+
139+
---
140+
141+
### importSources
142+
143+
```js
144+
importSources: Array<string | { from: string, as: string }>; // Default: ['@stylexjs/stylex', 'stylex']
145+
```
146+
147+
Possible strings where you can import stylex from. Files that do not match the
148+
import sources may be skipped from being processed to speed up compilation.

packages/@stylexjs/postcss-plugin/src/builder.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ function createBuilder() {
104104

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

109110
const files = getFiles();
110111
const filesToTransform = [];
@@ -140,7 +141,7 @@ function createBuilder() {
140141
filesToTransform.map((file) => {
141142
const filePath = path.resolve(cwd, file);
142143
const contents = fs.readFileSync(filePath, 'utf-8');
143-
if (!bundler.shouldTransform(contents)) {
144+
if (!bundler.shouldTransform(contents, { importSources })) {
144145
return;
145146
}
146147
return bundler.transform(filePath, contents, babelConfig, {

packages/@stylexjs/postcss-plugin/src/bundler.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ module.exports = function createBundler() {
1313
const styleXRulesMap = new Map();
1414

1515
// Determines if the source code should be transformed based on the presence of StyleX imports.
16-
function shouldTransform(sourceCode) {
17-
return sourceCode.includes('stylex');
16+
function shouldTransform(sourceCode, options) {
17+
const { importSources } = options;
18+
19+
return importSources.some((importSource) => {
20+
if (typeof importSource === 'string') {
21+
return sourceCode.includes(importSource);
22+
}
23+
return importSource.includes(sourceCode.from);
24+
});
1825
}
1926

2027
// Transforms the source code using Babel, extracting StyleX rules and storing them.
@@ -25,6 +32,7 @@ module.exports = function createBundler() {
2532
filename: id,
2633
caller: {
2734
name: '@stylexjs/postcss-plugin',
35+
platform: 'web',
2836
isDev,
2937
},
3038
...babelConfig,

packages/@stylexjs/postcss-plugin/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const plugin = ({
2222
include,
2323
exclude,
2424
useCSSLayers = false,
25+
importSources = ['@stylexjs/stylex', 'stylex'],
2526
}) => {
2627
exclude = [
2728
// Exclude type declaration files by default because it never contains any CSS rules.
@@ -49,6 +50,7 @@ const plugin = ({
4950
cwd,
5051
babelConfig,
5152
useCSSLayers,
53+
importSources,
5254
isDev,
5355
});
5456

packages/docs/docs/api/configuration/postcss-plugin.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ Array of paths or glob patterns to exclude from compilation. Paths in `exclude`
4141

4242
---
4343

44+
## importSources
45+
46+
```js
47+
importSources: Array<string | { from: string, as: string }>; // Default: ['@stylexjs/stylex', 'stylex']
48+
```
49+
50+
Possible strings where you can import stylex from. Files that do not match the
51+
import sources may be skipped from being processed to speed up compilation.
52+
53+
---
54+
4455
### include
4556

4657
```js

0 commit comments

Comments
 (0)