Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3811,12 +3811,12 @@ export class LuaTransformer {
}

private getImportPath(relativePath: string): string {
const rootDir = this.options.rootDir || path.resolve(".");
const absoluteImportPath = this.formatPathToLuaPath(this.getAbsoluteImportPath(relativePath));
const absoluteRootDirPath = this.formatPathToLuaPath(rootDir);
const rootDir = this.options.rootDir ? path.resolve(this.options.rootDir) : path.resolve(".");
const absoluteImportPath = path.format(path.parse(this.getAbsoluteImportPath(relativePath)));
const absoluteRootDirPath = path.format(path.parse(rootDir));
if (absoluteImportPath.includes(absoluteRootDirPath)) {
const relativePathToRoot = absoluteImportPath.replace(absoluteRootDirPath, "").slice(1);
return relativePathToRoot;
return this.formatPathToLuaPath(
absoluteImportPath.replace(absoluteRootDirPath, "").slice(1));
} else {
throw TSTLErrors.UnresolvableRequirePath(undefined,
`Cannot create require path. Module does not exist within --rootDir`,
Expand Down
3 changes: 2 additions & 1 deletion test/unit/require.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { CompilerOptions } from "../../src/CompilerOptions";
export class RequireTests {

@TestCase("file.ts", "./folder/Module", "folder.Module", { rootDir: "." }, false)
@TestCase("file.ts", "./folder/Module", "folder.Module", { rootDir: "./" }, false)
@TestCase("src/file.ts", "./folder/Module", "src.folder.Module", { rootDir: "." }, false)
@TestCase("file.ts", "folder/Module", "folder.Module", { rootDir: ".", baseUrl: "." }, false)
@TestCase("file.ts", "folder/Module", "folder.Module", { rootDir: "./", baseUrl: "." }, false)
@TestCase("src/file.ts", "./folder/Module", "folder.Module", { rootDir: "src" }, false)
@TestCase("src/file.ts", "./folder/Module", "folder.Module", { rootDir: "./src" }, false)
@TestCase("file.ts", "../Module", "", { rootDir: "./src" }, true)
Expand All @@ -22,7 +24,6 @@ export class RequireTests {
options: CompilerOptions,
throwsError: boolean): void {
const regex = /require\("(.*?)"\)/; // This regex extracts `hello` from require("hello")
options.rootDir = path.resolve(options.rootDir); // This happens automatically from the command line
if (throwsError) {
Expect(() => util.transpileString(`import * from "${usedPath}";`, options, true, filePath)).toThrow();
} else {
Expand Down