Skip to content

Commit 781c286

Browse files
committed
Basic node_modules resolution support
1 parent 6b34308 commit 781c286

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

src/transpilation/transpiler.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as resolve from "resolve";
33
import * as ts from "typescript";
44
import { CompilerOptions, isBundleEnabled, LuaTarget } from "../CompilerOptions";
55
import { getLuaLibBundle } from "../LuaLib";
6-
import { cast, isNonNull, normalizeSlashes, trimExtension } from "../utils";
6+
import { assert, cast, isNonNull, normalizeSlashes, trimExtension } from "../utils";
77
import { getBundleResult } from "./bundle";
88
import { replaceResolveMacroInSource, replaceResolveMacroSourceNodes, ResolveMacroReplacer } from "./macro";
99
import { getProgramTranspileResult, TranspileOptions } from "./transpile";
@@ -79,13 +79,13 @@ class Transpilation {
7979

8080
if (isBundleEnabled(this.options)) {
8181
const [bundleDiagnostics, bundleFile] = getBundleResult(this.program, this.emitHost, this.files, file =>
82-
this.toRequireParameter(this.toOutputStructure(file.fileName))
82+
this.toRequireParameter(this.toGeneratedFileName(file.fileName))
8383
);
8484
this.diagnostics.push(...bundleDiagnostics);
8585
return [bundleFile];
8686
} else {
8787
return this.files.map(file => {
88-
const pathInOutDir = this.toAbsoluteOutputPath(this.toOutputStructure(file.fileName));
88+
const pathInOutDir = this.toAbsoluteOutputPath(this.toGeneratedFileName(file.fileName));
8989
const outputPath = normalizeSlashes(trimExtension(pathInOutDir) + ".lua");
9090
return { ...file, outputPath };
9191
});
@@ -115,7 +115,7 @@ class Transpilation {
115115
// TODO: Load source map files
116116
});
117117

118-
return this.toRequireParameter(this.toOutputStructure(resolvedPath));
118+
return this.toRequireParameter(this.toGeneratedFileName(resolvedPath));
119119
};
120120

121121
if (file.sourceMapNode) {
@@ -160,20 +160,14 @@ class Transpilation {
160160
throw new Error(`"${resolved}" is builtin Node.js module.`);
161161
}
162162

163-
this.toOutputStructure(resolved);
164-
165163
return resolved;
166164
}
167165

168-
protected toOutputStructure(fileName: string) {
166+
protected toGeneratedFileName(fileName: string) {
169167
const result = path.relative(this.rootDir, trimExtension(fileName));
170-
171-
if (result.startsWith("..") || path.isAbsolute(result)) {
172-
// TODO: Walk up to find package.json and put it to node_modules/name_version_hash/
173-
throw new Error(`Cannot resolve "${fileName}" within rootDir`);
174-
}
175-
176-
return result.replace(/\./g, "__");
168+
// TODO: handle files on other drives
169+
assert(!path.isAbsolute(result), `Invalid path: ${result}`);
170+
return result.replace(/\.\.\//g, "_/").replace(/\./g, "__");
177171
}
178172

179173
protected toRequireParameter(fileName: string) {

0 commit comments

Comments
 (0)