From 798e312604a23b5984b332bcf84a44f0588a23c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E5=A3=B0?= Date: Thu, 28 Mar 2024 07:21:01 +0000 Subject: [PATCH] Use gulp to build esm version --- build/gulpfile.editor.js | 5 ++++- build/lib/compilation.js | 15 ++++++++++++++- build/lib/compilation.ts | 20 +++++++++++++++++--- build/lib/standalone.js | 15 ++++++++++++++- build/lib/standalone.ts | 16 +++++++++++++++- 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/build/gulpfile.editor.js b/build/gulpfile.editor.js index 968f224634e..ef3da446e15 100644 --- a/build/gulpfile.editor.js +++ b/build/gulpfile.editor.js @@ -88,6 +88,9 @@ const extractEditorSrcTask = task.define('extract-editor-src', () => { // Disable mangling for the editor, as it complicates debugging & quite a few users rely on private/protected fields. const compileEditorAMDTask = task.define('compile-editor-amd', compilation.compileTask('out-editor-src', 'out-editor-build', true, { disableMangle: true })); +const compileEditorESMTaskPipeline = task.define('compile-editor-esm', compilation.compileTask('out-editor-esm', 'out-monaco-editor-core/esm', true, { disableMangle: true, +moduleKind: 1 /** CommonJS */, transformConstEnum: 1 })); + const optimizeEditorAMDTask = task.define('optimize-editor-amd', optimize.optimizeTask( { out: 'out-editor', @@ -425,7 +428,7 @@ gulp.task('editor-distro', ), task.series( createESMSourcesAndResourcesTask, - compileEditorESMTask, + compileEditorESMTaskPipeline, appendJSToESMImportsTask, ) ), diff --git a/build/lib/compilation.js b/build/lib/compilation.js index 35bc464d34a..ef04780fe7c 100644 --- a/build/lib/compilation.js +++ b/build/lib/compilation.js @@ -37,13 +37,17 @@ function getTypeScriptCompilerOptions(src) { options.newLine = /\r\n/.test(fs.readFileSync(__filename, 'utf8')) ? 0 : 1; return options; } -function createCompile(src, build, emitError, transpileOnly) { +function createCompile(src, build, emitError, transpileOnly, moduleKind) { const tsb = require('./tsb'); const sourcemaps = require('gulp-sourcemaps'); const projectPath = path.join(__dirname, '../../', src, 'tsconfig.json'); const overrideOptions = { ...getTypeScriptCompilerOptions(src), inlineSources: Boolean(build) }; + if (moduleKind) { + overrideOptions.module = moduleKind; + } if (!build) { overrideOptions.inlineSourceMap = true; + overrideOptions.noEmitOnError = false; } const compilation = tsb.create(projectPath, overrideOptions, { verbose: false, @@ -132,6 +136,7 @@ function compileTask(src, out, build, options = {}) { .pipe(mangleStream) .pipe(generator.stream) .pipe(compile()) + .pipe(options?.transformConstEnum ? transformConstEnum() : es.through()) .pipe(gulp.dest(out)); }; task.taskName = `compile-${path.basename(src)}`; @@ -291,4 +296,12 @@ exports.watchApiProposalNamesTask = task.define('watch-api-proposal-names', () = .pipe(util.debounce(task)) .pipe(gulp.dest('src')); }); +function transformConstEnum() { + return es.map((file, cb) => { + if (/\.ts$/.test(file.path)) { + file.contents = Buffer.from(file.contents.toString().replace(/const enum/g, 'enum')); + } + cb(null, file); + }); +} //# sourceMappingURL=compilation.js.map \ No newline at end of file diff --git a/build/lib/compilation.ts b/build/lib/compilation.ts index 94bfe6e832d..5f3b93aa89e 100644 --- a/build/lib/compilation.ts +++ b/build/lib/compilation.ts @@ -41,15 +41,19 @@ function getTypeScriptCompilerOptions(src: string): ts.CompilerOptions { return options; } -function createCompile(src: string, build: boolean, emitError: boolean, transpileOnly: boolean | { swc: boolean }) { +function createCompile(src: string, build: boolean, emitError: boolean, transpileOnly: boolean | { swc: boolean }, moduleKind?: ts.ModuleKind) { const tsb = require('./tsb') as typeof import('./tsb'); const sourcemaps = require('gulp-sourcemaps') as typeof import('gulp-sourcemaps'); - const projectPath = path.join(__dirname, '../../', src, 'tsconfig.json'); const overrideOptions = { ...getTypeScriptCompilerOptions(src), inlineSources: Boolean(build) }; + + if (moduleKind) { + overrideOptions.module = moduleKind; + } if (!build) { overrideOptions.inlineSourceMap = true; + overrideOptions.noEmitOnError = false; } const compilation = tsb.create(projectPath, overrideOptions, { @@ -114,7 +118,7 @@ export function transpileTask(src: string, out: string, swc: boolean): task.Stre return task; } -export function compileTask(src: string, out: string, build: boolean, options: { disableMangle?: boolean } = {}): task.StreamTask { +export function compileTask(src: string, out: string, build: boolean, options: { disableMangle?: boolean; transformConstEnum?: boolean; moduleKind?: ts.ModuleKind } = {}): task.StreamTask { const task = () => { @@ -156,6 +160,7 @@ export function compileTask(src: string, out: string, build: boolean, options: { .pipe(mangleStream) .pipe(generator.stream) .pipe(compile()) + .pipe(options?.transformConstEnum ? transformConstEnum() : es.through()) .pipe(gulp.dest(out)); }; @@ -340,3 +345,12 @@ export const watchApiProposalNamesTask = task.define('watch-api-proposal-names', .pipe(util.debounce(task)) .pipe(gulp.dest('src')); }); + +function transformConstEnum() { + return es.map((file: File, cb: any) => { + if (/\.ts$/.test(file.path)) { + file.contents = Buffer.from(file.contents.toString().replace(/const enum/g, 'enum')); + } + cb(null, file); + }); +} diff --git a/build/lib/standalone.js b/build/lib/standalone.js index 6cf416393d3..3a42ee2fd7c 100644 --- a/build/lib/standalone.js +++ b/build/lib/standalone.js @@ -160,12 +160,18 @@ function createESMSourcesAndResources2(options) { const pos = info.importedFiles[i].pos; const end = info.importedFiles[i].end; let importedFilepath; + let importedIsAFile = false; if (/^vs\/css!/.test(importedFilename)) { importedFilepath = importedFilename.substr('vs/css!'.length) + '.css'; } else { importedFilepath = importedFilename; } + // try to resolve the imported file path + const filePath = path.join(SRC_FOLDER, importedFilepath); + if (fs.existsSync(filePath + '.ts')) { + importedIsAFile = true; + } if (/(^\.\/)|(^\.\.\/)/.test(importedFilepath)) { importedFilepath = path.join(path.dirname(file), importedFilepath); } @@ -177,7 +183,14 @@ function createESMSourcesAndResources2(options) { relativePath = '../../' + path.basename(path.dirname(path.dirname(file))); } else { - relativePath = path.relative(path.dirname(file), importedFilepath); + if (importedIsAFile) { + importedFilepath = importedFilepath + '.ts'; + relativePath = path.relative(path.dirname(file), importedFilepath); + relativePath = relativePath.replace(/\.ts$/, ''); + } + else { + relativePath = path.relative(path.dirname(file), importedFilepath); + } } relativePath = relativePath.replace(/\\/g, '/'); if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) { diff --git a/build/lib/standalone.ts b/build/lib/standalone.ts index 31c33820a07..bff565d325d 100644 --- a/build/lib/standalone.ts +++ b/build/lib/standalone.ts @@ -192,11 +192,19 @@ export function createESMSourcesAndResources2(options: IOptions2): void { const end = info.importedFiles[i].end; let importedFilepath: string; + let importedIsAFile = false; if (/^vs\/css!/.test(importedFilename)) { importedFilepath = importedFilename.substr('vs/css!'.length) + '.css'; } else { importedFilepath = importedFilename; } + + // try to resolve the imported file path + const filePath = path.join(SRC_FOLDER, importedFilepath); + if (fs.existsSync(filePath + '.ts')) { + importedIsAFile= true; + } + if (/(^\.\/)|(^\.\.\/)/.test(importedFilepath)) { importedFilepath = path.join(path.dirname(file), importedFilepath); } @@ -207,7 +215,13 @@ export function createESMSourcesAndResources2(options: IOptions2): void { } else if (importedFilepath === path.dirname(path.dirname(file)).replace(/\\/g, '/')) { relativePath = '../../' + path.basename(path.dirname(path.dirname(file))); } else { - relativePath = path.relative(path.dirname(file), importedFilepath); + if (importedIsAFile) { + importedFilepath = importedFilepath + '.ts'; + relativePath = path.relative(path.dirname(file), importedFilepath); + relativePath = relativePath.replace(/\.ts$/, ''); + } else{ + relativePath = path.relative(path.dirname(file), importedFilepath); + } } relativePath = relativePath.replace(/\\/g, '/'); if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {