Skip to content

Commit c2c03df

Browse files
committed
fixing declarations
1 parent 52ecd5b commit c2c03df

4 files changed

Lines changed: 104 additions & 2 deletions

File tree

build/vite-plugin-openscript.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,45 @@ export function openScriptComponentPlugin(options = {}) {
6363
}
6464
},
6565

66+
transform(code, id) {
67+
// Only transform files in components directory
68+
if (!id.includes(componentsDir) || !id.endsWith(".js")) return;
69+
70+
// Find class definition
71+
const classMatch = code.match(/class\s+(\w+)\s+extends\s+Component/);
72+
if (!classMatch) return;
73+
74+
const className = classMatch[1];
75+
76+
// If code already sets this.name explicitly, skip (simple check)
77+
// We still use the runtime check (!this.name) to be safe, but this avoids double injection if we run multiple times
78+
if (code.includes(`this.name = "${className}"`)) return;
79+
80+
if (code.includes("constructor")) {
81+
// Inject after super()
82+
// Matches super(...) or super() with optional semicolon
83+
return code.replace(
84+
/(super\s*\([^)]*\)\s*;?)/,
85+
`$1\n if (!this.name) this.name = "${className}";`
86+
);
87+
} else {
88+
// No constructor, inject one
89+
// Find the first opening brace after class definition
90+
const classDef = classMatch[0];
91+
const openBraceIndex = code.indexOf("{", code.indexOf(classDef));
92+
93+
if (openBraceIndex !== -1) {
94+
return (
95+
code.slice(0, openBraceIndex + 1) +
96+
`\n constructor() { super(); this.name = "${className}"; }` +
97+
code.slice(openBraceIndex + 1)
98+
);
99+
}
100+
}
101+
102+
return code;
103+
},
104+
66105
// HMR support
67106
handleHotUpdate({ file, server }) {
68107
if (file.includes(componentsDir)) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "modular-openscriptjs",
3-
"version": "1.0.8",
3+
"version": "1.0.9",
44
"description": "OpenScriptJs Framework - A lightweight, reactive JavaScript framework for building modern web applications",
55
"type": "module",
66
"main": "./dist/modular-openscriptjs.umd.js",

templates/bootstrap/vite.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineConfig } from 'vite';
2+
import { openScriptComponentPlugin } from '../..';
23

34
export default defineConfig({
45
server: {
@@ -8,5 +9,12 @@ export default defineConfig({
89
build: {
910
outDir: 'dist',
1011
sourcemap: true
11-
}
12+
},
13+
plugins: [
14+
openScriptComponentPlugin({
15+
componentsDir: 'src/components',
16+
autoRegister: true,
17+
generateTypes: true
18+
})
19+
]
1220
});

test_regex.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const code1 = `
2+
export default class MyComponent extends Component {
3+
constructor() {
4+
super();
5+
this.state = {};
6+
}
7+
}
8+
`;
9+
10+
const code2 = `
11+
export default class NoConstructor extends Component {
12+
render() { return h.div(); }
13+
}
14+
`;
15+
16+
const code3 = `
17+
export default class ExistingName extends Component {
18+
constructor() {
19+
super();
20+
this.name = "CustomName";
21+
}
22+
}
23+
`;
24+
25+
function transform(code, className) {
26+
if (code.includes(`this.name = "${className}"`)) return code;
27+
28+
if (code.includes("constructor")) {
29+
return code.replace(
30+
/(super\s*\([^)]*\)\s*;?)/,
31+
`$1\n if (!this.name) this.name = "${className}";`
32+
);
33+
} else {
34+
const classMatch = code.match(/class\s+(\w+)\s+extends\s+Component/);
35+
const classDef = classMatch[0];
36+
const openBraceIndex = code.indexOf("{", code.indexOf(classDef));
37+
if (openBraceIndex !== -1) {
38+
return (
39+
code.slice(0, openBraceIndex + 1) +
40+
`\n constructor() { super(); this.name = "${className}"; }` +
41+
code.slice(openBraceIndex + 1)
42+
);
43+
}
44+
}
45+
return code;
46+
}
47+
48+
console.log("--- Test 1 ---");
49+
console.log(transform(code1, "MyComponent"));
50+
51+
console.log("--- Test 2 ---");
52+
console.log(transform(code2, "NoConstructor"));
53+
54+
console.log("--- Test 3 ---");
55+
console.log(transform(code3, "ExistingName"));

0 commit comments

Comments
 (0)