Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion build_lualib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ if (fs.existsSync(bundlePath)) {
fs.unlinkSync(bundlePath);
}

fs.writeFileSync(bundlePath, LuaLib.loadFeatures(Object.values(tstl.LuaLibFeature)));
const emitHost = {
readFile: (path: string) => fs.readFileSync(path, "utf-8"),
writeFile: fs.writeFileSync,
};
fs.writeFileSync(bundlePath, LuaLib.loadFeatures(Object.values(tstl.LuaLibFeature), emitHost));
17 changes: 13 additions & 4 deletions src/Emit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from "fs";
import * as path from "path";
import * as ts from "typescript";
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
import { TranspiledFile } from "./Transpile";
import { TranspiledFile, EmitHost } from "./Transpile";

const trimExt = (filePath: string) => filePath.slice(0, -path.extname(filePath).length);
const normalizeSlashes = (filePath: string) => filePath.replace(/\\/g, "/");
Expand All @@ -12,7 +12,11 @@ export interface OutputFile {
}

let lualibContent: string;
export function emitTranspiledFiles(options: CompilerOptions, transpiledFiles: TranspiledFile[]): OutputFile[] {
export function emitTranspiledFiles(
options: CompilerOptions,
transpiledFiles: TranspiledFile[],
emitHost: EmitHost = ts.sys
): OutputFile[] {
let { rootDir, outDir, outFile, luaLibImport } = options;

const configFileName = options.configFilePath as string | undefined;
Expand Down Expand Up @@ -57,7 +61,12 @@ export function emitTranspiledFiles(options: CompilerOptions, transpiledFiles: T

if (luaLibImport === LuaLibImportKind.Require || luaLibImport === LuaLibImportKind.Always) {
if (lualibContent === undefined) {
lualibContent = fs.readFileSync(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"), "utf8");
const lualibBundle = emitHost.readFile(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"));
if (lualibBundle !== undefined) {
lualibContent = lualibBundle;
} else {
throw new Error("Could not load lualib bundle from ./dist/lualib/lualib_bundle.lua");
}
}

let outPath = path.resolve(rootDir, "lualib_bundle.lua");
Expand Down
11 changes: 8 additions & 3 deletions src/LuaLib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from "fs";
import * as path from "path";
import { EmitHost } from "./Transpile";

export enum LuaLibFeature {
ArrayConcat = "ArrayConcat",
Expand Down Expand Up @@ -73,7 +73,7 @@ const luaLibDependencies: { [lib in LuaLibFeature]?: LuaLibFeature[] } = {
};

export class LuaLib {
public static loadFeatures(features: Iterable<LuaLibFeature>): string {
public static loadFeatures(features: Iterable<LuaLibFeature>, emitHost: EmitHost): string {
let result = "";

const loadedFeatures = new Set<LuaLibFeature>();
Expand All @@ -86,7 +86,12 @@ export class LuaLib {
dependencies.forEach(load);
}
const featureFile = path.resolve(__dirname, `../dist/lualib/${feature}.lua`);
result += fs.readFileSync(featureFile).toString() + "\n";
const luaLibFeature = emitHost.readFile(featureFile);
if (luaLibFeature !== undefined) {
result += luaLibFeature.toString() + "\n";
} else {
throw new Error(`Could not read lualib feature ../dist/lualib/${feature}.lua`);
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/LuaPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as tstl from "./LuaAST";
import { luaKeywords } from "./LuaKeywords";
import { LuaLib, LuaLibFeature } from "./LuaLib";
import * as tsHelper from "./TSHelper";
import { EmitHost } from "./Transpile";

type SourceChunk = string | SourceNode;

Expand Down Expand Up @@ -38,12 +39,15 @@ export class LuaPrinter {
};

private options: CompilerOptions;
private emitHost: EmitHost;

private currentIndent: string;

private sourceFile = "";

public constructor(options: CompilerOptions) {
public constructor(options: CompilerOptions, emitHost: EmitHost) {
this.options = options;
this.emitHost = emitHost;
this.currentIndent = "";
}

Expand Down Expand Up @@ -128,7 +132,7 @@ export class LuaPrinter {
// Inline lualib features
else if (luaLibImport === LuaLibImportKind.Inline && luaLibFeatures.size > 0) {
header += "-- Lua Library inline imports\n";
header += LuaLib.loadFeatures(luaLibFeatures);
header += LuaLib.loadFeatures(luaLibFeatures, this.emitHost);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/Transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ export interface TranspileOptions {
customTransformers?: ts.CustomTransformers;
transformer?: LuaTransformer;
printer?: LuaPrinter;
emitHost?: EmitHost;
}

export interface EmitHost {
readFile(path: string): string | undefined;
}

export function transpile({
program,
sourceFiles: targetSourceFiles,
customTransformers = {},
emitHost = ts.sys,
transformer = new LuaTransformer(program),
printer = new LuaPrinter(program.getCompilerOptions()),
printer = new LuaPrinter(program.getCompilerOptions(), emitHost),
}: TranspileOptions): TranspileResult {
const options = program.getCompilerOptions() as CompilerOptions;

Expand Down