Skip to content

Commit d84acb4

Browse files
authored
Cleans up build scripts (#5097)
* Cleans up build scripts * Adds back removeDir
1 parent abae948 commit d84acb4

File tree

8 files changed

+199
-171
lines changed

8 files changed

+199
-171
lines changed

build/amd/rollup-types.config.mjs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,22 @@ import nodeResolve from '@rollup/plugin-node-resolve';
99
import { join } from 'path';
1010
import { defineConfig } from 'rollup';
1111
import { dts } from 'rollup-plugin-dts';
12-
13-
const root = join(import.meta.dirname, '../../');
14-
const outDir = join(import.meta.dirname, './out');
15-
16-
/**
17-
* @param {string} filePath
18-
* @param {string} newExt
19-
*/
20-
function changeExt(filePath, newExt) {
21-
const idx = filePath.lastIndexOf('.');
22-
if (idx === -1) {
23-
return filePath + newExt;
24-
} else {
25-
return filePath.substring(0, idx) + newExt;
26-
}
27-
}
28-
29-
const mappedPaths = {
30-
[join(root, 'node_modules/monaco-editor-core/esm/')]: '.',
31-
[join(root, 'node_modules/')]: 'external/',
32-
[join(root, 'src/')]: 'vs/'
33-
};
12+
import { dtsDeprecationWarning, mapModuleId } from '../shared.mjs';
3413

3514
export default defineConfig({
3615
input: {
3716
types: join(import.meta.dirname, './src/types.ts')
3817
},
3918
output: {
40-
dir: outDir,
19+
dir: join(import.meta.dirname, './out'),
4120
format: 'es',
4221
preserveModules: false,
4322
entryFileNames: function (chunkInfo) {
4423
const moduleId = chunkInfo.facadeModuleId;
4524
if (moduleId) {
46-
for (const [key, val] of Object.entries(mappedPaths)) {
47-
if (moduleId.startsWith(key)) {
48-
const relativePath = moduleId.substring(key.length);
49-
return changeExt(join(val, relativePath), '.d.ts');
50-
}
25+
const m = mapModuleId(moduleId, '.d.ts');
26+
if (m !== undefined) {
27+
return m;
5128
}
5229
}
5330
return '[name].d.ts';
@@ -61,6 +38,7 @@ export default defineConfig({
6138
stripInternal: true
6239
},
6340
includeExternal: ['monaco-editor-core', '@vscode/monaco-lsp-client']
64-
})
41+
}),
42+
dtsDeprecationWarning(),
6543
]
6644
});

build/amd/vite.config.js renamed to build/amd/vite.config.mjs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
import { readFileSync } from 'node:fs';
2-
import { glob } from 'node:fs/promises';
3-
import { basename, dirname, join, resolve } from 'node:path';
2+
import { dirname, resolve } from 'node:path';
43
import { fileURLToPath } from 'node:url';
54
import { defineConfig } from 'vite';
65
import { urlToEsmPlugin } from './plugin';
6+
import { getNlsEntryPoints } from '../shared.mjs';
77

88
const __dirname = dirname(fileURLToPath(import.meta.url));
99

