Skip to content

Commit a4947ef

Browse files
pedrolamassidharthachatterjee
authored andcommitted
fix(gatsby-plugin-mdx): Replaces to-style with style-to-object (#16062)
1 parent 8f58d4f commit a4947ef

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

packages/gatsby-plugin-mdx/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"@babel/preset-env": "^7.4.3",
2626
"@babel/preset-react": "^7.0.0",
2727
"@babel/types": "^7.5.5",
28+
"camelcase-css": "^2.0.1",
2829
"change-case": "^3.1.0",
2930
"core-js": "2",
3031
"dataloader": "^1.4.0",
@@ -46,7 +47,7 @@
4647
"retext-english": "^3.0.2",
4748
"slash": "^2.0.0",
4849
"static-site-generator-webpack-plugin": "^3.4.2",
49-
"to-style": "^1.3.3",
50+
"style-to-object": "^0.2.3",
5051
"underscore.string": "^3.3.4",
5152
"unified": "^8.3.2",
5253
"unist-util-map": "^1.0.4",

packages/gatsby-plugin-mdx/utils/babel-plugin-html-attr-to-jsx-attr.js

+8-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { camelCase } = require("change-case");
2-
const toStyleObject = require("to-style").object;
2+
const styleToObject = require('style-to-object');
3+
const camelCaseCSS = require('camelcase-css');
34
const t = require("@babel/types");
45

56
// object retrieved from https://github.com/facebook/react/blob/master/packages/react-dom/src/shared/possibleStandardNames.js
@@ -490,19 +491,6 @@ var TRANSLATIONS = {
490491
zoomandpan: "zoomAndPan",
491492
}
492493

493-
const valueFromType = value => {
494-
switch (typeof value) {
495-
case "string":
496-
return t.stringLiteral(value);
497-
case "number":
498-
return t.numericLiteral(value);
499-
case "boolean":
500-
return t.booleanLiteral(value);
501-
default:
502-
throw new Error("gatsby-plugin-mdx needs to include a new type");
503-
}
504-
};
505-
506494
const propsKeysVisitor = {
507495
ObjectProperty(node) {
508496
if (node.node.key.extra.rawValue in TRANSLATIONS) {
@@ -529,14 +517,14 @@ var jsxAttributeFromHTMLAttributeVisitor = {
529517
node.node.value.type === "StringLiteral"
530518
// node.node.value.type !== "JSXExpressionContainer"
531519
) {
532-
const styleObject = toStyleObject(node.node.value.extra.rawValue, {
533-
camelize: true
534-
});
535-
// node.node.value.value = `{${JSON.stringify(styleObject)}}`;
520+
let styleArray = []
521+
styleToObject(node.node.value.extra.rawValue, function(name, value, declaration) {
522+
styleArray.push([camelCaseCSS(name), value])
523+
})
536524
node.node.value = t.jSXExpressionContainer(
537525
t.objectExpression(
538-
Object.entries(styleObject).map(([key, value]) =>
539-
t.objectProperty(t.StringLiteral(key), valueFromType(value))
526+
styleArray.map(([key, value]) =>
527+
t.objectProperty(t.StringLiteral(key), t.stringLiteral(value))
540528
)
541529
)
542530
);

yarn.lock

+17
Original file line numberDiff line numberDiff line change
@@ -5680,6 +5680,11 @@ [email protected], camel-case@^3.0.0:
56805680
no-case "^2.2.0"
56815681
upper-case "^1.1.1"
56825682

5683+
camelcase-css@^2.0.1:
5684+
version "2.0.1"
5685+
resolved "https://registry.yarnpkg.com/camelcase-css/-/camelcase-css-2.0.1.tgz#ee978f6947914cc30c6b44741b6ed1df7f043fd5"
5686+
integrity sha512-QOSvevhslijgYwRx6Rv7zKdMF8lbRmx+uQGx2+vDc+KI/eBnsy9kit5aj23AgGu3pa4t9AgwbnXWqS+iOY+2aA==
5687+
56835688
camelcase-keys@^2.0.0:
56845689
version "2.1.0"
56855690
resolved "https://registry.yarnpkg.com/camelcase-keys/-/camelcase-keys-2.1.0.tgz#308beeaffdf28119051efa1d932213c91b8f92e7"
@@ -11291,6 +11296,11 @@ ink@^2.3.0:
1129111296
wrap-ansi "^5.0.0"
1129211297
yoga-layout-prebuilt "^1.9.3"
1129311298

11299+
11300+
version "0.1.1"
11301+
resolved "https://registry.yarnpkg.com/inline-style-parser/-/inline-style-parser-0.1.1.tgz#ec8a3b429274e9c0a1f1c4ffa9453a7fef72cea1"
11302+
integrity sha512-7NXolsK4CAS5+xvdj5OMMbI962hU/wvwoxk+LWR9Ek9bVtyuuYScDN6eS0rUm6TxApFpw7CX1o4uJzcd4AyD3Q==
11303+
1129411304
inline-style-prefixer@^4.0.0:
1129511305
version "4.0.2"
1129611306
resolved "https://registry.yarnpkg.com/inline-style-prefixer/-/inline-style-prefixer-4.0.2.tgz#d390957d26f281255fe101da863158ac6eb60911"
@@ -19558,6 +19568,13 @@ style-to-object@^0.2.1:
1955819568
dependencies:
1955919569
css "2.2.4"
1956019570

19571+
style-to-object@^0.2.3:
19572+
version "0.2.3"
19573+
resolved "https://registry.yarnpkg.com/style-to-object/-/style-to-object-0.2.3.tgz#afcf42bc03846b1e311880c55632a26ad2780bcb"
19574+
integrity sha512-1d/k4EY2N7jVLOqf2j04dTc37TPOv/hHxZmvpg8Pdh8UYydxeu/C1W1U4vD8alzf5V2Gt7rLsmkr4dxAlDm9ng==
19575+
dependencies:
19576+
inline-style-parser "0.1.1"
19577+
1956119578
stylehacks@^4.0.0:
1956219579
version "4.0.0"
1956319580
resolved "https://registry.yarnpkg.com/stylehacks/-/stylehacks-4.0.0.tgz#64b323951c4a24e5fc7b2ec06c137bf32d155e8a"

0 commit comments

Comments
 (0)