Skip to content

Commit c7fad62

Browse files
committed
working fixing virtual
1 parent c2c03df commit c7fad62

2 files changed

Lines changed: 37 additions & 16 deletions

File tree

build/vite-plugin-openscript.js

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from "fs";
22
import path from "path";
33
import { normalizePath } from "vite";
4+
import MagicString from "magic-string";
45

56
/**
67
* OpenScript Component Auto-Import Plugin
@@ -59,7 +60,12 @@ export function openScriptComponentPlugin(options = {}) {
5960
load(id) {
6061
if (id === resolvedVirtualModuleId) {
6162
// Generate virtual module that imports all components
62-
return generateVirtualModule(componentsDir, components, autoRegister);
63+
return generateVirtualModule(
64+
config.root,
65+
componentsDir,
66+
components,
67+
autoRegister
68+
);
6369
}
6470
},
6571

@@ -74,32 +80,39 @@ export function openScriptComponentPlugin(options = {}) {
7480
const className = classMatch[1];
7581

7682
// 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
7883
if (code.includes(`this.name = "${className}"`)) return;
7984

85+
const s = new MagicString(code);
86+
8087
if (code.includes("constructor")) {
8188
// 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-
);
89+
const superMatch = code.match(/(super\s*\([^)]*\)\s*;?)/);
90+
if (superMatch) {
91+
const index = superMatch.index + superMatch[0].length;
92+
s.appendRight(
93+
index,
94+
`\n if (!this.name) this.name = "${className}";`
95+
);
96+
}
8797
} else {
8898
// No constructor, inject one
89-
// Find the first opening brace after class definition
9099
const classDef = classMatch[0];
91100
const openBraceIndex = code.indexOf("{", code.indexOf(classDef));
92101

93102
if (openBraceIndex !== -1) {
94-
return (
95-
code.slice(0, openBraceIndex + 1) +
96-
`\n constructor() { super(); this.name = "${className}"; }` +
97-
code.slice(openBraceIndex + 1)
103+
s.appendRight(
104+
openBraceIndex + 1,
105+
`\n constructor() { super(); this.name = "${className}"; }`
98106
);
99107
}
100108
}
101109

102-
return code;
110+
if (s.hasChanged()) {
111+
return {
112+
code: s.toString(),
113+
map: s.generateMap({ source: id, includeContent: true }),
114+
};
115+
}
103116
},
104117

105118
// HMR support
@@ -195,9 +208,14 @@ export {};
195208
/**
196209
* Generate virtual module content that imports and registers all components
197210
*/
198-
function generateVirtualModule(componentsDir, components, autoRegister) {
211+
function generateVirtualModule(root, componentsDir, components, autoRegister) {
199212
const imports = components
200-
.map((c) => `import ${c.name} from '../${componentsDir}/${c.path}';`)
213+
.map((c) => {
214+
const absolutePath = normalizePath(
215+
path.resolve(root, componentsDir, c.path)
216+
);
217+
return `import ${c.name} from '${absolutePath}';`;
218+
})
201219
.join("\n");
202220

203221
const exports = components.map((c) => c.name).join(", ");

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "modular-openscriptjs",
3-
"version": "1.0.9",
3+
"version": "1.0.12",
44
"description": "OpenScriptJs Framework - A lightweight, reactive JavaScript framework for building modern web applications",
55
"type": "module",
66
"main": "./dist/modular-openscriptjs.umd.js",
@@ -83,5 +83,8 @@
8383
"terser": "^5.36.0",
8484
"vite": "^7.2.4",
8585
"vitest": "^4.0.14"
86+
},
87+
"dependencies": {
88+
"magic-string": "^0.30.21"
8689
}
8790
}

0 commit comments

Comments
 (0)