mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 20:52:56 +01:00
Fixes worker sandbox problems (#4975)
This commit is contained in:
parent
a4d7907bd4
commit
6f3fbe8c3a
5 changed files with 114 additions and 32 deletions
60
build/amd/plugin.js
Normal file
60
build/amd/plugin.js
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @type {() => import('rollup').Plugin}
|
||||
*/
|
||||
export function urlToEsmPlugin() {
|
||||
return {
|
||||
name: 'import-meta-url',
|
||||
async transform(code, id) {
|
||||
if (this.environment?.mode === 'dev') {
|
||||
return;
|
||||
}
|
||||
let idx = 0;
|
||||
|
||||
// Look for `new URL("...?worker", import.meta.url)` patterns.
|
||||
const regex = /new\s+URL\s*\(\s*(['"`])(.*?)\?worker\1\s*,\s*import\.meta\.url\s*\)?/g;
|
||||
|
||||
let match;
|
||||
let modified = false;
|
||||
let result = code;
|
||||
let offset = 0;
|
||||
/** @type {string[]} */
|
||||
const additionalImports = [];
|
||||
|
||||
while ((match = regex.exec(code)) !== null) {
|
||||
let path = match[2];
|
||||
|
||||
if (!path.startsWith('.') && !path.startsWith('/')) {
|
||||
path = `./${path}`;
|
||||
}
|
||||
|
||||
const start = match.index;
|
||||
const end = start + match[0].length;
|
||||
|
||||
const varName = `__worker_url_${idx++}__`;
|
||||
additionalImports.push(`import ${varName} from ${JSON.stringify(path + '?worker&url')};`);
|
||||
|
||||
const replacement = varName;
|
||||
|
||||
result = result.slice(0, start + offset) + replacement + result.slice(end + offset);
|
||||
offset += replacement.length - (end - start);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!modified) {
|
||||
return null;
|
||||
}
|
||||
|
||||
result = additionalImports.join('\n') + '\n' + result;
|
||||
|
||||
return {
|
||||
code: result,
|
||||
map: null
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -1,37 +1,58 @@
|
|||
import * as require_ from 'require';
|
||||
import * as require from 'require';
|
||||
|
||||
self.MonacoEnvironment = {
|
||||
getWorker: function (_moduleId, label) {
|
||||
const require = require_;
|
||||
if (!require) {
|
||||
label = label; // NOOP
|
||||
}
|
||||
if (label === 'json') {
|
||||
return new Worker(new URL('../../../src/language/json/json.worker.ts', import.meta.url), {
|
||||
type: 'module'
|
||||
});
|
||||
return new Worker(
|
||||
getWorkerBootstrapUrl(
|
||||
new URL('../../../src/language/json/json.worker.ts?worker', import.meta.url)
|
||||
)
|
||||
);
|
||||
}
|
||||
if (label === 'css' || label === 'scss' || label === 'less') {
|
||||
return new Worker(new URL('../../../src/language/css/css.worker.ts', import.meta.url), {
|
||||
type: 'module'
|
||||
});
|
||||
return new Worker(
|
||||
getWorkerBootstrapUrl(
|
||||
new URL('../../../src/language/css/css.worker.ts?worker', import.meta.url)
|
||||
)
|
||||
);
|
||||
}
|
||||
if (label === 'html' || label === 'handlebars' || label === 'razor') {
|
||||
return new Worker(new URL('../../../src/language/html/html.worker.ts', import.meta.url), {
|
||||
type: 'module'
|
||||
});
|
||||
return new Worker(
|
||||
getWorkerBootstrapUrl(
|
||||
new URL('../../../src/language/html/html.worker.ts?worker', import.meta.url)
|
||||
)
|
||||
);
|
||||
}
|
||||
if (label === 'typescript' || label === 'javascript') {
|
||||
return new Worker(new URL('../../../src/language/typescript/ts.worker.ts', import.meta.url), {
|
||||
type: 'module'
|
||||
});
|
||||
return new Worker(
|
||||
getWorkerBootstrapUrl(
|
||||
new URL('../../../src/language/typescript/ts.worker.ts?worker', import.meta.url)
|
||||
)
|
||||
);
|
||||
}
|
||||
return new Worker(new URL('../../../src/editor/editor.worker.ts', import.meta.url), {
|
||||
type: 'module'
|
||||
});
|
||||
return new Worker(
|
||||
getWorkerBootstrapUrl(new URL('../../../src/editor/editor.worker.ts?worker', import.meta.url))
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
function getWorkerBootstrapUrl(workerScriptUrl) {
|
||||
const blob = new Blob(
|
||||
[
|
||||
[
|
||||
`const ttPolicy = globalThis.trustedTypes?.createPolicy('defaultWorkerFactory', { createScriptURL: value => value });`,
|
||||
`globalThis.workerttPolicy = ttPolicy;`,
|
||||
`importScripts(ttPolicy?.createScriptURL(${JSON.stringify(
|
||||
workerScriptUrl
|
||||
)}) ?? ${JSON.stringify(workerScriptUrl)});`,
|
||||
`globalThis.postMessage({ type: 'vscode-worker-ready' });`
|
||||
].join('')
|
||||
],
|
||||
{ type: 'application/javascript' }
|
||||
);
|
||||
return URL.createObjectURL(blob);
|
||||
}
|
||||
|
||||
import 'vs/nls.messages-loader!';
|
||||
import '../../../src/basic-languages/monaco.contribution';
|
||||
import '../../../src/language/css/monaco.contribution';
|
||||
|
|
@ -39,7 +60,7 @@ import '../../../src/language/html/monaco.contribution';
|
|||
import '../../../src/language/json/monaco.contribution';
|
||||
import '../../../src/language/typescript/monaco.contribution';
|
||||
|
||||
const styleSheetUrl = require_.toUrl('vs/style.css');
|
||||
const styleSheetUrl = require.toUrl('vs/style.css');
|
||||
|
||||
const link = document.createElement('link');
|
||||
link.rel = 'stylesheet';
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { glob } from 'node:fs/promises';
|
|||
import { basename, dirname, join, resolve } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { defineConfig } from 'vite';
|
||||
import { urlToEsmPlugin } from './plugin';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
|
|
@ -77,7 +78,8 @@ export default defineConfig(async (args) => {
|
|||
source: readFileSync(resolve(__dirname, './src/loader.js'), 'utf-8')
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
urlToEsmPlugin()
|
||||
]
|
||||
};
|
||||
});
|
||||
|
|
|
|||
17
package-lock.json
generated
17
package-lock.json
generated
|
|
@ -29,7 +29,7 @@
|
|||
"jsdom": "^19.0.0",
|
||||
"jsonc-parser": "^3.0.0",
|
||||
"mocha": "^9.2.0",
|
||||
"monaco-editor-core": "^0.53.0-dev-20250828",
|
||||
"monaco-editor-core": "^0.53.0-dev-20250905",
|
||||
"parcel": "^2.7.0",
|
||||
"pin-github-action": "^1.8.0",
|
||||
"prettier": "^2.5.1",
|
||||
|
|
@ -5496,11 +5496,10 @@
|
|||
"dev": true
|
||||
},
|
||||
"node_modules/monaco-editor-core": {
|
||||
"version": "0.53.0-dev-20250828",
|
||||
"resolved": "https://registry.npmjs.org/monaco-editor-core/-/monaco-editor-core-0.53.0-dev-20250828.tgz",
|
||||
"integrity": "sha512-KDlcU32Fe+stH/FSInNbcGdE0Vi9INYwH0I6veTc2IC9xuiLL9JsmYQ0zLIeFXjfYEtsQWyj0BJYixxCV4GeLg==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
"version": "0.53.0-dev-20250905",
|
||||
"resolved": "https://registry.npmjs.org/monaco-editor-core/-/monaco-editor-core-0.53.0-dev-20250905.tgz",
|
||||
"integrity": "sha512-naSEvkM5l8ubpYwBW69tb4SAQg8bzG1EduXSXdidgnqk6SB7c5n6hSUlVJvRktJHjuaoUMfwA0+ppiUb5F05jg==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/mri": {
|
||||
"version": "1.2.0",
|
||||
|
|
@ -11295,9 +11294,9 @@
|
|||
}
|
||||
},
|
||||
"monaco-editor-core": {
|
||||
"version": "0.53.0-dev-20250828",
|
||||
"resolved": "https://registry.npmjs.org/monaco-editor-core/-/monaco-editor-core-0.53.0-dev-20250828.tgz",
|
||||
"integrity": "sha512-KDlcU32Fe+stH/FSInNbcGdE0Vi9INYwH0I6veTc2IC9xuiLL9JsmYQ0zLIeFXjfYEtsQWyj0BJYixxCV4GeLg==",
|
||||
"version": "0.53.0-dev-20250905",
|
||||
"resolved": "https://registry.npmjs.org/monaco-editor-core/-/monaco-editor-core-0.53.0-dev-20250905.tgz",
|
||||
"integrity": "sha512-naSEvkM5l8ubpYwBW69tb4SAQg8bzG1EduXSXdidgnqk6SB7c5n6hSUlVJvRktJHjuaoUMfwA0+ppiUb5F05jg==",
|
||||
"dev": true
|
||||
},
|
||||
"mri": {
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@
|
|||
"jsdom": "^19.0.0",
|
||||
"jsonc-parser": "^3.0.0",
|
||||
"mocha": "^9.2.0",
|
||||
"monaco-editor-core": "^0.53.0-dev-20250828",
|
||||
"monaco-editor-core": "^0.53.0-dev-20250905",
|
||||
"parcel": "^2.7.0",
|
||||
"pin-github-action": "^1.8.0",
|
||||
"prettier": "^2.5.1",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue