Skip to content

Commit ce2d348

Browse files
refactor(Sky): Enable runtime relative imports for Electron workbench
Simplify the Electron workbench loading by moving baseUrl computation from build-time JS patching to runtime configuration: - Add `_VSCODE_USE_RELATIVE_IMPORTS` global in Base.astro that tells the Electron workbench to use relative imports against http://localhost instead of vscode-file:// URIs - Remove the Step 5 build hook patch for workbench.js baseUrl computation - no longer needed since the runtime flag handles it - Add vscode-file: to CSP script-src, style-src, and connect-src allowlists in Electron.astro and index.astro for non-module assets (fetch, images, JSON) handled by the native Rust Scheme.rs This reduces build complexity while maintaining compatibility with Tauri's WKWebView.
1 parent 28938a5 commit ce2d348

4 files changed

Lines changed: 18 additions & 57 deletions

File tree

Source/Function/Markup/Base.astro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import Meta from "../Meta.astro";
2424
<!-- Global VSCode File Root - Must execute before module scripts -->
2525
<script is:inline>
2626
globalThis._VSCODE_FILE_ROOT = `${window.location.origin}/Static/Application/`;
27+
// Tells the Electron workbench to use relative imports
28+
// (resolves against http://localhost URL) instead of
29+
// vscode-file:// URLs that WKWebView can't import().
30+
globalThis._VSCODE_USE_RELATIVE_IMPORTS = true;
2731
// esbuild __name helper — required by Dependency/out/ files loaded in
2832
// extension host worker contexts that don't have their own __name definition.
2933
if (typeof globalThis.__name === "undefined") {

Source/pages/Electron.astro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,21 @@ const Worker = `/Worker.js?BASE_REMOTE=${encodeURIComponent(Astro.url.origin)}`;
9090
"'unsafe-inline'",
9191
"'unsafe-eval'",
9292
"blob:",
93+
"vscode-file:",
9394
"http://localhost:*",
9495
"https://tauri.localhost",
9596
],
9697
"style-src": [
9798
"'self'",
9899
"'unsafe-inline'",
100+
"vscode-file:",
99101
"http://localhost:*",
100102
"https://tauri.localhost",
101103
],
102104
"connect-src": [
103105
"'self'",
106+
"ipc:",
107+
"vscode-file:",
104108
"http://localhost:*",
105109
"https://tauri.localhost",
106110
"wss://tauri.localhost",

Source/pages/index.astro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,21 @@ const Worker = `/Worker.js?BASE_REMOTE=${encodeURIComponent(Astro.url.origin)}`;
9191
"'unsafe-inline'",
9292
"'unsafe-eval'",
9393
"blob:",
94+
"vscode-file:",
9495
"http://localhost:*",
9596
"https://tauri.localhost",
9697
],
9798
"style-src": [
9899
"'self'",
99100
"'unsafe-inline'",
101+
"vscode-file:",
100102
"http://localhost:*",
101103
"https://tauri.localhost",
102104
],
103105
"connect-src": [
104106
"'self'",
105107
"ipc:",
108+
"vscode-file:",
106109
"http://localhost:*",
107110
"https://tauri.localhost",
108111
"wss://tauri.localhost",

astro.config.ts

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -180,61 +180,12 @@ export default defineConfig({
180180
);
181181
await StripCSSImports(Destination);
182182

183-
// Step 5: Patch Electron workbench.js baseUrl computation.
184-
// The original uses vscode-file://vscode-app/{appRoot}/out/
185-
// which doesn't exist in Tauri. Replace with the embedded
186-
// asset root /Static/Application/ using location.origin.
187-
const ElectronWorkbench = join(
188-
Destination,
189-
"code",
190-
"electron-browser",
191-
"workbench",
192-
"workbench.js",
193-
);
194-
try {
195-
const WB = await readFile(
196-
ElectronWorkbench,
197-
"utf-8",
198-
);
199-
// The original line uses fileUriFromPath which
200-
// produces vscode-file:// URIs that Tauri can't load.
201-
// Replace with direct URL from _VSCODE_FILE_ROOT.
202-
const SearchStr =
203-
"fileUriFromPath(configuration.appRoot";
204-
const Idx = WB.indexOf(SearchStr);
205-
let Patched = WB;
206-
if (Idx !== -1) {
207-
// Find the enclosing: const baseUrl = new URL(`${...}/out/`);
208-
// Replace entire line from "const baseUrl" to the semicolon
209-
const LineStart = WB.lastIndexOf(
210-
"const baseUrl",
211-
Idx,
212-
);
213-
const LineEnd = WB.indexOf(";", Idx) + 1;
214-
if (LineStart !== -1 && LineEnd > 0) {
215-
Patched =
216-
WB.slice(0, LineStart) +
217-
`const baseUrl = new URL(globalThis._VSCODE_FILE_ROOT || "/Static/Application/", globalThis.location?.origin || "https://tauri.localhost")` +
218-
WB.slice(LineEnd);
219-
}
220-
}
221-
if (Patched !== WB) {
222-
await writeFile(
223-
ElectronWorkbench,
224-
Patched,
225-
"utf-8",
226-
);
227-
console.log(
228-
"[CopyVSCode] Step 5: Patched electron workbench baseUrl",
229-
);
230-
} else {
231-
console.log(
232-
"[CopyVSCode] Step 5: Pattern not found in workbench.js (may already be patched)",
233-
);
234-
}
235-
} catch {
236-
// Electron workbench may not exist (Browser/Mountain build)
237-
}
183+
// Step 5: No JS patching needed.
184+
// VSCODE_DEV=true in Wind's process.env + _VSCODE_USE_RELATIVE_IMPORTS=true
185+
// in Base.astro makes the Electron workbench use relative import paths
186+
// that resolve against http://localhost instead of vscode-file://.
187+
// The native vscode-file:// Rust handler (Scheme.rs) covers
188+
// non-module requests (fetch, images, JSON).
238189

239190
// Step 6: Inject __name shim into extension host iframe.
240191
// The blob worker created by this HTML doesn't have the
@@ -249,8 +200,7 @@ export default defineConfig({
249200
);
250201
try {
251202
const HTML = await readFile(ExtHostIframe, "utf-8");
252-
const NameShim =
253-
`var __defProp=Object.defineProperty;var __name=(t,v)=>__defProp(t,"name",{value:v,configurable:true});`;
203+
const NameShim = `var __defProp=Object.defineProperty;var __name=(t,v)=>__defProp(t,"name",{value:v,configurable:true});`;
254204
// Inject into the blob content (before the first globalThis._VSCODE line)
255205
const PatchedHTML = HTML.replace(
256206
"`/*extensionHostWorker*/`,",

0 commit comments

Comments
 (0)