Skip to content

Commit

Permalink
Support importSources and load the react-strict-dom babel plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
HorusGoul committed Mar 20, 2024
1 parent db792c2 commit a918795
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions packages/vite-plugin-stylex/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface StyleXVitePluginOptions
| "classNamePrefix"
| "unstable_moduleResolution"
| "useRemForFontSize"
| "importSources"
>
> {
stylexImports?: string[];
Expand Down Expand Up @@ -162,6 +163,25 @@ export default function styleXVitePlugin({
warned = true;
}

const styleXRelatedModules = new Set([...stylexImports]);

if (options.importSources) {
for (const source of options.importSources) {
if (typeof source === "string") {
styleXRelatedModules.add(source);
} else {
styleXRelatedModules.add(source.from);
}
}
}

const hasReactStrictDom = styleXRelatedModules.has("react-strict-dom");

const babelReactStrictDomPlugin = hasReactStrictDom
? // @ts-ignore
import("react-strict-dom/babel")
: false;

return {
name: "vite-plugin-stylex",

Expand Down Expand Up @@ -393,7 +413,11 @@ export default function styleXVitePlugin({
return inputCode.replace(STYLEX_REPLACE_RULE, compileStyleX());
}

if (!stylexImports.some((importName) => inputCode.includes(importName))) {
if (
!Array.from(styleXRelatedModules).some((importName) =>
inputCode.includes(importName)
)
) {
return;
}

Expand All @@ -413,6 +437,13 @@ export default function styleXVitePlugin({
? flowSyntaxPlugin
: typescriptSyntaxPlugin,
jsxSyntaxPlugin,
babelReactStrictDomPlugin
? await babelReactStrictDomPlugin.catch(() => {
this.error(
`Could not load the react-strict-dom babel plugin. Make sure you have the latest version of react-strict-dom installed.`
);
})
: null,
[
stylexBabelPlugin,
{
Expand All @@ -426,7 +457,7 @@ export default function styleXVitePlugin({
},
...options,
},
],
].filter(Boolean),
],
})
.catch((error) => {
Expand Down

0 comments on commit a918795

Please sign in to comment.