Skip to content

Commit 6afdc45

Browse files
committed
Simplify concepts used in module id generation
1 parent d494d6f commit 6afdc45

2 files changed

Lines changed: 16 additions & 18 deletions

File tree

src/transpilation/bundle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function getBundleResult(
3333
program: ts.Program,
3434
emitHost: EmitHost,
3535
files: ProcessedFile[],
36-
getRequirePath: (file: ProcessedFile) => string
36+
createModuleId: (file: ProcessedFile) => string
3737
): [ts.Diagnostic[], EmitFile] {
3838
const diagnostics: ts.Diagnostic[] = [];
3939

@@ -55,13 +55,13 @@ export function getBundleResult(
5555
}
5656

5757
// For each file: ["<module path>"] = function() <lua content> end,
58-
const moduleTableEntries = files.map(f => moduleSourceNode(f, escapeString(getRequirePath(f))));
58+
const moduleTableEntries = files.map(f => moduleSourceNode(f, escapeString(createModuleId(f))));
5959

6060
// Create ____modules table containing all entries from moduleTableEntries
6161
const moduleTable = createModuleTableNode(moduleTableEntries);
6262

6363
// return require("<entry module path>")
64-
const entryPoint = `return require(${escapeString(getRequirePath(entryFile))})\n`;
64+
const entryPoint = `return require(${escapeString(createModuleId(entryFile))})\n`;
6565

6666
const bundleNode = joinSourceChunks([requireOverride, moduleTable, entryPoint]);
6767
const { code, map } = bundleNode.toStringWithSourceMap();

src/transpilation/transpiler.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,15 @@ class Transpilation {
8787

8888
if (isBundleEnabled(this.options)) {
8989
const [bundleDiagnostics, bundleFile] = getBundleResult(this.program, this.emitHost, this.files, file =>
90-
this.toRequireParameter(this.toGeneratedFileName(file.fileName))
90+
this.createModuleId(file.fileName)
9191
);
9292
this.diagnostics.push(...bundleDiagnostics);
9393
return [bundleFile];
9494
} else {
95-
return this.files.map(file => {
96-
const pathInOutDir = this.toAbsoluteOutputPath(this.toGeneratedFileName(file.fileName));
97-
const outputPath = normalizeSlashes(trimExtension(pathInOutDir) + ".lua");
98-
return { ...file, outputPath };
99-
});
95+
return this.files.map(file => ({
96+
...file,
97+
outputPath: this.moduleIdToOutputPath(this.createModuleId(file.fileName)),
98+
}));
10099
}
101100
}
102101

@@ -130,7 +129,7 @@ class Transpilation {
130129
}
131130
}
132131

133-
return this.toRequireParameter(this.toGeneratedFileName(resolvedPath));
132+
return this.createModuleId(resolvedPath);
134133
};
135134

136135
if (file.sourceMapNode) {
@@ -153,18 +152,17 @@ class Transpilation {
153152
useSyncFileSystemCalls: true,
154153
});
155154

156-
protected toGeneratedFileName(fileName: string) {
155+
protected createModuleId(fileName: string) {
157156
const result = path.relative(this.rootDir, trimExtension(fileName));
158157
// TODO: handle files on other drives
159158
assert(!path.isAbsolute(result), `Invalid path: ${result}`);
160-
return result.replace(/\.\.\//g, "_/").replace(/\./g, "__");
159+
return result
160+
.replace(/\.\.[/\\]/g, "_/")
161+
.replace(/\./g, "__")
162+
.replace(/[/\\]/g, ".");
161163
}
162164

163-
protected toRequireParameter(fileName: string) {
164-
return fileName.replace(/[/\\]/g, ".");
165-
}
166-
167-
protected toAbsoluteOutputPath(fileName: string) {
168-
return path.resolve(this.outDir, `${fileName}.lua`);
165+
protected moduleIdToOutputPath(moduleId: string) {
166+
return normalizeSlashes(path.resolve(this.outDir, `${moduleId.replace(/\./g, "/")}.lua`));
169167
}
170168
}

0 commit comments

Comments
 (0)