Skip to content

Commit 4c695cf

Browse files
authored
Support 'exports' field in package.json of published modules. (#1392)
* Support 'exports' field in package.json of published modules. * add exports field test * use fs symlink during test * remove package-lock in test
1 parent 93ff05d commit 4c695cf

File tree

12 files changed

+112
-1
lines changed

12 files changed

+112
-1
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
/test/cli/errors
44
/test/cli/watch
55
/test/transpile/directories
6-
/test/transpile/module-resolution/*/node_modules
6+
/test/transpile/module-resolution/**/node_modules
7+
/test/transpile/module-resolution/**/dist
78
/test/transpile/outFile

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
node_modules
22
/dist
33
/coverage
4+
/test/transpile/module-resolution/**/dist
5+
/test/transpile/module-resolution/**/tsconfig.tsbuildinfo
46

57
yarn.lock
68
.vscode

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
/coverage
33
/test/translation/transformation/characterEscapeSequence.ts
44
/benchmark/dist
5+
/test/transpile/module-resolution/**/node_modules
6+
/test/transpile/module-resolution/**/dist

src/transpilation/resolve.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const resolver = resolve.ResolverFactory.createResolver({
1515
enforceExtension: true, // Resolved file must be a lua file
1616
fileSystem: { ...new resolve.CachedInputFileSystem(fs) },
1717
useSyncFileSystemCalls: true,
18+
conditionNames: ["require", "node", "default"],
1819
symlinks: false, // Do not resolve symlinks to their original paths (that breaks node_modules detection)
1920
});
2021

test/transpile/module-resolution.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as path from "path";
22
import * as tstl from "../../src";
33
import * as util from "../util";
44
import * as ts from "typescript";
5+
import * as fs from "fs-extra";
56
import { BuildMode } from "../../src";
67
import { normalizeSlashes } from "../../src/utils";
78
import { pathsWithoutBaseUrl } from "../../src/transpilation/diagnostics";
@@ -272,6 +273,52 @@ describe("module resolution project with dependencies built by tstl library mode
272273
});
273274
});
274275

276+
describe("module resolution project with dependencies built by tstl library mode and has exports field", () => {
277+
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-tstl-library-has-exports-field");
278+
const appPath = path.join(projectPath, "app");
279+
280+
// First compile dependencies into node_modules. NOTE: Actually writing to disk, very slow
281+
const dependency1Path = path.join(projectPath, "dependency1-ts");
282+
tstl.transpileProject(path.join(dependency1Path, "tsconfig.json"));
283+
284+
// Install dependencies. This will create node_modules folder with dependency1-ts in it.
285+
const nodeModulesPath = path.join(appPath, "node_modules");
286+
fs.ensureDirSync(nodeModulesPath);
287+
fs.ensureSymlinkSync(dependency1Path, path.join(nodeModulesPath, "dependency1"), "dir");
288+
289+
const expectedResult = {
290+
dependency1IndexResult: "function in dependency 1 index: dependency1OtherFileFunc in dependency1/d1otherfile",
291+
dependency1OtherFileFuncResult: "dependency1OtherFileFunc in dependency1/d1otherfile",
292+
};
293+
294+
test("can resolve lua dependencies", () => {
295+
const transpileResult = util
296+
.testProject(path.join(appPath, "tsconfig.json"))
297+
.setMainFileName(path.join(appPath, "main.ts"))
298+
.setOptions({ outDir: "tstl-out", moduleResolution: ts.ModuleResolutionKind.Node16 })
299+
.expectToEqual(expectedResult)
300+
.getLuaResult();
301+
302+
// Assert node_modules file requires the correct lualib_bundle
303+
const requiringLuaFile = path.join("lua_modules", "dependency1", "dist", "index.lua");
304+
const lualibRequiringFile = transpileResult.transpiledFiles.find(f => f.outPath.endsWith(requiringLuaFile));
305+
expect(lualibRequiringFile).toBeDefined();
306+
expect(lualibRequiringFile?.lua).toContain('require("lualib_bundle")');
307+
});
308+
309+
test("can resolve dependencies and bundle", () => {
310+
const mainFile = path.join(appPath, "main.ts");
311+
util.testProject(path.join(appPath, "tsconfig.json"))
312+
.setMainFileName(mainFile)
313+
.setOptions({
314+
luaBundle: "bundle.lua",
315+
luaBundleEntry: mainFile,
316+
moduleResolution: ts.ModuleResolutionKind.Node16,
317+
})
318+
.expectToEqual(expectedResult);
319+
});
320+
});
321+
275322
// Test fix for https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1037
276323
describe("module resolution with tsx", () => {
277324
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-tsx");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { dependency1IndexFunc } from "dependency1/sub";
2+
import { dependency1OtherFileFunc } from "dependency1/sub/d1otherfile";
3+
4+
export const dependency1IndexResult = dependency1IndexFunc();
5+
export const dependency1OtherFileFuncResult = dependency1OtherFileFunc();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "app",
3+
"version": "0.0.1",
4+
"dependencies": {
5+
"dependency1": "file:../dependency1-ts"
6+
}
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "node16"
4+
},
5+
"references": [{ "path": "./node_modules/dependency1" }]
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "dependency1",
3+
"version": "0.0.1",
4+
"exports": {
5+
"./sub": {
6+
"require": "./dist/index",
7+
"types": "./dist/index.d.ts"
8+
},
9+
"./sub/*": {
10+
"require": "./dist/*",
11+
"types": "./dist/*.d.ts"
12+
}
13+
}
14+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function dependency1OtherFileFunc() {
2+
return "dependency1OtherFileFunc in dependency1/d1otherfile";
3+
}

0 commit comments

Comments
 (0)