Improve sourcemap handling in modCompilerService
This commit is contained in:
parent
6d19452455
commit
4b70bfb60d
@ -2,7 +2,7 @@
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { inject, injectable } from "tsyringe";
|
import { inject, injectable } from "tsyringe";
|
||||||
import { CompilerOptions, ModuleKind, ModuleResolutionKind, ScriptTarget, TranspileOptions, transpileModule } from "typescript";
|
import ts from "typescript";
|
||||||
import type { ILogger } from "../models/spt/utils/ILogger";
|
import type { ILogger } from "../models/spt/utils/ILogger";
|
||||||
import { VFS } from "../utils/VFS";
|
import { VFS } from "../utils/VFS";
|
||||||
import { HashCacheService } from "./HashCacheService";
|
import { HashCacheService } from "./HashCacheService";
|
||||||
@ -64,18 +64,17 @@ export class ModCompilerService
|
|||||||
{
|
{
|
||||||
noEmitOnError: true,
|
noEmitOnError: true,
|
||||||
noImplicitAny: false,
|
noImplicitAny: false,
|
||||||
target: ScriptTarget.ES2022,
|
target: ts.ScriptTarget.ES2022,
|
||||||
module: ModuleKind.CommonJS,
|
module: ts.ModuleKind.CommonJS,
|
||||||
moduleResolution: ModuleResolutionKind.Node10,
|
moduleResolution: ts.ModuleResolutionKind.Node10,
|
||||||
inlineSourceMap: true,
|
sourceMap: true,
|
||||||
resolveJsonModule: true,
|
resolveJsonModule: true,
|
||||||
allowJs: true,
|
allowJs: true,
|
||||||
esModuleInterop: true,
|
esModuleInterop: true,
|
||||||
downlevelIteration: true,
|
downlevelIteration: true,
|
||||||
experimentalDecorators: true,
|
experimentalDecorators: true,
|
||||||
emitDecoratorMetadata: true,
|
emitDecoratorMetadata: true,
|
||||||
rootDir: modPath,
|
rootDir: modPath
|
||||||
isolatedModules: true
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,22 +83,21 @@ export class ModCompilerService
|
|||||||
* @param fileNames Paths to TS files
|
* @param fileNames Paths to TS files
|
||||||
* @param options Compiler options
|
* @param options Compiler options
|
||||||
*/
|
*/
|
||||||
protected async compile(fileNames: string[], options: CompilerOptions): Promise<void>
|
protected async compile(fileNames: string[], options: ts.CompilerOptions): Promise<void>
|
||||||
{
|
|
||||||
const tranOptions: TranspileOptions = {
|
|
||||||
compilerOptions: options
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const filePath of fileNames)
|
|
||||||
{
|
|
||||||
const readFile = fs.readFileSync(filePath);
|
|
||||||
const text = readFile.toString();
|
|
||||||
|
|
||||||
let replacedText: string;
|
|
||||||
if (globalThis.G_RELEASE_CONFIGURATION)
|
|
||||||
{
|
{
|
||||||
// C:/snapshot/project || /snapshot/project
|
// C:/snapshot/project || /snapshot/project
|
||||||
const baseDir: string = __dirname.replace(/\\/g,"/").split("/").slice(0, 3).join("/");
|
const baseDir: string = __dirname.replace(/\\/g,"/").split("/").slice(0, 3).join("/");
|
||||||
|
|
||||||
|
for (const filePath of fileNames)
|
||||||
|
{
|
||||||
|
const destPath = filePath.replace(".ts", ".js");
|
||||||
|
const parsedPath = path.parse(filePath);
|
||||||
|
const parsedDestPath = path.parse(destPath);
|
||||||
|
const text = fs.readFileSync(filePath).toString();
|
||||||
|
let replacedText: string;
|
||||||
|
|
||||||
|
if (globalThis.G_RELEASE_CONFIGURATION)
|
||||||
|
{
|
||||||
replacedText = text.replace(/(@spt-aki)/g, `${baseDir}/obj`);
|
replacedText = text.replace(/(@spt-aki)/g, `${baseDir}/obj`);
|
||||||
for (const dependency of this.serverDependencies)
|
for (const dependency of this.serverDependencies)
|
||||||
{
|
{
|
||||||
@ -111,8 +109,19 @@ export class ModCompilerService
|
|||||||
replacedText = text.replace(/(@spt-aki)/g, path.join(__dirname, "..").replace(/\\/g,"/"));
|
replacedText = text.replace(/(@spt-aki)/g, path.join(__dirname, "..").replace(/\\/g,"/"));
|
||||||
}
|
}
|
||||||
|
|
||||||
const output = transpileModule(replacedText, tranOptions);
|
const output = ts.transpileModule(replacedText, { compilerOptions: options });
|
||||||
fs.writeFileSync(filePath.replace(".ts", ".js"), output.outputText);
|
|
||||||
|
if (output.sourceMapText)
|
||||||
|
{
|
||||||
|
output.outputText = output.outputText.replace("//# sourceMappingURL=module.js.map", `//# sourceMappingURL=${parsedDestPath.base}.map`);
|
||||||
|
|
||||||
|
const sourceMap = JSON.parse(output.sourceMapText);
|
||||||
|
sourceMap.file = parsedDestPath.base;
|
||||||
|
sourceMap.sources = [ parsedPath.base ];
|
||||||
|
|
||||||
|
fs.writeFileSync(`${destPath}.map`, JSON.stringify(sourceMap));
|
||||||
|
}
|
||||||
|
fs.writeFileSync(destPath, output.outputText);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!this.areFilesReady(fileNames))
|
while (!this.areFilesReady(fileNames))
|
||||||
|
Loading…
Reference in New Issue
Block a user