MCPcopy Create free account
hub / github.com/DataV-Team/DataV-Vue3 / transform

Function transform

scripts/plugins/compileStyledComponentsPlugin.ts:24–76  ·  view source on GitHub ↗
(content, id)

Source from the content-addressed store, hash-verified

22 name: 'compileStyledComponentsPlugin',
23
24 async transform(content, id) {
25 if (!/tsx$/.test(id)) {
26 return;
27 }
28
29 const source = project.getSourceFile(id);
30
31 if (fileMap.has(id) && fileMap.get(id) !== content) {
32 source.refreshFromFileSystemSync();
33 }
34 fileMap.set(id, content);
35
36 const needTransformNode: NoSubstitutionTemplateLiteral[] = [];
37
38 source.forEachDescendant((node) => {
39 if (!node.isKind(SyntaxKind.NoSubstitutionTemplateLiteral)) {
40 return;
41 }
42
43 const isStyledTemplate = node.getParentIf((parent) =>
44 parent.getChildAtIndex(0).getFullText().trim().startsWith('styled')
45 );
46 if (!isStyledTemplate) {
47 return;
48 }
49
50 needTransformNode.push(node);
51 });
52
53 if (!needTransformNode.length) {
54 return;
55 }
56
57 await Promise.all(
58 needTransformNode.map(async (node) => {
59 const lessString = node.getFullText().trim().replaceAll('`', '');
60
61 const prefix = `${styledPlaceholder} {`;
62
63 const lessInput = `${prefix}${lessString}}`;
64
65 const cssOutput = (await less.render(lessInput)).css.trim();
66 const cssString = cssOutput.slice(prefix.length, cssOutput.length - 1);
67
68 node.replaceWithText(`\`${cssString}\``);
69 })
70 );
71
72 return {
73 code: source.getFullText(),
74 map: null,
75 };
76 },
77 };
78}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected