mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 08:10:11 +01:00
Uses rollup for ESM build and d.ts bundling. (#5048)
* Uses rollup for ESM build and d.ts bundling.
Moves monaco.languages.{typescript, json, html, css} to monaco.*.
Moves monaco.editor.createWebWorker to monaco.createWebWorker.
* Adds excluded files from dist, as they needed to be patched.
This commit is contained in:
parent
0fd6f29a23
commit
5a7e917587
39 changed files with 1795 additions and 712 deletions
170
build/utils.ts
170
build/utils.ts
|
|
@ -5,181 +5,11 @@
|
|||
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as cp from 'child_process';
|
||||
import * as esbuild from 'esbuild';
|
||||
import * as glob from 'glob';
|
||||
import { ensureDir } from './fs';
|
||||
|
||||
export const REPO_ROOT = path.join(__dirname, '../');
|
||||
|
||||
/**
|
||||
* Launch the typescript compiler synchronously over a project.
|
||||
*/
|
||||
export function runTsc(_projectPath: string) {
|
||||
const projectPath = path.join(REPO_ROOT, _projectPath);
|
||||
console.log(`Launching compiler at ${_projectPath}...`);
|
||||
const res = cp.spawnSync(
|
||||
process.execPath,
|
||||
[path.join(__dirname, '../node_modules/typescript/lib/tsc.js'), '-p', projectPath],
|
||||
{ stdio: 'inherit' }
|
||||
);
|
||||
console.log(`Compiled ${_projectPath}`);
|
||||
if (res.status !== 0) {
|
||||
process.exit(res.status);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Launch prettier on a specific file.
|
||||
*/
|
||||
export function prettier(_filePath: string) {
|
||||
const filePath = path.join(REPO_ROOT, _filePath);
|
||||
cp.spawnSync(
|
||||
process.execPath,
|
||||
[path.join(__dirname, '../node_modules/prettier/bin-prettier.js'), '--write', filePath],
|
||||
{ stdio: 'inherit' }
|
||||
);
|
||||
|
||||
console.log(`Ran prettier over ${_filePath}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform an external .d.ts file to an internal .d.ts file
|
||||
*/
|
||||
export function massageAndCopyDts(source: string, destination: string, namespace: string) {
|
||||
const absoluteSource = path.join(REPO_ROOT, source);
|
||||
const absoluteDestination = path.join(REPO_ROOT, destination);
|
||||
|
||||
const lines = fs
|
||||
.readFileSync(absoluteSource)
|
||||
.toString()
|
||||
.split(/\r\n|\r|\n/);
|
||||
|
||||
let result = [
|
||||
`/*---------------------------------------------------------------------------------------------`,
|
||||
` * Copyright (c) Microsoft Corporation. All rights reserved.`,
|
||||
` * Licensed under the MIT License. See License.txt in the project root for license information.`,
|
||||
` *--------------------------------------------------------------------------------------------*/`,
|
||||
``,
|
||||
`declare namespace ${namespace} {`
|
||||
];
|
||||
for (let line of lines) {
|
||||
if (/^import/.test(line)) {
|
||||
continue;
|
||||
}
|
||||
if (line === 'export {};') {
|
||||
continue;
|
||||
}
|
||||
line = line.replace(/ /g, '\t');
|
||||
line = line.replace(/declare /g, '');
|
||||
if (line.length > 0) {
|
||||
line = `\t${line}`;
|
||||
result.push(line);
|
||||
}
|
||||
}
|
||||
result.push(`}`);
|
||||
result.push(``);
|
||||
|
||||
ensureDir(path.dirname(absoluteDestination));
|
||||
fs.writeFileSync(absoluteDestination, result.join('\n'));
|
||||
|
||||
prettier(destination);
|
||||
}
|
||||
|
||||
export function build(options: import('esbuild').BuildOptions) {
|
||||
esbuild.build(options).then((result) => {
|
||||
if (result.errors.length > 0) {
|
||||
console.error(result.errors);
|
||||
}
|
||||
if (result.warnings.length > 0) {
|
||||
console.error(result.warnings);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function buildESM(options: { base: string; entryPoints: string[]; external: string[] }) {
|
||||
build({
|
||||
entryPoints: options.entryPoints,
|
||||
bundle: true,
|
||||
target: 'esnext',
|
||||
format: 'esm',
|
||||
drop: ['debugger'],
|
||||
banner: {
|
||||
js: bundledFileHeader
|
||||
},
|
||||
external: options.external,
|
||||
outbase: `src/${options.base}`,
|
||||
outdir: `out/languages/bundled/esm/vs/${options.base}/`
|
||||
});
|
||||
}
|
||||
|
||||
function getGitVersion() {
|
||||
const git = path.join(REPO_ROOT, '.git');
|
||||
const headPath = path.join(git, 'HEAD');
|
||||
let head;
|
||||
|
||||
try {
|
||||
head = fs.readFileSync(headPath, 'utf8').trim();
|
||||
} catch (e) {
|
||||
return void 0;
|
||||
}
|
||||
|
||||
if (/^[0-9a-f]{40}$/i.test(head)) {
|
||||
return head;
|
||||
}
|
||||
|
||||
const refMatch = /^ref: (.*)$/.exec(head);
|
||||
|
||||
if (!refMatch) {
|
||||
return void 0;
|
||||
}
|
||||
|
||||
const ref = refMatch[1];
|
||||
const refPath = path.join(git, ref);
|
||||
|
||||
try {
|
||||
return fs.readFileSync(refPath, 'utf8').trim();
|
||||
} catch (e) {
|
||||
// noop
|
||||
}
|
||||
|
||||
const packedRefsPath = path.join(git, 'packed-refs');
|
||||
let refsRaw;
|
||||
|
||||
try {
|
||||
refsRaw = fs.readFileSync(packedRefsPath, 'utf8').trim();
|
||||
} catch (e) {
|
||||
return void 0;
|
||||
}
|
||||
|
||||
const refsRegex = /^([0-9a-f]{40})\s+(.+)$/gm;
|
||||
let refsMatch;
|
||||
const refs = {};
|
||||
|
||||
while ((refsMatch = refsRegex.exec(refsRaw))) {
|
||||
refs[refsMatch[2]] = refsMatch[1];
|
||||
}
|
||||
|
||||
return refs[ref];
|
||||
}
|
||||
|
||||
export const bundledFileHeader = (() => {
|
||||
const sha1 = getGitVersion();
|
||||
const semver = require('../package.json').version;
|
||||
const headerVersion = semver + '(' + sha1 + ')';
|
||||
|
||||
const BUNDLED_FILE_HEADER = [
|
||||
'/*!-----------------------------------------------------------------------------',
|
||||
' * Copyright (c) Microsoft Corporation. All rights reserved.',
|
||||
' * Version: ' + headerVersion,
|
||||
' * Released under the MIT license',
|
||||
' * https://github.com/microsoft/monaco-editor/blob/main/LICENSE.txt',
|
||||
' *-----------------------------------------------------------------------------*/',
|
||||
''
|
||||
].join('\n');
|
||||
|
||||
return BUNDLED_FILE_HEADER;
|
||||
})();
|
||||
|
||||
export interface IFile {
|
||||
path: string;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue