Skip to content

Commit 374a4f6

Browse files
committed
transform code
1 parent 68c5a44 commit 374a4f6

File tree

2 files changed

+26
-44
lines changed

2 files changed

+26
-44
lines changed

app/stores/app.js

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { transformExtensionCode } from '../utils/extensionCodeTransformer.js'
2+
13
export const useAppStore = defineStore("app", () => {
24
const stores = []
35

@@ -89,51 +91,10 @@ export const useAppStore = defineStore("app", () => {
8991

9092
if (path.startsWith('blob:')) {
9193
const response = await fetch(path)
92-
let code = await response.text()
93-
94-
code = code.replace(
95-
/import\s+(?:{([^}]+)}|\*\s+as\s+(\w+)|(\w+))\s+from\s+["']vue["'];?/gs,
96-
(match, namedImports, namespaceImport, defaultImport) => {
97-
if (namedImports) {
98-
const converted = namedImports.replace(/\s+as\s+/g, ': ')
99-
return `const {${converted}} = window.__VUE_RUNTIME__;`
100-
} else if (namespaceImport) {
101-
return `const ${namespaceImport} = window.__VUE_RUNTIME__;`
102-
} else if (defaultImport) {
103-
return `const ${defaultImport} = window.__VUE_RUNTIME__;`
104-
}
105-
return match
106-
}
107-
)
108-
109-
code = code.replace(
110-
/import\s+(?:{([^}]+)}|\*\s+as\s+(\w+)|(\w+))\s+from\s+["']pinia["'];?/gs,
111-
(match, namedImports, namespaceImport, defaultImport) => {
112-
if (namedImports) {
113-
const converted = namedImports.replace(/\s+as\s+/g, ': ')
114-
return `const {${converted}} = window.__PINIA__;`
115-
} else if (namespaceImport) {
116-
return `const ${namespaceImport} = window.__PINIA__;`
117-
} else if (defaultImport) {
118-
return `const ${defaultImport} = window.__PINIA__;`
119-
}
120-
return match
121-
}
122-
)
123-
124-
code = code.replace(
125-
/import\s+[^;]+from\s+["']@geode\/[^"']+["'];?/gs,
126-
(match) => `/* ${match} */ // External dependency - resolved at runtime\n`
127-
)
128-
129-
code = code.replace(
130-
/import\s+[^;]+from\s+["']@ogw_[^"']+["'];?/gs,
131-
(match) => `/* ${match} */ // External dependency - resolved at runtime\n`
132-
)
133-
134-
console.log('[AppStore] Rewritten extension code preview:', code.substring(0, 800))
94+
const code = await response.text()
95+
const transformedCode = transformExtensionCode(code)
13596

136-
const newBlob = new Blob([code], { type: 'application/javascript' })
97+
const newBlob = new Blob([transformedCode], { type: 'application/javascript' })
13798
finalURL = URL.createObjectURL(newBlob)
13899
}
139100

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export function transformExtensionCode(code) {
2+
code = code.replace(
3+
/from\s+["']vue["']/g,
4+
'from "data:text/javascript,' + encodeURIComponent(`
5+
const Vue = window.Vue;
6+
export default Vue;
7+
${Object.keys(window.Vue || {}).map(key => `export const ${key} = Vue.${key};`).join('')}
8+
`) + '"'
9+
)
10+
11+
code = code.replace(
12+
/from\s+["']pinia["']/g,
13+
'from "data:text/javascript,' + encodeURIComponent(`
14+
const Pinia = window.Pinia;
15+
export default Pinia;
16+
${Object.keys(window.Pinia || {}).map(key => `export const ${key} = Pinia.${key};`).join('')}
17+
`) + '"'
18+
)
19+
20+
return code
21+
}

0 commit comments

Comments
 (0)