Skip to content

Commit

Permalink
Fix cross-origin worker loading (again)
Browse files Browse the repository at this point in the history
The previous fix kind of worked, but it made the bundled index.js too
large (~10MB) to be uploaded reliably to COS for deployment.

This new fix made the worker script a separate chunk again, but with the
ability to be fetched across sites.
  • Loading branch information
syimyuzya committed Sep 8, 2024
1 parent 766e83c commit d206604
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
"alphabetize": { "order": "asc", "caseInsensitive": true }
}
],
"import/no-unresolved": ["error", { "ignore": ["\\?(?:worker(&inline)?|raw)$"] }]
"import/no-unresolved": ["error", { "ignore": ["\\?(?:worker(&inline|&url)?|raw)$"] }]
}
}
11 changes: 9 additions & 2 deletions src/editor/initialize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { languages } from "monaco-editor";
import TSWorker from "monaco-editor/esm/vs/language/typescript/ts.worker.js?worker&inline";
import tsWorkerUrl from "monaco-editor/esm/vs/language/typescript/ts.worker.js?worker&url";
import { SuggestAdapter } from "monaco-editor/esm/vs/language/typescript/tsMode";

// NOTE Workaround for cross-origin loading.
// See: https://github.com/vitejs/vite/issues/13680#issuecomment-1819274694
// Normally we could just import the worker like `import TSWorker from "...?worker";` and then `new TSWorker()`,
// but it doesn't work if the assets are deployed on a different site.
const tsWorkerWrapperScript = `import ${JSON.stringify(new URL(tsWorkerUrl, import.meta.url))}`;
const tsWorkerWrapperBlob = new Blob([tsWorkerWrapperScript], { type: "application/javascript" });

declare global {
interface Window {
MonacoEnvironment: {
Expand All @@ -12,7 +19,7 @@ declare global {

self.MonacoEnvironment = {
getWorker() {
return new TSWorker();
return new Worker(URL.createObjectURL(tsWorkerWrapperBlob), { type: "module" });
},
};

Expand Down
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function makeConfig(assetLocation = base) {
__APP_VERSION__: JSON.stringify(process.env.npm_package_version),
},
build: {
chunkSizeWarningLimit: 10240,
chunkSizeWarningLimit: 8192,
outDir: "build",
},
experimental: {
Expand Down

0 comments on commit d206604

Please sign in to comment.