mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 20:52:56 +01:00
Let TypeScript resolve deps in lib files + allow external tools to extend the lib support
This commit is contained in:
parent
778ace10c0
commit
1d6f5a0e24
3 changed files with 77 additions and 46 deletions
|
|
@ -124,6 +124,7 @@ function importLibs() {
|
||||||
};
|
};
|
||||||
|
|
||||||
enqueue('');
|
enqueue('');
|
||||||
|
enqueue('es6');
|
||||||
enqueue('es2015');
|
enqueue('es2015');
|
||||||
|
|
||||||
var result = [];
|
var result = [];
|
||||||
|
|
@ -149,10 +150,9 @@ function importLibs() {
|
||||||
for (let i = 0; i < lines.length; i++) {
|
for (let i = 0; i < lines.length; i++) {
|
||||||
let m = lines[i].match(/\/\/\/\s*<reference\s*lib="([^"]+)"/);
|
let m = lines[i].match(/\/\/\/\s*<reference\s*lib="([^"]+)"/);
|
||||||
if (m) {
|
if (m) {
|
||||||
flushOutputLines();
|
|
||||||
writeOutput(getVariableName(m[1]));
|
|
||||||
deps.push(getVariableName(m[1]));
|
deps.push(getVariableName(m[1]));
|
||||||
enqueue(m[1]);
|
enqueue(m[1]);
|
||||||
|
outputLines.push(lines[i]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
outputLines.push(lines[i]);
|
outputLines.push(lines[i]);
|
||||||
|
|
@ -161,6 +161,7 @@ function importLibs() {
|
||||||
|
|
||||||
result.push({
|
result.push({
|
||||||
name: getVariableName(name),
|
name: getVariableName(name),
|
||||||
|
filepath: getFileName(name),
|
||||||
deps: deps,
|
deps: deps,
|
||||||
output: output
|
output: output
|
||||||
});
|
});
|
||||||
|
|
@ -170,14 +171,19 @@ function importLibs() {
|
||||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||||
*--------------------------------------------------------------------------------------------*/
|
*--------------------------------------------------------------------------------------------*/
|
||||||
${generatedNote}`;
|
${generatedNote}
|
||||||
|
|
||||||
|
/** Contains all the lib files */
|
||||||
|
export const libFileMap: Record<string, string> = {}
|
||||||
|
`
|
||||||
|
;
|
||||||
// Do a topological sort
|
// Do a topological sort
|
||||||
while (result.length > 0) {
|
while (result.length > 0) {
|
||||||
for (let i = result.length - 1; i >= 0; i--) {
|
for (let i = result.length - 1; i >= 0; i--) {
|
||||||
if (result[i].deps.length === 0) {
|
if (result[i].deps.length === 0) {
|
||||||
// emit this node
|
// emit this node
|
||||||
strResult += `\nexport const ${result[i].name}: string = ${result[i].output};\n`;
|
strResult += `\nexport const ${result[i].name}: string = ${result[i].output};\n`;
|
||||||
|
strResult += `\libFileMap['${result[i].filepath}'] = ${result[i].name};\n`;
|
||||||
// mark dep as resolved
|
// mark dep as resolved
|
||||||
for (let j = 0; j < result.length; j++) {
|
for (let j = 0; j < result.length; j++) {
|
||||||
for (let k = 0; k < result[j].deps.length; k++) {
|
for (let k = 0; k < result[j].deps.length; k++) {
|
||||||
|
|
@ -195,14 +201,6 @@ ${generatedNote}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
strResult += `
|
|
||||||
/** This is the DTS which is used when the target is ES6 or below */
|
|
||||||
export const lib_es5_bundled_dts = lib_dts;
|
|
||||||
|
|
||||||
/** This is the DTS which is used by default in monaco-typescript, and when the target is 2015 or above */
|
|
||||||
export const lib_es2015_bundled_dts = lib_es2015_dts + "" + lib_dom_dts + "" + lib_webworker_importscripts_dts + "" + lib_scripthost_dts + "";
|
|
||||||
`
|
|
||||||
|
|
||||||
var dstPath = path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.ts');
|
var dstPath = path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.ts');
|
||||||
fs.writeFileSync(dstPath, strResult);
|
fs.writeFileSync(dstPath, strResult);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -5,21 +5,11 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import * as ts from './lib/typescriptServices';
|
import * as ts from './lib/typescriptServices';
|
||||||
import { lib_es5_dts, lib_es2015_bundled_dts } from './lib/lib';
|
import { libFileMap } from './lib/lib';
|
||||||
import { IExtraLibs } from './monaco.contribution';
|
import { IExtraLibs } from './monaco.contribution';
|
||||||
|
|
||||||
import IWorkerContext = monaco.worker.IWorkerContext;
|
import IWorkerContext = monaco.worker.IWorkerContext;
|
||||||
|
|
||||||
const DEFAULT_ES5_LIB = {
|
|
||||||
NAME: 'defaultLib:lib.d.ts',
|
|
||||||
CONTENTS: lib_es5_dts
|
|
||||||
};
|
|
||||||
|
|
||||||
const ES2015_LIB = {
|
|
||||||
NAME: 'defaultLib:lib.es2015.d.ts',
|
|
||||||
CONTENTS: lib_es2015_bundled_dts
|
|
||||||
};
|
|
||||||
|
|
||||||
export class TypeScriptWorker implements ts.LanguageServiceHost, monaco.languages.typescript.TypeScriptWorker {
|
export class TypeScriptWorker implements ts.LanguageServiceHost, monaco.languages.typescript.TypeScriptWorker {
|
||||||
|
|
||||||
// --- model sync -----------------------
|
// --- model sync -----------------------
|
||||||
|
|
@ -84,10 +74,8 @@ export class TypeScriptWorker implements ts.LanguageServiceHost, monaco.language
|
||||||
// extra lib
|
// extra lib
|
||||||
text = this._extraLibs[fileName].content;
|
text = this._extraLibs[fileName].content;
|
||||||
|
|
||||||
} else if (fileName === DEFAULT_ES5_LIB.NAME) {
|
} else if (fileName in libFileMap) {
|
||||||
text = DEFAULT_ES5_LIB.CONTENTS;
|
text = libFileMap[fileName];
|
||||||
} else if (fileName === ES2015_LIB.NAME) {
|
|
||||||
text = ES2015_LIB.CONTENTS;
|
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -126,8 +114,30 @@ export class TypeScriptWorker implements ts.LanguageServiceHost, monaco.language
|
||||||
}
|
}
|
||||||
|
|
||||||
getDefaultLibFileName(options: ts.CompilerOptions): string {
|
getDefaultLibFileName(options: ts.CompilerOptions): string {
|
||||||
// TODO@joh support lib.es7.d.ts
|
switch (options.target) {
|
||||||
return (options.target || ts.ScriptTarget.ES2015) < ts.ScriptTarget.ES2015 ? DEFAULT_ES5_LIB.NAME : ES2015_LIB.NAME;
|
case 99 /* ESNext */:
|
||||||
|
const esnext = "lib.esnext.full.d.ts";
|
||||||
|
if (esnext in libFileMap || esnext in this._extraLibs) return esnext
|
||||||
|
case 7 /* ES2020 */:
|
||||||
|
case 6 /* ES2019 */:
|
||||||
|
case 5 /* ES2018 */:
|
||||||
|
case 4 /* ES2017 */:
|
||||||
|
case 3 /* ES2016 */:
|
||||||
|
case 2 /* ES2015 */:
|
||||||
|
default:
|
||||||
|
// Support a dynamic lookup for the ES20XX version based on the target
|
||||||
|
// which is safe unless TC39 changes their numbering system
|
||||||
|
const eslib = `lib.es${2013 + (options.target || 99)}.full.d.ts`;
|
||||||
|
// Note: This also looks in _extraLibs, If you want
|
||||||
|
// to add support for additional target options, you will need to
|
||||||
|
// add the extra dts files to _extraLibs via the API.
|
||||||
|
if (eslib in libFileMap || eslib in this._extraLibs) return eslib
|
||||||
|
|
||||||
|
return "lib.es6.d.ts"; // We don't use lib.es2015.full.d.ts due to breaking change.
|
||||||
|
case 1:
|
||||||
|
case 0:
|
||||||
|
return "lib.d.ts";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isDefaultLibFileName(fileName: string): boolean {
|
isDefaultLibFileName(fileName: string): boolean {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue