Skip to content

Commit 6f3fbe8

Browse files
authored
Fixes worker sandbox problems (#4975)
1 parent a4d7907 commit 6f3fbe8

File tree

5 files changed

+114
-32
lines changed

5 files changed

+114
-32
lines changed

build/amd/plugin.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
/**
7+
* @type {() => import('rollup').Plugin}
8+
*/
9+
export function urlToEsmPlugin() {
10+
return {
11+
name: 'import-meta-url',
12+
async transform(code, id) {
13+
if (this.environment?.mode === 'dev') {
14+
return;
15+
}
16+
let idx = 0;
17+
18+
// Look for `new URL("...?worker", import.meta.url)` patterns.
19+
const regex = /new\s+URL\s*\(\s*(['"`])(.*?)\?worker\1\s*,\s*import\.meta\.url\s*\)?/g;
20+
21+
let match;
22+
let modified = false;
23+
let result = code;
24+
let offset = 0;
25+
/** @type {string[]} */
26+
const additionalImports = [];
27+
28+
while ((match = regex.exec(code)) !== null) {
29+
let path = match[2];
30+
31+
if (!path.startsWith('.') && !path.startsWith('/')) {
32+
path = `./${path}`;
33+
}
34+
35+
const start = match.index;
36+
const end = start + match[0].length;
37+
38+
const varName = `__worker_url_${idx++}__`;
39+
additionalImports.push(`import ${varName} from ${JSON.stringify(path + '?worker&url')};`);
40+
41+
const replacement = varName;
42+
43+
result = result.slice(0, start + offset) + replacement + result.slice(end + offset);
44+
offset += replacement.length - (end - start);
45+
modified = true;
46+
}
47+
48+
if (!modified) {
49+
return null;
50+
}
51+
52+
result = additionalImports.join('\n') + '\n' + result;
53+
54+
return {
55+
code: result,
56+
map: null
57+
};
58+
}
59+
};
60+
}

build/amd/src/editor.main.js

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,66 @@
1-
import * as require_ from 'require';
1+
import * as require from 'require';
22

33
self.MonacoEnvironment = {
44
getWorker: function (_moduleId, label) {
5-
const require = require_;
6-
if (!require) {
7-
label = label; // NOOP
8-
}
95
if (label === 'json') {
10-
return new Worker(new URL('../../../src/language/json/json.worker.ts', import.meta.url), {
11-
type: 'module'
12-
});
6+
return new Worker(
7+
getWorkerBootstrapUrl(
8+
new URL('../../../src/language/json/json.worker.ts?worker', import.meta.url)
9+
)
10+
);
1311
}
1412
if (label === 'css' || label === 'scss' || label === 'less') {
15-
return new Worker(new URL('../../../src/language/css/css.worker.ts', import.meta.url), {
16-
type: 'module'
17-
});
13+
return new Worker(
14+
getWorkerBootstrapUrl(
15+
new URL('../../../src/language/css/css.worker.ts?worker', import.meta.url)
16+
)
17+
);
1818
}
1919
if (label === 'html' || label === 'handlebars' || label === 'razor') {
20-
return new Worker(new URL('../../../src/language/html/html.worker.ts', import.meta.url), {
21-
type: 'module'
22-
});
20+
return new Worker(
21+
getWorkerBootstrapUrl(
22+
new URL('../../../src/language/html/html.worker.ts?worker', import.meta.url)
23+
)
24+
);
2325
}
2426
if (label === 'typescript' || label === 'javascript') {
25-
return new Worker(new URL('../../../src/language/typescript/ts.worker.ts', import.meta.url), {
26-
type: 'module'
27-
});
27+
return new Worker(
28+
getWorkerBootstrapUrl(
29+
new URL('../../../src/language/typescript/ts.worker.ts?worker', import.meta.url)
30+
)
31+
);
2832
}
29-
return new Worker(new URL('../../../src/editor/editor.worker.ts', import.meta.url), {
30-
type: 'module'
31-
});
33+
return new Worker(
34+
getWorkerBootstrapUrl(new URL('../../../src/editor/editor.worker.ts?worker', import.meta.url))
35+
);
3236
}
3337
};
3438

39+
function getWorkerBootstrapUrl(workerScriptUrl) {
40+
const blob = new Blob(
41+
[
42+
[
43+
`const ttPolicy = globalThis.trustedTypes?.createPolicy('defaultWorkerFactory', { createScriptURL: value => value });`,
44+
`globalThis.workerttPolicy = ttPolicy;`,
45+
`importScripts(ttPolicy?.createScriptURL(${JSON.stringify(
46+
workerScriptUrl
47+
)}) ?? ${JSON.stringify(workerScriptUrl)});`,
48+
`globalThis.postMessage({ type: 'vscode-worker-ready' });`
49+
].join('')
50+
],
51+
{ type: 'application/javascript' }
52+
);
53+
return URL.createObjectURL(blob);
54+
}
55+
3556
import 'vs/nls.messages-loader!';
3657
import '../../../src/basic-languages/monaco.contribution';
3758
import '../../../src/language/css/monaco.contribution';
3859
import '../../../src/language/html/monaco.contribution';
3960
import '../../../src/language/json/monaco.contribution';
4061
import '../../../src/language/typescript/monaco.contribution';
4162

42-
const styleSheetUrl = require_.toUrl('vs/style.css');
63+
const styleSheetUrl = require.toUrl('vs/style.css');
4364

4465
const link = document.createElement('link');
4566
link.rel = 'stylesheet';

build/amd/vite.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { glob } from 'node:fs/promises';
33
import { basename, dirname, join, resolve } from 'node:path';
44
import { fileURLToPath } from 'node:url';
55
import { defineConfig } from 'vite';
6+
import { urlToEsmPlugin } from './plugin';
67

78
const __dirname = dirname(fileURLToPath(import.meta.url));
89

@@ -77,7 +78,8 @@ export default defineConfig(async (args) => {
7778
source: readFileSync(resolve(__dirname, './src/loader.js'), 'utf-8')
7879
});
7980
}
80-
}
81+
},
82+
urlToEsmPlugin()
8183
]
8284
};
8385
});

package-lock.json

Lines changed: 8 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"jsdom": "^19.0.0",
5353
"jsonc-parser": "^3.0.0",
5454
"mocha": "^9.2.0",
55-
"monaco-editor-core": "^0.53.0-dev-20250828",
55+
"monaco-editor-core": "^0.53.0-dev-20250905",
5656
"parcel": "^2.7.0",
5757
"pin-github-action": "^1.8.0",
5858
"prettier": "^2.5.1",

0 commit comments

Comments
 (0)