Skip to content

Commit 5b2657d

Browse files
committed
Moved resolution modules to resolve.ts, rewrote them to use configFilePath and rootDir, addressed other PR comments
1 parent 2a1f595 commit 5b2657d

15 files changed

Lines changed: 60 additions & 58 deletions

File tree

src/CompilerOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export enum LuaTarget {
4747
export function validateOptions(options: CompilerOptions): ts.Diagnostic[] {
4848
const diagnostics: ts.Diagnostic[] = [];
4949

50-
if (options.luaBundle && (options.luaBundleEntry === "" || !options.luaBundleEntry)) {
50+
if (options.luaBundle && !options.luaBundleEntry) {
5151
diagnostics.push(configErrorDiagnostic(`'luaBundleEntry' is required when 'luaBundle' is enabled.`));
5252
}
5353

src/Emit.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as ts from "typescript";
33
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
44
import { EmitHost, TranspiledFile } from "./Transpile";
55
import { normalizeSlashes, trimExtension } from "./utils";
6+
import { getProjectRootDir } from "./resolve";
67

78
export interface OutputFile {
89
name: string;
@@ -11,18 +12,15 @@ export interface OutputFile {
1112

1213
let lualibContent: string;
1314
export function emitTranspiledFiles(
15+
program: ts.Program,
1416
options: CompilerOptions,
1517
transpiledFiles: TranspiledFile[],
1618
emitHost: EmitHost = ts.sys
1719
): OutputFile[] {
18-
let { rootDir, outDir, luaLibImport } = options;
20+
let { outDir, luaLibImport } = options;
1921

20-
const configFileName = options.configFilePath as string | undefined;
21-
// TODO: Use getCommonSourceDirectory
22-
const baseDir = configFileName ? path.dirname(configFileName) : process.cwd();
23-
24-
rootDir = rootDir || baseDir;
25-
outDir = outDir ? path.resolve(baseDir, outDir) : rootDir;
22+
const rootDir = getProjectRootDir(program);
23+
outDir = outDir ? path.resolve(rootDir, outDir) : rootDir;
2624

2725
const files: OutputFile[] = [];
2826
for (const { fileName, lua, sourceMap, declaration, declarationMap } of transpiledFiles) {

src/Transpile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import { SourceNode } from "source-map";
12
import * as ts from "typescript";
3+
import { bundleTranspiledFiles } from "./bundle";
24
import { CompilerOptions, validateOptions } from "./CompilerOptions";
35
import * as diagnosticFactories from "./diagnostics";
46
import { Block } from "./LuaAST";
57
import { LuaPrinter } from "./LuaPrinter";
68
import { LuaTransformer } from "./LuaTransformer";
79
import { TranspileError } from "./TranspileError";
810
import { getCustomTransformers } from "./TSTransformers";
9-
import { bundleTranspiledFiles } from "./bundle";
10-
import { SourceNode } from "source-map";
1111

1212
export interface TranspiledFile {
1313
fileName: string;

src/bundle.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import * as path from "path";
2-
import { TranspiledFile, EmitHost } from "./Transpile";
32
import { SourceNode } from "source-map";
4-
import { formatPathToLuaPath, trimExtension, normalizeSlashes } from "./utils";
5-
import { Diagnostic, Program } from "typescript";
3+
import * as ts from "typescript";
64
import { couldNotFindBundleEntryPoint } from "./diagnostics";
5+
import { getProjectRootDir, resolveFromRootDir } from "./resolve";
6+
import { EmitHost, TranspiledFile } from "./Transpile";
7+
import { formatPathToLuaPath, trimExtension } from "./utils";
78

89
const formatPath = (path: string) => formatPathToLuaPath(trimExtension(path));
910

1011
export function bundleTranspiledFiles(
1112
bundleFile: string,
1213
entryModule: string,
1314
transpiledFiles: TranspiledFile[],
14-
program: Program,
15+
program: ts.Program,
1516
emitHost: EmitHost
16-
): [Diagnostic[], TranspiledFile] {
17-
const diagnostics: Diagnostic[] = [];
17+
): [ts.Diagnostic[], TranspiledFile] {
18+
const diagnostics: ts.Diagnostic[] = [];
1819

19-
const resolvedEntryModule = resolveAbsolutePath(program, entryModule);
20-
if (!transpiledFiles.some(f => normalizeSlashes(f.fileName) === normalizeSlashes(resolvedEntryModule))) {
20+
const resolvedEntryModule = resolveFromRootDir(program, entryModule);
21+
if (!transpiledFiles.some(f => resolveFromRootDir(program, f.fileName) === resolvedEntryModule)) {
2122
return [[couldNotFindBundleEntryPoint(entryModule)], { fileName: bundleFile }];
2223
}
2324

@@ -37,8 +38,10 @@ export function bundleTranspiledFiles(
3738

3839
// Override `require` to read from ____modules table.
3940
const requireOverride =
41+
`local ____moduleCache = {}\n` +
4042
`local ____originalRequire = require\n` +
41-
`function require(file) if ____modules[file] then return ____modules[file]() ` +
43+
`function require(file) if ____moduleCache[file] then return ____moduleCache[file] end\n` +
44+
`if ____modules[file] then ____moduleCache[file] = ____modules[file](); return ____moduleCache[file] ` +
4245
`else print("Could not find module '"..file.."' to require."); return ____originalRequire(file) end end\n`;
4346
const entryPoint = `return require("${formatPath(entryModule)}")\n`;
4447

@@ -48,7 +51,7 @@ export function bundleTranspiledFiles(
4851
return [
4952
diagnostics,
5053
{
51-
fileName: resolveAbsolutePath(program, bundleFile),
54+
fileName: path.join(getProjectRootDir(program), bundleFile),
5255
lua: code,
5356
sourceMap: map.toString(),
5457
sourceMapNode: moduleTable,
@@ -82,10 +85,3 @@ function joinSourceChunks(chunks: SourceChunk[]): SourceNode {
8285
// tslint:disable-next-line:no-null-keyword
8386
return new SourceNode(null, null, null, chunks);
8487
}
85-
86-
const getProjectRootDir = (program: Program) => program.getCommonSourceDirectory();
87-
88-
const resolveAbsolutePath = (program: Program, pathToResolve: string) =>
89-
path.isAbsolute(pathToResolve)
90-
? pathToResolve
91-
: path.normalize(path.join(getProjectRootDir(program), pathToResolve));

src/cli/parse.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,22 +178,12 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {
178178
if (value === null) return { value };
179179

180180
switch (option.type) {
181+
case "string":
181182
case "boolean": {
182-
if (typeof value !== "boolean") {
183+
if (typeof value !== option.type) {
183184
return {
184185
value: undefined,
185-
error: cliDiagnostics.compilerOptionRequiresAValueOfType(option.name, "boolean"),
186-
};
187-
}
188-
189-
return { value };
190-
}
191-
192-
case "string": {
193-
if (typeof value !== "string") {
194-
return {
195-
value: undefined,
196-
error: cliDiagnostics.compilerOptionRequiresAValueOfType(option.name, "string"),
186+
error: cliDiagnostics.compilerOptionRequiresAValueOfType(option.name, option.type),
197187
};
198188
}
199189

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface TranspileFilesResult {
2626
export function transpileFiles(rootNames: string[], options: CompilerOptions = {}): TranspileFilesResult {
2727
const program = ts.createProgram(rootNames, options);
2828
const { transpiledFiles, diagnostics: transpileDiagnostics } = transpile({ program });
29-
const emitResult = emitTranspiledFiles(program.getCompilerOptions(), transpiledFiles);
29+
const emitResult = emitTranspiledFiles(program, program.getCompilerOptions(), transpiledFiles);
3030

3131
const diagnostics = ts.sortAndDeduplicateDiagnostics([
3232
...ts.getPreEmitDiagnostics(program),

src/resolve.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as path from "path";
2+
import * as ts from "typescript";
3+
import { normalizeSlashes } from "./utils";
4+
5+
export function getProjectRootDir(program: ts.Program): string {
6+
const options = program.getCompilerOptions();
7+
const projectDir = options.configFilePath || program.getCommonSourceDirectory();
8+
9+
return normalizeSlashes(options.rootDir ? path.resolve(projectDir, options.rootDir) : projectDir);
10+
}
11+
12+
export const resolveFromRootDir = (program: ts.Program, pathToResolve: string) =>
13+
path.isAbsolute(pathToResolve)
14+
? normalizeSlashes(pathToResolve)
15+
: normalizeSlashes(path.resolve(getProjectRootDir(program), pathToResolve));

src/tstl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function performCompilation(
111111
...transpileDiagnostics,
112112
]);
113113

114-
const emitResult = tstl.emitTranspiledFiles(options, transpiledFiles);
114+
const emitResult = tstl.emitTranspiledFiles(program, options, transpiledFiles);
115115
emitResult.forEach(({ name, text }) => ts.sys.writeFile(name, text));
116116

117117
diagnostics.forEach(reportDiagnostic);
@@ -182,7 +182,7 @@ function updateWatchCompilationHost(
182182

183183
const { diagnostics: emitDiagnostics, transpiledFiles } = tstl.transpile({ program, sourceFiles });
184184

185-
const emitResult = tstl.emitTranspiledFiles(options, transpiledFiles);
185+
const emitResult = tstl.emitTranspiledFiles(program, options, transpiledFiles);
186186
emitResult.forEach(({ name, text }) => ts.sys.writeFile(name, text));
187187

188188
const diagnostics = ts.sortAndDeduplicateDiagnostics([

src/typescript-internal.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ declare module "typescript" {
1515
interface Program {
1616
getCommonSourceDirectory(): string;
1717
}
18+
19+
interface CompilerOptions {
20+
configFilePath?: string;
21+
}
1822
}

test/cli/parse.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ describe("command line", () => {
118118

119119
["luaBundle", "foo", { luaBundle: "foo" }],
120120
["luaBundleEntry", "bar", { luaBundleEntry: "bar" }],
121-
])("--%s %s", (optionName, value, expected) => {
121+
])("{ %p: %p }", (optionName, value, expected) => {
122122
const result = tstl.parseCommandLine([`--${optionName}`, value]);
123123

124124
expect(result.errors).not.toHaveErrorDiagnostics();
@@ -226,7 +226,7 @@ describe("tsconfig", () => {
226226

227227
["luaBundle", "foo", { luaBundle: "foo" }],
228228
["luaBundleEntry", "bar", { luaBundleEntry: "bar" }],
229-
])("--%s %s", (optionName, value, expected) => {
229+
])("{ %p: %p }", (optionName, value, expected) => {
230230
const result = parseConfigFileContent({ tstl: { [optionName]: value } });
231231

232232
expect(result.errors).not.toHaveErrorDiagnostics();

0 commit comments

Comments
 (0)