forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranspiler.ts
More file actions
133 lines (110 loc) · 5.02 KB
/
Copy pathtranspiler.ts
File metadata and controls
133 lines (110 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import * as path from "path";
import * as ts from "typescript";
import { isBundleEnabled } from "../CompilerOptions";
import { getLuaLibBundle } from "../LuaLib";
import { normalizeSlashes, trimExtension } from "../utils";
import { getBundleResult } from "./bundle";
import { resolveDependencies } from "./resolve";
import { getProgramTranspileResult, TranspileOptions } from "./transpile";
import { EmitFile, EmitHost, ProcessedFile } from "./utils";
export interface TranspilerOptions {
emitHost?: EmitHost;
}
export interface EmitOptions extends TranspileOptions {
writeFile?: ts.WriteFileCallback;
}
export interface EmitResult {
emitSkipped: boolean;
diagnostics: readonly ts.Diagnostic[];
}
export class Transpiler {
protected emitHost: EmitHost;
constructor({ emitHost = ts.sys }: TranspilerOptions = {}) {
this.emitHost = emitHost;
}
public emit(emitOptions: EmitOptions): EmitResult {
const { program, writeFile = this.emitHost.writeFile } = emitOptions;
const { diagnostics, transpiledFiles: freshFiles } = getProgramTranspileResult(
this.emitHost,
writeFile,
emitOptions
);
const { emitPlan } = this.getEmitPlan(program, diagnostics, freshFiles);
const options = program.getCompilerOptions();
const emitBOM = options.emitBOM ?? false;
for (const { outputPath, code, sourceMap, sourceFiles } of emitPlan) {
writeFile(outputPath, code, emitBOM, undefined, sourceFiles);
if (options.sourceMap && sourceMap !== undefined) {
writeFile(outputPath + ".map", sourceMap, emitBOM, undefined, sourceFiles);
}
}
return { diagnostics, emitSkipped: emitPlan.length === 0 };
}
protected getEmitPlan(
program: ts.Program,
diagnostics: ts.Diagnostic[],
files: ProcessedFile[]
): { emitPlan: EmitFile[] } {
const options = program.getCompilerOptions();
const lualibRequired = files.some(f => f.code.includes('require("lualib_bundle")'));
if (lualibRequired) {
// Add lualib bundle to source dir 'virtually', will be moved to correct output dir in emitPlan
const fileName = normalizeSlashes(path.resolve(getSourceDir(program), "lualib_bundle.lua"));
files.unshift({ fileName, code: getLuaLibBundle(this.emitHost) });
}
// Resolve imported modules and modify output Lua requires
const resolutionResult = resolveDependencies(program, files, this.emitHost);
diagnostics.push(...resolutionResult.diagnostics);
let emitPlan: EmitFile[];
if (isBundleEnabled(options)) {
const [bundleDiagnostics, bundleFile] = getBundleResult(program, resolutionResult.resolvedFiles);
diagnostics.push(...bundleDiagnostics);
emitPlan = [bundleFile];
} else {
emitPlan = resolutionResult.resolvedFiles.map(file => ({
...file,
outputPath: getEmitPath(file.fileName, program),
}));
}
return { emitPlan };
}
}
export function getEmitPath(file: string, program: ts.Program): string {
const relativeOutputPath = getEmitPathRelativeToOutDir(file, program);
const outDir = getEmitOutDir(program);
return path.join(outDir, relativeOutputPath);
}
export function getEmitPathRelativeToOutDir(fileName: string, program: ts.Program): string {
const sourceDir = getSourceDir(program);
// Default output path is relative path in source dir
let emitPathSplits = path.relative(sourceDir, fileName).split(path.sep);
// If source is in a parent directory of source dir, move it into the source dir
emitPathSplits = emitPathSplits.filter(s => s !== "..");
// To avoid overwriting lua sources in node_modules, emit into lua_modules
if (emitPathSplits[0] === "node_modules") {
emitPathSplits[0] = "lua_modules";
}
// Make extension lua
emitPathSplits[emitPathSplits.length - 1] = trimExtension(emitPathSplits[emitPathSplits.length - 1]) + ".lua";
return path.join(...emitPathSplits);
}
export function getSourceDir(program: ts.Program): string {
const rootDir = program.getCompilerOptions().rootDir;
if (rootDir && rootDir.length > 0) {
return path.isAbsolute(rootDir) ? rootDir : path.resolve(getProjectRoot(program), rootDir);
}
return program.getCommonSourceDirectory();
}
export function getEmitOutDir(program: ts.Program): string {
const outDir = program.getCompilerOptions().outDir;
if (outDir && outDir.length > 0) {
return path.isAbsolute(outDir) ? outDir : path.resolve(getProjectRoot(program), outDir);
}
return program.getCommonSourceDirectory();
}
export function getProjectRoot(program: ts.Program): string {
// Try to get the directory the tsconfig is in
const tsConfigPath = program.getCompilerOptions().configFilePath;
// If no tsconfig is known, use common source directory
return tsConfigPath ? path.dirname(tsConfigPath) : program.getCommonSourceDirectory();
}