Skip to content

Commit a19996d

Browse files
committed
feat: prepend
1 parent 3a73791 commit a19996d

File tree

4 files changed

+91
-9
lines changed

4 files changed

+91
-9
lines changed

cmp/compiler/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,18 @@
2020
"test": "echo 'not implemented'"
2121
},
2222
"dependencies": {
23+
"@babel/core": "^7.26.0",
24+
"@babel/generator": "^7.26.0",
25+
"@babel/parser": "^7.26.0",
26+
"@babel/traverse": "^7.26.0",
27+
"@babel/types": "^7.26.0",
2328
"unplugin": "^2.3.10"
2429
},
2530
"devDependencies": {
31+
"@types/babel__core": "^7.20.5",
32+
"@types/babel__generator": "^7.6.8",
33+
"@types/babel__traverse": "^7.20.6",
34+
"@types/node": "^24.10.1",
2635
"tsup": "^8.3.0",
2736
"typescript": "^5.6.0",
2837
"vite": "^7.2.4"

cmp/compiler/src/index.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { createUnplugin } from 'unplugin';
2+
import { parse } from '@babel/parser';
3+
import traverse from '@babel/traverse';
4+
import generate from '@babel/generator';
5+
import * as t from '@babel/types';
26

37
export interface CompilerOptions {}
48

@@ -20,8 +24,36 @@ export const unplugin = createUnplugin<CompilerOptions>((options = {}) => {
2024
},
2125

2226
transform(code: string, id: string) {
23-
// noop
24-
return null;
27+
// Only transform JSX/TSX files
28+
if (!/\.[jt]sx$/.test(id)) {
29+
return null;
30+
}
31+
32+
try {
33+
const ast = parse(code, {
34+
sourceType: 'module',
35+
plugins: ['jsx', 'typescript'],
36+
});
37+
38+
traverse(ast, {
39+
JSXText(path) {
40+
const value = path.node.value;
41+
// Check if text is non-empty and meaningful (not just whitespace)
42+
if (value.trim().length > 0) {
43+
path.node.value = value.replace(/^(\s*)(.+?)(\s*)$/, '$1[compiler] $2$3');
44+
}
45+
},
46+
});
47+
48+
const output = generate(ast, {}, code);
49+
return {
50+
code: output.code,
51+
map: output.map,
52+
};
53+
} catch (error) {
54+
console.error(`Error transforming ${id}:`, error);
55+
return null;
56+
}
2557
},
2658
};
2759
});

cmp/demo/vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
3+
import compiler from '@compiler/core/vite';
34

45
export default defineConfig({
5-
plugins: [react()],
6+
plugins: [compiler(), react()],
67
});

cmp/pnpm-lock.yaml

Lines changed: 46 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)