Skip to content

Commit 3497b07

Browse files
committed
fix chunk transformer
1 parent 5734ebc commit 3497b07

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

sites/docs/scripts/transform-chunks.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ import { walk } from "estree-walker";
33
import prettier from "@prettier/sync";
44
import { codeBlockPrettierConfig } from "../other/code-block-prettier.js";
55

6-
// eslint-disable-next-line ts/no-explicit-any
7-
type Attribute = any;
8-
// eslint-disable-next-line ts/no-explicit-any
9-
type TemplateNode = any;
10-
116
type Chunk = {
127
name: string;
138
dependencies: string[];
@@ -18,38 +13,38 @@ type Chunk = {
1813
container: { className: string };
1914
};
2015
export function getChunks(source: string, filename: string) {
21-
const ast = parse(source, { filename });
16+
type TemplateNode = typeof ast["fragment"]["nodes"] extends Array<infer T> ? T : typeof ast["fragment"]["nodes"];
17+
const ast = parse(source, { filename, modern: true });
2218
const chunks: Chunk[] = [];
2319

24-
// eslint-disable-next-line ts/no-explicit-any
25-
walk(ast as any, {
26-
enter(n) {
20+
// @ts-expect-error yea, stfu
21+
walk(ast, {
22+
enter(n: TemplateNode) {
2723
const chunkNode = n as TemplateNode;
28-
if (chunkNode.type !== "Element" && chunkNode.type !== "InlineComponent") return;
24+
if (chunkNode.type !== "RegularElement" && chunkNode.type !== "Component") return;
2925

30-
const attrs: Attribute[] = chunkNode.attributes;
26+
const attrs = chunkNode.attributes.filter(a => a.type === "Attribute");
3127
const nameNode = attrs.find((a) => a.name === "data-x-chunk-name");
3228
const descriptionNode = attrs.find((a) => a.name === "data-x-chunk-description");
3329
if (descriptionNode === undefined || nameNode === undefined) return;
3430

3531
const containerNode = attrs.find((a) => a.name === "data-x-chunk-container");
3632

37-
const name: string = nameNode.value[0].data;
38-
const description: string = descriptionNode.value[0].data;
39-
const containerClassName: string = containerNode?.value[0].data ?? "";
33+
const name = extractAttributeValue(nameNode)!;
34+
const description = extractAttributeValue(descriptionNode)!;
35+
const containerClassName = containerNode ? extractAttributeValue(containerNode)! : "";
4036
const dependencies = new Set<string>();
4137

4238
// discard any prop members
43-
const [componentName] = chunkNode.name.split(".") as string[];
39+
const [componentName] = chunkNode.name.split(".");
4440
dependencies.add(componentName);
4541

4642
// walk the chunk to acquire all component dependencies
47-
// eslint-disable-next-line ts/no-explicit-any
48-
walk(chunkNode as any, {
49-
enter(n) {
50-
const node = n as TemplateNode;
51-
if (node.type === "InlineComponent") {
52-
const [componentName] = node.name.split(".") as string[];
43+
// @ts-expect-error stfu
44+
walk(chunkNode.fragment, {
45+
enter(node: TemplateNode) {
46+
if (node.type === "Component") {
47+
const [componentName] = node.name.split(".");
5348
dependencies.add(componentName);
5449
}
5550
},
@@ -75,6 +70,13 @@ export function getChunks(source: string, filename: string) {
7570
return chunks;
7671
}
7772

73+
// eslint-disable-next-line ts/no-explicit-any
74+
function extractAttributeValue(attribute: any): string | undefined {
75+
if (Array.isArray(attribute.value) && attribute.value[0].type === "Text") {
76+
return attribute.value[0].data;
77+
}
78+
}
79+
7880
export function transformChunk(source: string, chunk: Chunk): string {
7981
const html = source.substring(chunk.start, chunk.end);
8082
const lines = source.split("\n");
@@ -83,7 +85,7 @@ export function transformChunk(source: string, chunk: Chunk): string {
8385
// we only want to look at the script tag...
8486
.slice(0, scriptEndIdx)
8587
// spaced on the edges to prevent false positives (e.g. `CreditCard` could be falsely triggered by `Card`)
86-
.filter((line) => chunk.dependencies.some((dep) => line.includes(` ${dep} `)));
88+
.filter((line) => chunk.dependencies.some((dep) => line.includes(` ${dep} `) || line.includes(` ${dep},`)));
8789

8890
let template = `<script lang="ts">\n`;
8991
template += imports.join("\n");

0 commit comments

Comments
 (0)