@@ -3,7 +3,7 @@ import * as ts from "typescript";
33import * as tstl from ".." ;
44import * as path from "path" ;
55import { getUsedLuaLibFeatures } from "../transformation/utils/lualib" ;
6- import { LuaLibFeature , LuaLibModulesInfo , luaLibModulesInfoFileName } from "../LuaLib" ;
6+ import { LuaLibFeature , LuaLibModulesInfo , luaLibModulesInfoFileName , resolveRecursiveLualibFeatures } from "../LuaLib" ;
77import { EmitHost , ProcessedFile } from "../transpilation/utils" ;
88import {
99 isExportAlias ,
@@ -43,10 +43,15 @@ class LuaLibPlugin implements tstl.Plugin {
4343 emitBOM
4444 ) ;
4545
46- // Create lualib bundle by inlining all lualib features into one file
47- const exportedLualibFeatures = result . filter ( f => path . basename ( f . fileName ) . split ( "." ) [ 0 ] in LuaLibFeature ) ;
46+ // Create map of result files keyed by their 'lualib name'
47+ const exportedLualibFeatures = new Map ( result . map ( f => [ path . basename ( f . fileName ) . split ( "." ) [ 0 ] , f . code ] ) ) ;
48+
49+ // Figure out the order required in the bundle by recursively resolving all dependency features
4850 const allFeatures = Object . values ( LuaLibFeature ) as LuaLibFeature [ ] ;
49- let lualibBundle = exportedLualibFeatures . map ( f => f . code ) . join ( "\n" ) ;
51+ const orderedFeatures = resolveRecursiveLualibFeatures ( allFeatures , emitHost ) ;
52+
53+ // Concatenate lualib files into bundle with exports table and add lualib_bundle.lua to results
54+ let lualibBundle = orderedFeatures . map ( f => exportedLualibFeatures . get ( LuaLibFeature [ f ] ) ) . join ( "\n" ) ;
5055 const exports = allFeatures . flatMap ( feature => luaLibModuleInfo [ feature ] . exports ) ;
5156 lualibBundle += `\nreturn {\n${ exports . map ( exportName => ` ${ exportName } = ${ exportName } ` ) . join ( ",\n" ) } \n}\n` ;
5257 result . push ( { fileName : "lualib_bundle.lua" , code : lualibBundle } ) ;
0 commit comments