Skip to content

Commit 54dc975

Browse files
committed
Changed path resolution again, using confFilePath again
1 parent 0942443 commit 54dc975

5 files changed

Lines changed: 52 additions & 31 deletions

File tree

src/Transpile.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface TranspileOptions {
3535
}
3636

3737
export interface EmitHost {
38+
getCurrentDirectory(): string;
3839
readFile(path: string): string | undefined;
3940
}
4041

src/bundle.ts

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import * as path from "path";
22
import { SourceNode } from "source-map";
33
import * as ts from "typescript";
44
import { couldNotFindBundleEntryPoint } from "./diagnostics";
5-
import { resolveFromRootDir } from "./resolve";
65
import { EmitHost, TranspiledFile } from "./Transpile";
76
import { formatPathToLuaPath, trimExtension } from "./utils";
7+
import { escapeString } from "./TSHelper";
8+
import { CompilerOptions } from "./CompilerOptions";
89

9-
const formatPath = (path: string) => formatPathToLuaPath(trimExtension(path));
10+
const formatPath = (path: string) => escapeString(formatPathToLuaPath(trimExtension(path)));
11+
const modulePath = (baseDir: string, pathToResolve: string) => formatPath(path.relative(baseDir, pathToResolve));
1012

1113
export function bundleTranspiledFiles(
1214
bundleFile: string,
@@ -17,17 +19,29 @@ export function bundleTranspiledFiles(
1719
): [ts.Diagnostic[], TranspiledFile] {
1820
const diagnostics: ts.Diagnostic[] = [];
1921

20-
const resolvedEntryModule = resolveFromRootDir(program, entryModule);
21-
if (!transpiledFiles.some(f => resolveFromRootDir(program, f.fileName) === resolvedEntryModule)) {
22+
const options = program.getCompilerOptions() as CompilerOptions;
23+
24+
const projectRootDir = options.configFilePath
25+
? path.dirname(options.configFilePath)
26+
: emitHost.getCurrentDirectory();
27+
28+
// Resolve project settings relative to project file.
29+
const resolvedEntryModule = path.resolve(projectRootDir, entryModule);
30+
const resolvedBundleFile = path.resolve(projectRootDir, bundleFile);
31+
32+
// Resolve source files relative to common source directory.
33+
const sourceRootDir = program.getCommonSourceDirectory();
34+
if (!transpiledFiles.some(f => path.resolve(sourceRootDir, f.fileName) === resolvedEntryModule)) {
2235
return [[couldNotFindBundleEntryPoint(entryModule)], { fileName: bundleFile }];
2336
}
2437

25-
// For each file: ["<file name>"] = function() <lua content> end,
26-
const projectRootDir = program.getCommonSourceDirectory();
27-
const moduleTableEntries: SourceChunk[] = transpiledFiles.map(f => moduleSourceNode(f, projectRootDir));
38+
// For each file: ["<module path>"] = function() <lua content> end,
39+
const moduleTableEntries: SourceChunk[] = transpiledFiles.map(f =>
40+
moduleSourceNode(f, modulePath(sourceRootDir, f.fileName))
41+
);
2842

2943
// If any of the modules contains a require for lualib_bundle, add it to the module table.
30-
const lualibRequired = transpiledFiles.some(f => f.lua && f.lua.match(/require\("lualib_bundle"\)/));
44+
const lualibRequired = transpiledFiles.some(f => f.lua && f.lua.includes(`require("lualib_bundle")`));
3145
if (lualibRequired) {
3246
const lualibBundle = emitHost.readFile(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"));
3347
moduleTableEntries.push(`["lualib_bundle"] = function() ${lualibBundle} end,\n`);
@@ -37,21 +51,30 @@ export function bundleTranspiledFiles(
3751
const moduleTable = createModuleTableNode(moduleTableEntries);
3852

3953
// Override `require` to read from ____modules table.
40-
const requireOverride =
41-
`local ____moduleCache = {}\n` +
42-
`local ____originalRequire = require\n` +
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] ` +
45-
`else print("Could not find module '"..file.."' to require."); return ____originalRequire(file) end end\n`;
46-
const entryPoint = `return require("${formatPath(entryModule)}")\n`;
54+
const requireOverride = `
55+
local ____moduleCache = {}
56+
local ____originalRequire = require
57+
function require(file)
58+
if ____moduleCache[file] then return ____moduleCache[file] end
59+
if ____modules[file] then
60+
____moduleCache[file] = ____modules[file]()
61+
return ____moduleCache[file]
62+
else
63+
print("Could not find module '"..file.."' to require.")
64+
return ____originalRequire(file)
65+
end
66+
end\n`;
67+
68+
// return require("<entry module path>")
69+
const entryPoint = `return require("${modulePath(sourceRootDir, resolvedEntryModule)}")\n`;
4770

4871
const bundleNode = joinSourceChunks([moduleTable, requireOverride, entryPoint]);
4972
const { code, map } = bundleNode.toStringWithSourceMap();
5073

5174
return [
5275
diagnostics,
5376
{
54-
fileName: path.join(program.getCommonSourceDirectory(), bundleFile),
77+
fileName: resolvedBundleFile,
5578
lua: code,
5679
sourceMap: map.toString(),
5780
sourceMapNode: moduleTable,
@@ -61,9 +84,8 @@ export function bundleTranspiledFiles(
6184
];
6285
}
6386

64-
function moduleSourceNode(transpiledFile: TranspiledFile, projectRootDir: string): SourceNode {
65-
const resolvedProjectPath = path.relative(projectRootDir, transpiledFile.fileName);
66-
const tableEntryHead = `["${formatPath(resolvedProjectPath)}"] = function() `;
87+
function moduleSourceNode(transpiledFile: TranspiledFile, modulePath: string): SourceNode {
88+
const tableEntryHead = `["${modulePath}"] = function() `;
6789
const tableEntryTail = `end,\n`;
6890

6991
if (transpiledFile.lua && transpiledFile.sourceMapNode) {

src/resolve.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

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/unit/bundle.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import * as util from "../util";
2-
import { couldNotFindBundleEntryPoint } from "../../src/diagnostics";
3-
import { LuaLibImportKind } from "../../src";
1+
import * as path from "path";
2+
import * as ts from "typescript";
43
import { DiagnosticCategory } from "typescript";
4+
import { LuaLibImportKind } from "../../src";
5+
import { couldNotFindBundleEntryPoint } from "../../src/diagnostics";
6+
import * as util from "../util";
57

68
test("no entry point", () => {
79
util.testBundle``
@@ -31,7 +33,7 @@ test("bundle file name", () => {
3133

3234
expect(diagnostics.length).toBe(0);
3335
expect(transpiledFiles.length).toBe(1);
34-
expect(transpiledFiles[0].fileName).toBe("mybundle.lua");
36+
expect(transpiledFiles[0].fileName).toBe(path.join(ts.sys.getCurrentDirectory(), "mybundle.lua"));
3537
});
3638

3739
test("import chain export -> reexport -> main", () => {

0 commit comments

Comments
 (0)