11import * as path from "path" ;
22import * as ts from "typescript" ;
3- import { CompilerOptions , LuaLibImportKind } from "./CompilerOptions" ;
3+ import { LuaLibImportKind } from "./CompilerOptions" ;
44import { EmitHost , TranspiledFile } from "./Transpile" ;
55import { normalizeSlashes , trimExtension } from "./utils" ;
66
@@ -11,18 +11,15 @@ export interface OutputFile {
1111
1212let lualibContent : string ;
1313export function emitTranspiledFiles (
14- options : CompilerOptions ,
14+ program : ts . Program ,
1515 transpiledFiles : TranspiledFile [ ] ,
1616 emitHost : EmitHost = ts . sys
1717) : OutputFile [ ] {
18- let { rootDir, outDir, outFile, luaLibImport } = options ;
18+ const options = program . getCompilerOptions ( ) ;
19+ let { outDir, luaLibImport, luaBundle } = options ;
1920
20- const configFileName = options . configFilePath as string | undefined ;
21- // TODO: Use getCommonSourceDirectory
22- const baseDir = configFileName ? path . dirname ( configFileName ) : process . cwd ( ) ;
23-
24- rootDir = rootDir || baseDir ;
25- outDir = outDir ? path . resolve ( baseDir , outDir ) : rootDir ;
21+ const rootDir = program . getCommonSourceDirectory ( ) ;
22+ outDir = outDir || rootDir ;
2623
2724 const files : OutputFile [ ] = [ ] ;
2825 for ( const { fileName, lua, sourceMap, declaration, declarationMap } of transpiledFiles ) {
@@ -31,14 +28,8 @@ export function emitTranspiledFiles(
3128 outPath = path . resolve ( outDir , path . relative ( rootDir , fileName ) ) ;
3229 }
3330
34- // change extension or rename to outFile
35- if ( outFile ) {
36- outPath = path . isAbsolute ( outFile ) ? outFile : path . resolve ( baseDir , outFile ) ;
37- } else {
38- outPath = trimExtension ( outPath ) + ".lua" ;
39- }
40-
41- outPath = normalizeSlashes ( outPath ) ;
31+ // change extension
32+ outPath = normalizeSlashes ( trimExtension ( outPath ) + ".lua" ) ;
4233
4334 if ( lua !== undefined ) {
4435 files . push ( { name : outPath , text : lua } ) ;
@@ -57,22 +48,30 @@ export function emitTranspiledFiles(
5748 }
5849 }
5950
60- if ( luaLibImport === LuaLibImportKind . Require || luaLibImport === LuaLibImportKind . Always ) {
61- if ( lualibContent === undefined ) {
62- const lualibBundle = emitHost . readFile ( path . resolve ( __dirname , "../dist/lualib/lualib_bundle.lua" ) ) ;
63- if ( lualibBundle !== undefined ) {
64- lualibContent = lualibBundle ;
65- } else {
66- throw new Error ( "Could not load lualib bundle from ./dist/lualib/lualib_bundle.lua" ) ;
51+ if (
52+ ! luaBundle &&
53+ ( luaLibImport === undefined ||
54+ luaLibImport === LuaLibImportKind . Require ||
55+ luaLibImport === LuaLibImportKind . Always )
56+ ) {
57+ const lualibRequired = files . some ( f => f . text && f . text . includes ( `require("lualib_bundle")` ) ) ;
58+ if ( lualibRequired ) {
59+ if ( lualibContent === undefined ) {
60+ const lualibBundle = emitHost . readFile ( path . resolve ( __dirname , "../dist/lualib/lualib_bundle.lua" ) ) ;
61+ if ( lualibBundle !== undefined ) {
62+ lualibContent = lualibBundle ;
63+ } else {
64+ throw new Error ( "Could not load lualib bundle from ./dist/lualib/lualib_bundle.lua" ) ;
65+ }
6766 }
68- }
6967
70- let outPath = path . resolve ( rootDir , "lualib_bundle.lua" ) ;
71- if ( outDir !== rootDir ) {
72- outPath = path . join ( outDir , path . relative ( rootDir , outPath ) ) ;
73- }
68+ let outPath = path . resolve ( rootDir , "lualib_bundle.lua" ) ;
69+ if ( outDir !== rootDir ) {
70+ outPath = path . join ( outDir , path . relative ( rootDir , outPath ) ) ;
71+ }
7472
75- files . push ( { name : normalizeSlashes ( outPath ) , text : lualibContent } ) ;
73+ files . push ( { name : normalizeSlashes ( outPath ) , text : lualibContent } ) ;
74+ }
7675 }
7776
7877 return files ;
0 commit comments