Skip to content

Commit 89f8d72

Browse files
committed
Renamed transpile/basic test to transpile/project, normalized output path of bundle
1 parent 54dc975 commit 89f8d72

9 files changed

Lines changed: 19 additions & 14 deletions

File tree

src/bundle.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { SourceNode } from "source-map";
33
import * as ts from "typescript";
44
import { couldNotFindBundleEntryPoint } from "./diagnostics";
55
import { EmitHost, TranspiledFile } from "./Transpile";
6-
import { formatPathToLuaPath, trimExtension } from "./utils";
6+
import { formatPathToLuaPath, trimExtension, normalizeSlashes } from "./utils";
77
import { escapeString } from "./TSHelper";
88
import { CompilerOptions } from "./CompilerOptions";
99

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

1313
export function bundleTranspiledFiles(
1414
bundleFile: string,
@@ -37,7 +37,7 @@ export function bundleTranspiledFiles(
3737

3838
// For each file: ["<module path>"] = function() <lua content> end,
3939
const moduleTableEntries: SourceChunk[] = transpiledFiles.map(f =>
40-
moduleSourceNode(f, modulePath(sourceRootDir, f.fileName))
40+
moduleSourceNode(f, createModulePath(sourceRootDir, f.fileName))
4141
);
4242

4343
// If any of the modules contains a require for lualib_bundle, add it to the module table.
@@ -55,31 +55,34 @@ export function bundleTranspiledFiles(
5555
local ____moduleCache = {}
5656
local ____originalRequire = require
5757
function require(file)
58-
if ____moduleCache[file] then return ____moduleCache[file] end
58+
if ____moduleCache[file] then
59+
return ____moduleCache[file]
60+
end
5961
if ____modules[file] then
6062
____moduleCache[file] = ____modules[file]()
6163
return ____moduleCache[file]
6264
else
63-
print("Could not find module '"..file.."' to require.")
64-
return ____originalRequire(file)
65+
if ____originalRequire then
66+
return ____originalRequire(file)
67+
else
68+
print("Could not find module '" .. file .. "' to require.")
69+
end
6570
end
6671
end\n`;
6772

6873
// return require("<entry module path>")
69-
const entryPoint = `return require("${modulePath(sourceRootDir, resolvedEntryModule)}")\n`;
74+
const entryPoint = `return require("${createModulePath(sourceRootDir, resolvedEntryModule)}")\n`;
7075

7176
const bundleNode = joinSourceChunks([moduleTable, requireOverride, entryPoint]);
7277
const { code, map } = bundleNode.toStringWithSourceMap();
7378

7479
return [
7580
diagnostics,
7681
{
77-
fileName: resolvedBundleFile,
82+
fileName: normalizeSlashes(resolvedBundleFile),
7883
lua: code,
7984
sourceMap: map.toString(),
8085
sourceMapNode: moduleTable,
81-
declaration: undefined,
82-
declarationMap: undefined,
8386
},
8487
];
8588
}
File renamed without changes.

test/transpile/bundle.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test("should transpile into one file", () => {
1313

1414
const { name, text } = transpileResult.emitResult[0];
1515
// Verify the name is as specified in tsconfig
16-
expect(path.join(name)).toBe(path.join(projectDir, "bundle.lua"));
16+
expect(name).toBe(path.join(projectDir, "bundle.lua").replace(/\\/g, "/"));
1717
// Verify exported module by executing
1818
expect(util.executeLuaModule(text)).toEqual({ myNumber: 3 });
1919
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from "path";
22
import { transpileProject } from "../../src";
33

4-
const projectDir = path.join(__dirname, "basic");
4+
const projectDir = path.join(__dirname, "project");
55
const inputProject = path.join(projectDir, "tsconfig.json");
66

77
test("should transpile", () => {

test/unit/bundle.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ test("bundle file name", () => {
3333

3434
expect(diagnostics.length).toBe(0);
3535
expect(transpiledFiles.length).toBe(1);
36-
expect(transpiledFiles[0].fileName).toBe(path.join(ts.sys.getCurrentDirectory(), "mybundle.lua"));
36+
expect(transpiledFiles[0].fileName).toBe(
37+
path.join(ts.sys.getCurrentDirectory(), "mybundle.lua").replace(/\\/g, "/")
38+
);
3739
});
3840

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

0 commit comments

Comments
 (0)