@@ -2,6 +2,7 @@ import * as path from "path";
22import * as tstl from "../../src" ;
33import * as util from "../util" ;
44import * as ts from "typescript" ;
5+ import * as fs from "fs-extra" ;
56import { BuildMode } from "../../src" ;
67import { normalizeSlashes } from "../../src/utils" ;
78import { 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
276323describe ( "module resolution with tsx" , ( ) => {
277324 const projectPath = path . resolve ( __dirname , "module-resolution" , "project-with-tsx" ) ;
0 commit comments