1010
export default defineConfig(async (args) => {
11-
const monacoEditorCoreDir = join(
12-
dirname(require.resolve('monaco-editor-core/package.json')),
13-
'esm'
14-
);
15-
const nlsEntries = {};
16-
for await (const path of glob(`${monacoEditorCoreDir}/nls.messages.*.js`)) {
17-
const entryName = basename(path).replace('.js', '');
18-
nlsEntries[entryName] = path;
19-
}
20-
2111
/** @type {import('vite').UserConfig} */
2212
return {
2313
base: './',
@@ -28,7 +18,7 @@ export default defineConfig(async (args) => {
2818
lib: {
2919
cssFileName: 'editor/editor.main',
3020
entry: {
31-
...nlsEntries,
21+
...getNlsEntryPoints(),
3222
'nls.messages-loader': resolve(__dirname, 'src/nls.messages-loader.js'),
3323
'editor/editor.main': resolve(__dirname, 'src/editor.main.ts'),
3424
'basic-languages/monaco.contribution': resolve(

build/build-monaco-editor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import fs = require('fs');
88
import { REPO_ROOT, readFiles, writeFiles } from '../build/utils';
99
import { generateEsmMetadataJsAndDTs } from './releaseMetadata';
1010
import { buildESM } from './esm/build.script';
11-
import { removeDir } from './fs';
1211
import { buildAmdMinDev } from './amd/build.script';
12+
import { rm } from 'fs/promises';
1313

1414
async function run() {
15-
removeDir(`out/monaco-editor`);
15+
await rm(path.join(REPO_ROOT, './out/monaco-editor'), { recursive: true, force: true });
1616

1717
await buildESM();
1818
await buildAmdMinDev();

build/esm/rollup-types.config.mjs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,24 @@ import nodeResolve from '@rollup/plugin-node-resolve';
99
import { join } from 'path';
1010
import { defineConfig } from 'rollup';
1111
import { dts } from "rollup-plugin-dts";
12+
import { dtsDeprecationWarning, mapModuleId } from '../shared.mjs';
1213

1314
const root = join(import.meta.dirname, '../../');
14-
const outDir = join(root, './out/monaco-editor/esm');
15-
16-
/**
17-
* @param {string} filePath
18-
* @param {string} newExt
19-
* @returns {string}
20-
*/
21-
function changeExt(filePath, newExt) {
22-
const idx = filePath.lastIndexOf('.');
23-
if (idx === -1) {
24-
return filePath + newExt;
25-
} else {
26-
return filePath.substring(0, idx) + newExt;
27-
}
28-
}
29-
30-
const mappedPaths = {
31-
[join(root, 'node_modules/monaco-editor-core/esm/')]: '.',
32-
[join(root, 'node_modules/')]: 'external/',
33-
[join(root, 'src/')]: 'vs/',
34-
};
3515

3616
export default defineConfig({
3717
input: {
3818
entry: join(root, './src/editor/editor.main.ts'),
3919
},
4020
output: {
41-
dir: outDir,
21+
dir: join(root, './out/monaco-editor/esm'),
4222
format: 'es',
4323
preserveModules: false,
4424
entryFileNames: function (chunkInfo) {
4525
const moduleId = chunkInfo.facadeModuleId;
4626
if (moduleId) {
47-
for (const [key, val] of Object.entries(mappedPaths)) {
48-
if (moduleId.startsWith(key)) {
49-
const relativePath = moduleId.substring(key.length);
50-
return changeExt(join(val, relativePath), '.d.ts');
51-
}
27+
const m = mapModuleId(moduleId, '.d.ts');
28+
if (m !== undefined) {
29+
return m;
5230
}
5331
}
5432
return '[name].d.ts';
@@ -63,5 +41,6 @@ export default defineConfig({
6341
},
6442
includeExternal: ['monaco-editor-core', '@vscode/monaco-lsp-client']
6543
}),
44+
dtsDeprecationWarning(),
6645
],
6746
});

build/esm/rollup.config.mjs

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,13 @@ import del from 'rollup-plugin-delete';
1313
import keepCssImports from './rollup-plugin-keep-css-imports/dist/index.mjs';
1414
import nodeResolve from '@rollup/plugin-node-resolve';
1515
import { urlToEsmPlugin } from './rollup-url-to-module-plugin/index.mjs';
16-
import { copyFileSync, mkdirSync } from 'fs';
17-
import { dirname } from 'path';
18-
import { fileURLToPath } from 'url';
19-
import { readdirSync } from 'fs';
16+
import { getNlsEntryPoints, mapModuleId } from '../shared.mjs';
17+
import { readFileSync } from 'fs';
18+
2019

2120
const root = join(import.meta.dirname, '../../');
2221
const outDir = join(root, './out/monaco-editor/esm');
2322

24-
/**
25-
* @param {string} filePath
26-
* @param {string} newExt
27-
* @returns {string}
28-
*/
29-
function changeExt(filePath, newExt) {
30-
const idx = filePath.lastIndexOf('.');
31-
if (idx === -1) {
32-
return filePath + newExt;
33-
} else {
34-
return filePath.substring(0, idx) + newExt;
35-
}
36-
}
37-
38-
const mappedPaths = {
39-
[join(root, 'node_modules/monaco-editor-core/esm/')]: '.',
40-
[join(root, 'node_modules/')]: 'external/',
41-
[join(root, 'monaco-lsp-client/')]: 'external/monaco-lsp-client/',
42-
[join(root, 'src/')]: 'vs/',
43-
};
44-
45-
function getNlsEntryPoints() {
46-
// Scan for nls.messages.*.js files dynamically
47-
const nlsDir = dirname(fileURLToPath(import.meta.resolve('monaco-editor-core/esm/nls.messages.en.js')));
48-
const nlsFiles = readdirSync(nlsDir)
49-
.filter(file => file.startsWith('nls.messages.') && file.endsWith('.js'))
50-
.reduce((acc, file) => {
51-
// @ts-ignore
52-
acc[file] = join(nlsDir, file);
53-
return acc;
54-
}, {});
55-
return nlsFiles;
56-
}
57-
5823
export default defineConfig({
5924
input: {
6025
entry: join(root, './src/editor/editor.main.ts'),
@@ -72,11 +37,9 @@ export default defineConfig({
7237
entryFileNames: function (chunkInfo) {
7338
const moduleId = chunkInfo.facadeModuleId;
7439
if (moduleId) {
75-
for (const [key, val] of Object.entries(mappedPaths)) {
76-
if (moduleId.startsWith(key)) {
77-
const relativePath = moduleId.substring(key.length);
78-
return changeExt(join(val, relativePath), '.js');
79-
}
40+
const r = mapModuleId(moduleId, '.js');
41+
if (r !== undefined) {
42+
return r;
8043
}
8144
}
8245
return '[name].js';
@@ -90,11 +53,12 @@ export default defineConfig({
9053

9154
{
9255
name: 'copy-codicon-font',
93-
buildEnd() {
94-
const codiconSource = join(root, 'node_modules/monaco-editor-core/esm/vs/base/browser/ui/codicons/codicon/codicon.ttf');
95-
const codiconDest = join(outDir, 'vs/base/browser/ui/codicons/codicon/codicon.ttf');
96-
mkdirSync(dirname(codiconDest), { recursive: true });
97-
copyFileSync(codiconSource, codiconDest);
56+
generateBundle() {
57+
this.emitFile({
58+
type: 'asset',
59+
fileName: 'vs/base/browser/ui/codicons/codicon/codicon.ttf',
60+
source: readFileSync(join(root, 'node_modules/monaco-editor-core/esm/vs/base/browser/ui/codicons/codicon/codicon.ttf'))
61+
});
9862
}
9963
},
10064

@@ -106,13 +70,10 @@ export default defineConfig({
10670
* @param {string} assetId
10771
*/
10872
outputPath: (assetId) => {
109-
for (const [key, val] of Object.entries(mappedPaths)) {
110-
if (assetId.startsWith(key)) {
111-
const relativePath = assetId.substring(key.length);
112-
return changeExt(join(outDir, val, relativePath), '.css');
113-
}
73+
const r = mapModuleId(assetId, '.css');
74+
if (r !== undefined) {
75+
return join(outDir, r);
11476
}
115-
11677
const relativePath = join(outDir, relative(root, assetId));
11778
return relativePath.replace(/(\.s[ca]ss)$/, ".min$1")
11879
},

build/fs.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,12 @@ export function ensureDir(dirname: string) {
2323
if (!existingDirCache.has(dir)) {
2424
try {
2525
fs.mkdirSync(dir);
26-
} catch (err) {}
26+
} catch (err) { }
2727
existingDirCache.add(dir);
2828
}
2929
});
3030
}
3131

32-
/**
33-
* Copy a file.
34-
*/
35-
export function copyFile(_source: string, _destination: string) {
36-
const source = path.join(REPO_ROOT, _source);
37-
const destination = path.join(REPO_ROOT, _destination);
38-
39-
ensureDir(path.dirname(destination));
40-
fs.writeFileSync(destination, fs.readFileSync(source));
41-
42-
console.log(`Copied ${_source} to ${_destination}`);
43-
}
44-
4532
/**
4633
* Remove a directory and all its contents.
4734
*/

build/shared.mjs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
// @ts-check
7+
8+
import { dirname, join } from 'path';
9+
import { fileURLToPath } from 'url';
10+
import { readdirSync } from 'fs';
11+
12+
/**
13+
* @param {string} filePath
14+
* @param {string} newExt
15+
*/
16+
export function changeExt(filePath, newExt) {
17+
const idx = filePath.lastIndexOf('.');
18+
if (idx === -1) {
19+
return filePath + newExt;
20+
} else {
21+
return filePath.substring(0, idx) + newExt;
22+
}
23+
}
24+
25+
export function getNlsEntryPoints() {
26+
const nlsDir = dirname(fileURLToPath(import.meta.resolve('monaco-editor-core/esm/nls.messages.en.js')));
27+
const nlsFiles = readdirSync(nlsDir)
28+
.filter(file => file.startsWith('nls.messages.') && file.endsWith('.js'))
29+
.reduce((acc, file) => {
30+
// @ts-ignore
31+
acc[file] = join(nlsDir, file);
32+
return acc;
33+
}, {});
34+
return nlsFiles;
35+
}
36+
37+
const root = join(import.meta.dirname, '../');
38+
39+
const mappedPaths = {
40+
[join(root, 'node_modules/monaco-editor-core/esm/')]: '.',
41+
[join(root, 'node_modules/')]: 'external/',
42+
[join(root, 'monaco-lsp-client/')]: 'external/monaco-lsp-client/',
43+
[join(root, 'src/')]: 'vs/',
44+
};
45+
46+
/**
47+
* @param {string} moduleId
48+
* @param {string} newExt (with leading .)
49+
* @returns {string | undefined}
50+
*/
51+
export function mapModuleId(moduleId, newExt) {
52+
for (const [key, val] of Object.entries(mappedPaths)) {
53+
if (moduleId.startsWith(key)) {
54+
const relativePath = moduleId.substring(key.length);
55+
return changeExt(join(val, relativePath), newExt);
56+
}
57+
}
58+
return undefined;
59+
}
60+
61+
/** @return {import('rollup').Plugin} */
62+
export function dtsDeprecationWarning() {
63+
return {
64+
name: 'add-dts-deprecation-warning',
65+
generateBundle(options, bundle) {
66+
for (const fileName in bundle) {
67+
const file = bundle[fileName];
68+
if (file.type === 'chunk' && fileName.endsWith('.d.ts')) {
69+
let content = file.code.toString();
70+
content = content + `
71+
declare namespace languages {
72+
/** @deprecated Use the new top level "css" namespace instead. */
73+
export const css: { deprecated: true };
74+
75+
/** @deprecated Use the new top level "html" namespace instead. */
76+
export const html: { deprecated: true };
77+
78+
/** @deprecated Use the new top level "json" namespace instead. */
79+
export const json: { deprecated: true };
80+
81+
/** @deprecated Use the new top level "typescript" namespace instead. */
82+
export const typescript: { deprecated: true };
83+
}
84+
`;
85+
file.code = content;
86+
}
87+
}
88+
}
89+
};
90+
}

0 commit comments

Comments
 (0)