fix typescript compiler script
This commit is contained in:
parent
29bea68525
commit
e93498fed5
@ -1,35 +1,31 @@
|
|||||||
import { createDocumentRegistry, createLanguageService, ModuleKind, ScriptSnapshot, ScriptTarget, type Diagnostic, type CompilerOptions, type IScriptSnapshot, flattenDiagnosticMessageText, CompilerHost, LanguageService } from "typescript";
|
import { createDocumentRegistry, createLanguageService, ModuleKind, ScriptSnapshot, ScriptTarget, type Diagnostic, type CompilerOptions, type IScriptSnapshot, flattenDiagnosticMessageText, CompilerHost, LanguageService } from "typescript";
|
||||||
|
|
||||||
// declare function getResource(name: string): string | undefined;
|
declare function getResource(name: string): string | undefined;
|
||||||
declare function print(...args: any[]): void;
|
declare function print(...args: any[]): void;
|
||||||
// declare function register(factory: CompilerFactory): void;
|
declare function register(factory: CompilerFactory): void;
|
||||||
|
declare function parseVLQ(compiled: string, original: string, map: string): (loc: Location) => Location;
|
||||||
|
declare function chainMaps(...mappers: ((loc: Location) => Location)[]): (loc: Location) => Location;
|
||||||
|
declare function registerSource(filename: string, src: string): void;
|
||||||
|
|
||||||
|
type Location = readonly [filename: string, line: number, col: number];
|
||||||
type CompilerFactory = (next: Compiler) => Compiler;
|
type CompilerFactory = (next: Compiler) => Compiler;
|
||||||
type Compiler = (filename: string, src: string, maps: any[]) => Function;
|
type Compiler = (filename: string, src: string, mapper: (loc: Location) => Location) => Function;
|
||||||
|
|
||||||
const resources: Record<string, string | undefined> = {};
|
const resources: Record<string, string | undefined> = {};
|
||||||
|
|
||||||
function getResource(name: string): string | undefined {
|
|
||||||
if (name === "/lib.d.ts") return "declare var a = 10;";
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
function resource(name: string) {
|
function resource(name: string) {
|
||||||
if (name in resources) return resources[name];
|
if (name in resources) return resources[name];
|
||||||
else return resources[name] = getResource(name);
|
else return resources[name] = getResource(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
function register(factory: CompilerFactory): void {
|
|
||||||
factory((filename, src) => Function(src));
|
|
||||||
}
|
|
||||||
|
|
||||||
register(next => {
|
register(next => {
|
||||||
const files: Record<string, IScriptSnapshot> = {};
|
const files: Record<string, IScriptSnapshot> = {};
|
||||||
const versions: Record<string, number> = {};
|
const versions: Record<string, number> = {};
|
||||||
let declI = 0;
|
let declI = 0;
|
||||||
|
|
||||||
const settings: CompilerOptions = {
|
const settings: CompilerOptions = {
|
||||||
target: ScriptTarget.ESNext,
|
target: ScriptTarget.ES5,
|
||||||
module: ModuleKind.Preserve,
|
module: ModuleKind.Preserve,
|
||||||
|
|
||||||
allowImportingTsExtensions: true,
|
allowImportingTsExtensions: true,
|
||||||
@ -39,6 +35,7 @@ register(next => {
|
|||||||
skipLibCheck: true,
|
skipLibCheck: true,
|
||||||
forceConsistentCasingInFileNames: true,
|
forceConsistentCasingInFileNames: true,
|
||||||
declaration: true,
|
declaration: true,
|
||||||
|
sourceMap: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
let service: LanguageService;
|
let service: LanguageService;
|
||||||
@ -75,7 +72,7 @@ register(next => {
|
|||||||
});
|
});
|
||||||
print("Loaded typescript!");
|
print("Loaded typescript!");
|
||||||
|
|
||||||
return (code, filename, mapChain) => {
|
return (filename, code, prevMap) => {
|
||||||
files["/src.ts"] = ScriptSnapshot.fromString(code);
|
files["/src.ts"] = ScriptSnapshot.fromString(code);
|
||||||
versions["/src.ts"] ??= 0;
|
versions["/src.ts"] ??= 0;
|
||||||
versions["/src.ts"]++;
|
versions["/src.ts"]++;
|
||||||
@ -83,10 +80,9 @@ register(next => {
|
|||||||
const emit = service.getEmitOutput("/src.ts");
|
const emit = service.getEmitOutput("/src.ts");
|
||||||
|
|
||||||
const diagnostics = new Array<Diagnostic>()
|
const diagnostics = new Array<Diagnostic>()
|
||||||
.concat(service.getCompilerOptionsDiagnostics())
|
|
||||||
.concat(service.getSyntacticDiagnostics("/src.ts"))
|
.concat(service.getSyntacticDiagnostics("/src.ts"))
|
||||||
.concat(service.getSemanticDiagnostics("/src.ts"))
|
.concat(service.getSemanticDiagnostics("/src.ts"))
|
||||||
.map(function (diagnostic) {
|
.map(diagnostic => {
|
||||||
const message = flattenDiagnosticMessageText(diagnostic.messageText, "\n");
|
const message = flattenDiagnosticMessageText(diagnostic.messageText, "\n");
|
||||||
|
|
||||||
if (diagnostic.file != null) {
|
if (diagnostic.file != null) {
|
||||||
@ -105,11 +101,19 @@ register(next => {
|
|||||||
throw new SyntaxError(diagnostics.join("\n"));
|
throw new SyntaxError(diagnostics.join("\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
var map = JSON.parse(emit.outputFiles[0].text);
|
const outputs: Record<string, string> = {};
|
||||||
var result = emit.outputFiles[1].text;
|
|
||||||
var declaration = emit.outputFiles[2].text;
|
|
||||||
|
|
||||||
var compiled = next(result, filename, mapChain.concat(map));
|
for (const el of emit.outputFiles) {
|
||||||
|
outputs[el.name] = el.text;
|
||||||
|
}
|
||||||
|
|
||||||
|
const rawMap = JSON.parse(outputs["/src.js.map"]);
|
||||||
|
const map = parseVLQ(filename, filename, rawMap.mappings);
|
||||||
|
const result = outputs["/src.js"];
|
||||||
|
const declaration = outputs["/src.d.ts"];
|
||||||
|
|
||||||
|
const compiled = next(filename, result, chainMaps(prevMap, map));
|
||||||
|
registerSource(filename, code);
|
||||||
|
|
||||||
return function (this: any) {
|
return function (this: any) {
|
||||||
const res = compiled.apply(this, arguments);
|
const res = compiled.apply(this, arguments);
|
||||||
|
10
src/main/resources/lib/lib.d.ts
vendored
Normal file
10
src/main/resources/lib/lib.d.ts
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
declare function print(...args: any[]): void;
|
||||||
|
|
||||||
|
declare type Array = {};
|
||||||
|
declare type Boolean = {};
|
||||||
|
declare type Function = {};
|
||||||
|
declare type IArguments = {};
|
||||||
|
declare type Number = {};
|
||||||
|
declare type Object = {};
|
||||||
|
declare type RegExp = {};
|
||||||
|
declare type String = {};
|
Loading…
Reference in New Issue
Block a user