Skip to content

Commit 55e0788

Browse files
committed
Resolve conflicts
1 parent 4a3b9d0 commit 55e0788

6 files changed

Lines changed: 43 additions & 19 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "typescript-to-lua",
2+
"name": "@memoraike/typescript-to-lua",
33
"version": "1.15.1",
44
"description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!",
55
"repository": "https://github.com/TypeScriptToLua/TypeScriptToLua",

src/CompilerOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface TypeScriptToLuaOptions {
2929
luaBundle?: string;
3030
luaBundleEntry?: string;
3131
luaTarget?: LuaTarget;
32+
kahluaImport?: boolean;
3233
luaLibImport?: LuaLibImportKind;
3334
luaPlugins?: LuaPluginImport[];
3435
noImplicitGlobalVariables?: boolean;

src/transpilation/bundle.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { getEmitOutDir, getEmitPathRelativeToOutDir, getProjectRoot } from "./tr
99
import { EmitFile, ProcessedFile } from "./utils";
1010

1111
const createModulePath = (pathToResolve: string, program: ts.Program) =>
12-
escapeString(formatPathToLuaPath(trimExtension(getEmitPathRelativeToOutDir(pathToResolve, program))));
12+
escapeString(
13+
formatPathToLuaPath(
14+
trimExtension(getEmitPathRelativeToOutDir(pathToResolve, program)),
15+
program.getCompilerOptions()
16+
)
17+
);
1318

1419
// Override `require` to read from ____modules table.
1520
function requireOverride(options: CompilerOptions) {

src/transpilation/resolve.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class ResolutionContext {
4949
// Remove @NoResolution prefix if not building in library mode
5050
if (!isBuildModeLibrary(this.program)) {
5151
const path = required.requirePath.replace("@NoResolution:", "");
52-
replaceRequireInCode(file, required, path, this.options.extension);
53-
replaceRequireInSourceMap(file, required, path, this.options.extension);
52+
replaceRequireInCode(file, required, path, this.options);
53+
replaceRequireInSourceMap(file, required, path, this.options);
5454
}
5555

5656
// Skip
@@ -88,8 +88,8 @@ class ResolutionContext {
8888
// Figure out resolved require path and dependency output path
8989
if (shouldRewriteRequires(dependencyPath, this.program)) {
9090
const resolvedRequire = getEmitPathRelativeToOutDir(dependencyPath, this.program);
91-
replaceRequireInCode(file, required, resolvedRequire, this.options.extension);
92-
replaceRequireInSourceMap(file, required, resolvedRequire, this.options.extension);
91+
replaceRequireInCode(file, required, resolvedRequire, this.options);
92+
replaceRequireInSourceMap(file, required, resolvedRequire, this.options);
9393
}
9494
}
9595

@@ -116,9 +116,14 @@ class ResolutionContext {
116116
}
117117

118118
private couldNotResolveImport(required: LuaRequire, file: ProcessedFile): void {
119-
const fallbackRequire = fallbackResolve(required, getSourceDir(this.program), path.dirname(file.fileName));
120-
replaceRequireInCode(file, required, fallbackRequire, this.options.extension);
121-
replaceRequireInSourceMap(file, required, fallbackRequire, this.options.extension);
119+
const fallbackRequire = fallbackResolve(
120+
required,
121+
getSourceDir(this.program),
122+
path.dirname(file.fileName),
123+
this.options
124+
);
125+
replaceRequireInCode(file, required, fallbackRequire, this.options);
126+
replaceRequireInSourceMap(file, required, fallbackRequire, this.options);
122127

123128
this.diagnostics.push(
124129
couldNotResolveRequire(required.requirePath, path.relative(getProjectRoot(this.program), file.fileName))
@@ -314,9 +319,9 @@ function replaceRequireInCode(
314319
file: ProcessedFile,
315320
originalRequire: LuaRequire,
316321
newRequire: string,
317-
extension: string | undefined
322+
options: CompilerOptions
318323
): void {
319-
const requirePath = requirePathForFile(newRequire, extension);
324+
const requirePath = requirePathForFile(newRequire, options);
320325
file.code = file.code =
321326
file.code.substring(0, originalRequire.from) +
322327
`require("${requirePath}")` +
@@ -327,9 +332,9 @@ function replaceRequireInSourceMap(
327332
file: ProcessedFile,
328333
originalRequire: LuaRequire,
329334
newRequire: string,
330-
extension?: string | undefined
335+
options: CompilerOptions
331336
): void {
332-
const requirePath = requirePathForFile(newRequire, extension);
337+
const requirePath = requirePathForFile(newRequire, options);
333338
if (file.sourceMapNode) {
334339
replaceInSourceMap(
335340
file.sourceMapNode,
@@ -340,14 +345,15 @@ function replaceRequireInSourceMap(
340345
}
341346
}
342347

343-
function requirePathForFile(filePath: string, extension = ".lua"): string {
348+
function requirePathForFile(filePath: string, options: CompilerOptions): string {
349+
let extension = options.extension ?? ".lua";
344350
if (!extension.startsWith(".")) {
345351
extension = `.${extension}`;
346352
}
347353
if (filePath.endsWith(extension)) {
348-
return formatPathToLuaPath(filePath.substring(0, filePath.length - extension.length));
354+
return formatPathToLuaPath(filePath.substring(0, filePath.length - extension.length), options);
349355
} else {
350-
return formatPathToLuaPath(filePath);
356+
return formatPathToLuaPath(filePath, options);
351357
}
352358
}
353359

@@ -389,13 +395,19 @@ function hasSourceFileInProject(filePath: string, program: ts.Program) {
389395
}
390396

391397
// Transform an import path to a lua require that is probably not correct, but can be used as fallback when regular resolution fails
392-
function fallbackResolve(required: LuaRequire, sourceRootDir: string, fileDir: string): string {
398+
function fallbackResolve(
399+
required: LuaRequire,
400+
sourceRootDir: string,
401+
fileDir: string,
402+
options: CompilerOptions
403+
): string {
393404
return formatPathToLuaPath(
394405
path
395406
.normalize(path.join(path.relative(sourceRootDir, fileDir), required.requirePath))
396407
.split(path.sep)
397408
.filter(s => s !== "." && s !== "..")
398-
.join(path.sep)
409+
.join(path.sep),
410+
options
399411
);
400412
}
401413

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as ts from "typescript";
22
import * as nativeAssert from "assert";
33
import * as path from "path";
4+
import { CompilerOptions } from "./CompilerOptions";
45

56
export function castArray<T>(value: T | T[]): T[];
67
export function castArray<T>(value: T | readonly T[]): readonly T[];
@@ -38,8 +39,9 @@ export const createSerialDiagnosticFactory = <T extends DiagnosticFactory>(creat
3839
export const normalizeSlashes = (filePath: string) => filePath.replace(/\\/g, "/");
3940
export const trimExtension = (filePath: string) => filePath.slice(0, -path.extname(filePath).length);
4041

41-
export function formatPathToLuaPath(filePath: string): string {
42+
export function formatPathToLuaPath(filePath: string, options: CompilerOptions): string {
4243
filePath = filePath.replace(/\.json$/, "");
44+
if (options.kahluaImport) return filePath;
4345
if (process.platform === "win32") {
4446
// Windows can use backslashes
4547
filePath = filePath.replace(/\.\\/g, "").replace(/\\/g, ".");

tsconfig-schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
"type": "boolean",
3333
"default": false
3434
},
35+
"kahluaImport": {
36+
"description": "Disable replacing slashes by dots in imports",
37+
"type": "boolean"
38+
},
3539
"luaBundle": {
3640
"description": "The name of the lua file to bundle output lua to. Requires luaBundleEntry.",
3741
"type": "string"

0 commit comments

Comments
 (0)