Skip to content

Commit 871eada

Browse files
committed
Only import if using code hike
1 parent 1483cfe commit 871eada

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { JsxNode, SuperNode, visit } from "./nodes"
2+
3+
export function chUsage(tree: SuperNode) {
4+
const usage = []
5+
visit(
6+
tree,
7+
["mdxJsxFlowElement", "mdxJsxTextElement"],
8+
(node: JsxNode) => {
9+
if (
10+
node.name &&
11+
node.name.startsWith("CH.") &&
12+
!usage.includes(node.name)
13+
) {
14+
usage.push(node.name)
15+
}
16+
}
17+
)
18+
return usage
19+
}

packages/mdx/src/mdx-plugin/plugin.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { CH_CODE_CONFIG_VAR_NAME } from "./unist-utils"
99
import { transformPreviews } from "./preview"
1010
import { transformInlineCodes } from "./inline-code"
1111
import { EsmNode, SuperNode, visit } from "./nodes"
12+
import { chUsage } from "./ch-usage"
1213

1314
type CodeHikeConfig = {
1415
theme: any
@@ -47,10 +48,14 @@ export function remarkCodeHike(
4748
throw e
4849
}
4950

50-
addConfig(tree, config)
51+
const usage = chUsage(tree)
5152

52-
if (config.autoImport && !hasCodeHikeImport) {
53-
addImportNode(tree)
53+
if (usage.length > 0) {
54+
addConfig(tree, config)
55+
56+
if (config.autoImport && !hasCodeHikeImport) {
57+
addImportNode(tree)
58+
}
5459
}
5560
}
5661
}

0 commit comments

Comments
 (0)