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
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
/test/cli/errors
/test/cli/watch
/test/transpile/directories
/test/transpile/module-resolution/*/node_modules
/test/transpile/module-resolution/**/node_modules
/test/transpile/module-resolution/**/dist
/test/transpile/outFile
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
node_modules
/dist
/coverage
/test/transpile/module-resolution/**/dist
/test/transpile/module-resolution/**/tsconfig.tsbuildinfo

yarn.lock
.vscode
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/coverage
/test/translation/transformation/characterEscapeSequence.ts
/benchmark/dist
/test/transpile/module-resolution/**/node_modules
/test/transpile/module-resolution/**/dist
1 change: 1 addition & 0 deletions src/transpilation/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const resolver = resolve.ResolverFactory.createResolver({
enforceExtension: true, // Resolved file must be a lua file
fileSystem: { ...new resolve.CachedInputFileSystem(fs) },
useSyncFileSystemCalls: true,
conditionNames: ["require", "node", "default"],
symlinks: false, // Do not resolve symlinks to their original paths (that breaks node_modules detection)
});

Expand Down
47 changes: 47 additions & 0 deletions test/transpile/module-resolution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as path from "path";
import * as tstl from "../../src";
import * as util from "../util";
import * as ts from "typescript";
import * as fs from "fs-extra";
import { BuildMode } from "../../src";
import { normalizeSlashes } from "../../src/utils";
import { pathsWithoutBaseUrl } from "../../src/transpilation/diagnostics";
Expand Down Expand Up @@ -272,6 +273,52 @@ describe("module resolution project with dependencies built by tstl library mode
});
});

describe("module resolution project with dependencies built by tstl library mode and has exports field", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-tstl-library-has-exports-field");
const appPath = path.join(projectPath, "app");

// First compile dependencies into node_modules. NOTE: Actually writing to disk, very slow
const dependency1Path = path.join(projectPath, "dependency1-ts");
tstl.transpileProject(path.join(dependency1Path, "tsconfig.json"));

// Install dependencies. This will create node_modules folder with dependency1-ts in it.
const nodeModulesPath = path.join(appPath, "node_modules");
fs.ensureDirSync(nodeModulesPath);
fs.ensureSymlinkSync(dependency1Path, path.join(nodeModulesPath, "dependency1"), "dir");

const expectedResult = {
dependency1IndexResult: "function in dependency 1 index: dependency1OtherFileFunc in dependency1/d1otherfile",
dependency1OtherFileFuncResult: "dependency1OtherFileFunc in dependency1/d1otherfile",
};

test("can resolve lua dependencies", () => {
const transpileResult = util
.testProject(path.join(appPath, "tsconfig.json"))
.setMainFileName(path.join(appPath, "main.ts"))
.setOptions({ outDir: "tstl-out", moduleResolution: ts.ModuleResolutionKind.Node16 })
.expectToEqual(expectedResult)
.getLuaResult();

// Assert node_modules file requires the correct lualib_bundle
const requiringLuaFile = path.join("lua_modules", "dependency1", "dist", "index.lua");
const lualibRequiringFile = transpileResult.transpiledFiles.find(f => f.outPath.endsWith(requiringLuaFile));
expect(lualibRequiringFile).toBeDefined();
expect(lualibRequiringFile?.lua).toContain('require("lualib_bundle")');
});

test("can resolve dependencies and bundle", () => {
const mainFile = path.join(appPath, "main.ts");
util.testProject(path.join(appPath, "tsconfig.json"))
.setMainFileName(mainFile)
.setOptions({
luaBundle: "bundle.lua",
luaBundleEntry: mainFile,
moduleResolution: ts.ModuleResolutionKind.Node16,
})
.expectToEqual(expectedResult);
});
});

// Test fix for https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1037
describe("module resolution with tsx", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-tsx");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { dependency1IndexFunc } from "dependency1/sub";
import { dependency1OtherFileFunc } from "dependency1/sub/d1otherfile";

export const dependency1IndexResult = dependency1IndexFunc();
export const dependency1OtherFileFuncResult = dependency1OtherFileFunc();
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "app",
"version": "0.0.1",
"dependencies": {
"dependency1": "file:../dependency1-ts"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"moduleResolution": "node16"
},
"references": [{ "path": "./node_modules/dependency1" }]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "dependency1",
"version": "0.0.1",
"exports": {
"./sub": {
"require": "./dist/index",
"types": "./dist/index.d.ts"
},
"./sub/*": {
"require": "./dist/*",
"types": "./dist/*.d.ts"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function dependency1OtherFileFunc() {
return "dependency1OtherFileFunc in dependency1/d1otherfile";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { dependency1OtherFileFunc } from "./d1otherfile";

export function dependency1IndexFunc() {
return "function in dependency 1 index: " + dependency1OtherFileFunc();
}

export function squares(nums: number[]) {
// Require lualib functionality
return nums.map(n => n * n);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"composite": true
},
"tstl": {
"buildMode": "library"
}
}