Babel generated code isn’t picked up by the StyleX plugin #170
-
|
I’ve been experimenting with a Babel plugin that injects StyleX The plugin is really simplistic. It simply injects a Can anyone offer any guidance on this? Raw source fileimport styleX from "@stylexjs/stylex";
const styles = styleX.create({
base: {
color: "blue",
},
});
export default function Test() {
return (
<>
<p {...styleX.props(styles.base)}>StyleX</p>
<div>Generated</div>
</>
);
}After running inject-test.jsEverything looks good at this point. The import styleX from "@stylexjs/stylex";
const styles = styleX.create({
base: {
color: "blue"
}
});
export default function Test() {
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("p", styleX.props(styles.base), "StyleX"), /*#__PURE__*/React.createElement("div", styleX.props(generated.base), "Generated"));
}
const generated = styleX.create({
base: {
color: "green"
}
});After running @stylexjs/babel-pluginNote that the manually defined call is compiled, but the import styleX from "@stylexjs/stylex";
export default function Test() {
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("p", {
className: "xju2f9n"
}, "StyleX"), /*#__PURE__*/React.createElement("div", styleX.props(generated.base), "Generated"));
}
const generated = styleX.create({
base: {
color: "green"
}
});I’ve triple checked in AST explorers. This makes no sense to me. I can only assume that somewhere in the StyleX plugin there’s a node metadata check that’s causing the nodes to be ignored. .babelrc.jsmodule.exports = {
presets: ["@babel/preset-react"],
plugins: ["./inject-test.js", "@stylexjs/babel-plugin"],
};The source of my inject-test.js pluginconst babel = require("@babel/core");
const t = require("@babel/types");
module.exports = function () {
return {
visitor: {
Program: {
exit(path) {
// Construct the `styleX.create` call
const styleXCreateCall = t.variableDeclaration("const", [
t.variableDeclarator(
t.identifier("generated"),
t.callExpression(
t.memberExpression(
t.identifier("styleX"),
t.identifier("create"),
),
[
t.objectExpression([
t.objectProperty(
t.identifier("base"),
t.objectExpression([
t.objectProperty(
t.identifier("color"),
t.stringLiteral("green"),
),
]),
),
]),
],
),
),
]);
// Append the `styleX.create` call at the end of the file
path.pushContainer("body", styleXCreateCall);
},
},
JSXElement(path) {
if (path.node.openingElement.name.name === "div") {
// Construct the `styleX.props` spread attribute
const styleXPropsSpread = t.jsxSpreadAttribute(
t.callExpression(
t.memberExpression(t.identifier("styleX"), t.identifier("props")),
[
t.memberExpression(
t.identifier("generated"),
t.identifier("base"),
),
],
),
);
// Add the spread attribute to the `<div>`
path.node.openingElement.attributes.push(styleXPropsSpread);
}
},
},
};
}; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Silly error in my plugin call ordering. Specifically that I was adding the |
Beta Was this translation helpful? Give feedback.
Silly error in my plugin call ordering.
Specifically that I was adding the
createinexitwhereas the StyleX plugin does that onenter.