11import * as path from "path" ;
22import { SourceNode } from "source-map" ;
33import * as ts from "typescript" ;
4- import { CompilerOptions } from "../CompilerOptions" ;
4+ import { CompilerOptions , isBundleEnabled } from "../CompilerOptions" ;
55import { escapeString } from "../LuaPrinter" ;
6- import { cast , formatPathToLuaPath , isNonNull , normalizeSlashes , trimExtension } from "../utils" ;
6+ import { assert , normalizeSlashes } from "../utils" ;
77import { couldNotFindBundleEntryPoint } from "./diagnostics" ;
88import { EmitFile , EmitHost , ProcessedFile } from "./utils" ;
99
10- const createModulePath = ( baseDir : string , pathToResolve : string ) =>
11- escapeString ( formatPathToLuaPath ( trimExtension ( path . relative ( baseDir , pathToResolve ) ) ) ) ;
12-
1310// Override `require` to read from ____modules table.
1411const requireOverride = `
1512local ____modules = {}
3532export function getBundleResult (
3633 program : ts . Program ,
3734 emitHost : EmitHost ,
38- files : ProcessedFile [ ]
35+ files : ProcessedFile [ ] ,
36+ getRequirePath : ( file : ProcessedFile ) => string
3937) : [ ts . Diagnostic [ ] , EmitFile ] {
4038 const diagnostics : ts . Diagnostic [ ] = [ ] ;
4139
4240 const options = program . getCompilerOptions ( ) as CompilerOptions ;
43- const bundleFile = cast ( options . luaBundle , isNonNull ) ;
44- const entryModule = cast ( options . luaBundleEntry , isNonNull ) ;
45-
46- const rootDir = program . getCommonSourceDirectory ( ) ;
47- const outDir = options . outDir ?? rootDir ;
41+ assert ( isBundleEnabled ( options ) ) ;
42+ const bundleFile = options . luaBundle ;
43+ const entryModule = options . luaBundleEntry ;
4844 const projectRootDir = options . configFilePath
4945 ? path . dirname ( options . configFilePath )
5046 : emitHost . getCurrentDirectory ( ) ;
47+ const outputPath = normalizeSlashes ( path . resolve ( projectRootDir , bundleFile ) ) ;
5148
5249 // Resolve project settings relative to project file.
5350 const resolvedEntryModule = path . resolve ( projectRootDir , entryModule ) ;
54- const outputPath = normalizeSlashes ( path . resolve ( projectRootDir , bundleFile ) ) ;
55-
56- if ( ! files . some ( f => f . fileName === resolvedEntryModule ) ) {
51+ const entryFile = files . find ( f => f . fileName === resolvedEntryModule ) ;
52+ if ( entryFile === undefined ) {
5753 diagnostics . push ( couldNotFindBundleEntryPoint ( entryModule ) ) ;
5854 return [ diagnostics , { outputPath, code : "" } ] ;
5955 }
6056
6157 // For each file: ["<module path>"] = function() <lua content> end,
62- const moduleTableEntries = files . map ( f => moduleSourceNode ( f , createModulePath ( outDir , f . fileName ) ) ) ;
58+ const moduleTableEntries = files . map ( f => moduleSourceNode ( f , escapeString ( getRequirePath ( f ) ) ) ) ;
6359
6460 // Create ____modules table containing all entries from moduleTableEntries
6561 const moduleTable = createModuleTableNode ( moduleTableEntries ) ;
6662
6763 // return require("<entry module path>")
68- const entryPoint = `return require(${ createModulePath ( outDir , resolvedEntryModule ) } )\n` ;
64+ const entryPoint = `return require(${ escapeString ( getRequirePath ( entryFile ) ) } )\n` ;
6965
7066 const bundleNode = joinSourceChunks ( [ requireOverride , moduleTable , entryPoint ] ) ;
7167 const { code, map } = bundleNode . toStringWithSourceMap ( ) ;
@@ -82,17 +78,11 @@ export function getBundleResult(
8278}
8379
8480function moduleSourceNode ( { code, sourceMapNode } : ProcessedFile , modulePath : string ) : SourceNode {
85- const tableEntryHead = `[${ modulePath } ] = function() ` ;
86- const tableEntryTail = "end,\n" ;
87-
88- return joinSourceChunks ( [ tableEntryHead , sourceMapNode ?? code , tableEntryTail ] ) ;
81+ return joinSourceChunks ( [ `[${ modulePath } ] = function()\n` , sourceMapNode ?? code , "\nend,\n" ] ) ;
8982}
9083
9184function createModuleTableNode ( fileChunks : SourceChunk [ ] ) : SourceNode {
92- const tableHead = "____modules = {\n" ;
93- const tableEnd = "}\n" ;
94-
95- return joinSourceChunks ( [ tableHead , ...fileChunks , tableEnd ] ) ;
85+ return joinSourceChunks ( [ "____modules = {\n" , ...fileChunks , "}\n" ] ) ;
9686}
9787
9888type SourceChunk = string | SourceNode ;
0 commit comments