Skip to content

Commit a19dd88

Browse files
committed
first version of monaco esm version package
1 parent ebb3d69 commit a19dd88

5 files changed

Lines changed: 701 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,6 @@ FodyWeavers.xsd
416416
*.msix
417417
*.msm
418418
*.msp
419+
420+
/esm
421+
/min

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# monaco-esm
1+
# monaco-editor-esm
22
Real ESM version of vscode monaco editor

_patchAndMinifyMonaco.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import * as esbuild from 'esbuild';
2+
import fs from 'fs';
3+
import path from 'path';
4+
5+
const target = './esm';
6+
const monacoEsm = './node_modules/monaco-editor/esm';
7+
const outDir = `${monacoEsm}/vs/editor`;
8+
const absOutDir = path.resolve(outDir);
9+
10+
// --- Patch: remove CSS imports (esbuild can't handle them) ---
11+
function walkDir(dir, callback) {
12+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
13+
const full = path.join(dir, entry.name);
14+
if (entry.isDirectory()) {
15+
walkDir(full, callback);
16+
} else if (entry.isFile() && full.endsWith('.js')) {
17+
callback(full);
18+
}
19+
}
20+
}
21+
22+
function removeCssImports(filePath) {
23+
const original = fs.readFileSync(filePath, 'utf8');
24+
const content = original.replace(/^\s*import\s+[^;]*['"]([^'"]+\.css)['"]\s*;?\s*$/gm, '');
25+
if (content !== original) {
26+
fs.writeFileSync(filePath, content, 'utf8');
27+
console.log('Cleaned:', filePath);
28+
}
29+
}
30+
31+
function copyFolderRecursive(source, target) {
32+
// Create target folder if it does not exist
33+
if (!fs.existsSync(target)) {
34+
fs.mkdirSync(target, { recursive: true });
35+
}
36+
37+
const entries = fs.readdirSync(source, { withFileTypes: true });
38+
39+
for (const entry of entries) {
40+
const sourcePath = path.join(source, entry.name);
41+
const targetPath = path.join(target, entry.name);
42+
43+
if (entry.isDirectory()) {
44+
copyFolderRecursive(sourcePath, targetPath);
45+
} else if (entry.isFile()) {
46+
fs.copyFileSync(sourcePath, targetPath);
47+
}
48+
}
49+
}
50+
51+
if (fs.existsSync(target)) {
52+
fs.rmdirSync(target, { recursive: true, force: true });
53+
}
54+
if (fs.existsSync('./min')) {
55+
fs.rmdirSync('./min', { recursive: true, force: true });
56+
}
57+
copyFolderRecursive(monacoEsm, target);
58+
fs.mkdirSync('./min/vs/editor', { recursive: true });
59+
fs.copyFileSync('./node_modules/monaco-editor/min/vs/editor/editor.main.css', `./min/vs/editor/editor.main.css`);
60+
61+
walkDir(path.resolve(target), removeCssImports);
62+
fs.rmSync('./node_modules/monaco-editor/dev', { recursive: true, force: true });
63+
64+
// --- Patch: fix shadow DOM mouse event handling ---
65+
const mouseHandlerPath = `${target}/vs/editor/browser/controller/mouseHandler.js`;
66+
const mouseHandlerCode = fs.readFileSync(mouseHandlerPath, 'utf8');
67+
const patchedMouseHandler = mouseHandlerCode.replace(
68+
/this\.viewHelper\.viewDomNode\.contains\(e\.target\)/,
69+
'this.viewHelper.viewDomNode.contains(e.composedPath()[0])'
70+
);
71+
if (patchedMouseHandler !== mouseHandlerCode) {
72+
fs.writeFileSync(mouseHandlerPath, patchedMouseHandler);
73+
console.log('Patched monaco editor mouseHandler');
74+
}
75+
76+
// --- Bundle: main editor ---
77+
await esbuild.build({
78+
entryPoints: [`${target}/vs/editor/editor.main.js`],
79+
outdir: outDir,
80+
entryNames: 'editor.main.min',
81+
bundle: true,
82+
minify: true,
83+
splitting: true,
84+
format: 'esm',
85+
platform: 'neutral',
86+
external: ['dompurify'],
87+
}).catch(() => process.exit(1));
88+
89+
// --- Bundle: language workers ---
90+
// Find all *.worker.js files outside outDir and bundle them there so that
91+
// import.meta.url-relative paths in the split chunks resolve correctly.
92+
function findWorkers(dir) {
93+
const workers = [];
94+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
95+
const full = path.join(dir, entry.name);
96+
if (entry.isDirectory()) {
97+
workers.push(...findWorkers(full));
98+
} else if (entry.isFile() && entry.name.endsWith('.worker.js') && path.resolve(dir) !== absOutDir) {
99+
workers.push(full);
100+
}
101+
}
102+
return workers;
103+
}
104+
105+
const workerFiles = findWorkers(target);
106+
console.log(`Bundling ${workerFiles.length} worker(s) into ${outDir}`);
107+
108+
for (const workerFile of workerFiles) {
109+
const name = path.basename(workerFile, '.js');
110+
await esbuild.build({
111+
entryPoints: [workerFile],
112+
outdir: outDir,
113+
entryNames: name,
114+
bundle: true,
115+
format: 'esm',
116+
platform: 'neutral',
117+
external: ['dompurify'],
118+
allowOverwrite: true,
119+
}).catch(() => process.exit(1));
120+
}

0 commit comments

Comments
 (0)