-
Notifications
You must be signed in to change notification settings - Fork 2
/
mdx.macro.js
118 lines (105 loc) · 3.46 KB
/
mdx.macro.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const { createMacro } = require("babel-plugin-macros");
const mdx = require("@mdx-js/mdx");
const restSpreadSyntax = require("babel-plugin-syntax-object-rest-spread");
const jsxSyntax = require("babel-plugin-syntax-jsx");
const path = require("path");
const fs = require("fs");
module.exports = createMacro(MDX);
function MDX({ references, babel, state }) {
const { mdx, imports } = references;
const { types: t } = babel;
let mdxImports = [];
mdx.forEach(referencePath => {
if (referencePath.parentPath.type === "CallExpression") {
const importStatements = requireRaw({ referencePath, state, babel });
mdxImports = [...mdxImports, ...importStatements];
} else {
throw new Error(
`This is not supported: \`${referencePath
.findParent(babel.types.isExpression)
.getSource()}\`. Please see the raw.macro documentation`
);
}
});
// Process any imports and add them where `imports` was called from
imports.forEach(reference => {
let { ast, code } = babel.transform(
[`import {MDXTag} from '@mdx-js/tag'`].concat(mdxImports).join("\n"),
{
ast: true
}
);
reference.parentPath.replaceWithMultiple(
ast.program.body.map(impNode => {
return t.importDeclaration(impNode.specifiers, impNode.source);
})
);
});
}
function requireRaw({ referencePath, state, babel }) {
const filename = state.file.opts.filename;
const { types: t } = babel;
const callExpressionPath = referencePath.parentPath;
const dirname = path.dirname(filename);
let rawPath;
try {
rawPath = callExpressionPath.get("arguments")[0].evaluate().value;
} catch (err) {
// swallow error, print better error below
}
if (rawPath === undefined) {
throw new Error(
`There was a problem evaluating the value of the argument for the code: ${callExpressionPath.getSource()}. ` +
`If the value is dynamic, please make sure that its value is statically deterministic.`
);
}
const fullPath = path.resolve(dirname, rawPath);
const fileContent = fs.readFileSync(fullPath, { encoding: "utf-8" });
let transformedFunction = mdx.sync(fileContent).replace("export default", "");
const funcName = callExpressionPath.parent.id.name;
let mdxImports = [];
transformedFunction = transformedFunction
.split("\n")
.map(line => {
if (line.includes("import")) {
mdxImports.push(line);
return null;
} else {
return line;
}
})
.filter(Boolean)
.join("\n");
let { ast, code } = babel.transform(
`const ${funcName} = ${transformedFunction}`,
{
plugins: [jsxSyntax, restSpreadSyntax],
ast: true
}
);
callExpressionPath.replaceWith(
t.arrowFunctionExpression(
[
// build out a function argument that looks like
// ({ components, ...props })
t.objectPattern([
t.objectProperty(
t.identifier("components"),
t.identifier("components"),
false,
// set shorthand to true, otherwise it generates
// ({ components: components })
true
),
// spread in props
t.restElement(t.identifier("props"))
])
],
// :this_is_fine_dog:
// ensure we grab the last function, in case babel
// transforms the above code into more than one function
ast.program.body[ast.program.body.length - 1].declarations[0].init.body
)
);
return mdxImports;
}