@@ -2,101 +2,90 @@ import * as path from "path";
22import { SourceNode } from "source-map" ;
33import * as ts from "typescript" ;
44import { CompilerOptions } from "../CompilerOptions" ;
5- import { getLuaLibBundle } from "../LuaLib" ;
65import { escapeString } from "../LuaPrinter" ;
7- import { formatPathToLuaPath , normalizeSlashes , trimExtension } from "../utils" ;
8- import * as diagnosticFactories from "./diagnostics" ;
9- import { EmitHost , TranspiledFile } from "./transpile " ;
6+ import { cast , formatPathToLuaPath , isNonNull , normalizeSlashes , trimExtension } from "../utils" ;
7+ import { couldNotFindBundleEntryPoint } from "./diagnostics" ;
8+ import { EmitFile , EmitHost , ProcessedFile } from "./utils " ;
109
1110const createModulePath = ( baseDir : string , pathToResolve : string ) =>
1211 escapeString ( formatPathToLuaPath ( trimExtension ( path . relative ( baseDir , pathToResolve ) ) ) ) ;
1312
14- export function bundleTranspiledFiles (
15- bundleFile : string ,
16- entryModule : string ,
17- transpiledFiles : TranspiledFile [ ] ,
13+ // Override `require` to read from ____modules table.
14+ const requireOverride = `
15+ local ____modules = {}
16+ local ____moduleCache = {}
17+ local ____originalRequire = require
18+ local function require(file)
19+ if ____moduleCache[file] then
20+ return ____moduleCache[file]
21+ end
22+ if ____modules[file] then
23+ ____moduleCache[file] = ____modules[file]()
24+ return ____moduleCache[file]
25+ else
26+ if ____originalRequire then
27+ return ____originalRequire(file)
28+ else
29+ error("module '" .. file .. "' not found")
30+ end
31+ end
32+ end
33+ ` ;
34+
35+ export function getBundleResult (
1836 program : ts . Program ,
19- emitHost : EmitHost
20- ) : [ ts . Diagnostic [ ] , TranspiledFile ] {
37+ emitHost : EmitHost ,
38+ files : ProcessedFile [ ]
39+ ) : [ ts . Diagnostic [ ] , EmitFile ] {
2140 const diagnostics : ts . Diagnostic [ ] = [ ] ;
2241
2342 const options = program . getCompilerOptions ( ) as CompilerOptions ;
43+ const bundleFile = cast ( options . luaBundle , isNonNull ) ;
44+ const entryModule = cast ( options . luaBundleEntry , isNonNull ) ;
2445
46+ const rootDir = program . getCommonSourceDirectory ( ) ;
47+ const outDir = options . outDir ?? rootDir ;
2548 const projectRootDir = options . configFilePath
2649 ? path . dirname ( options . configFilePath )
2750 : emitHost . getCurrentDirectory ( ) ;
2851
2952 // Resolve project settings relative to project file.
3053 const resolvedEntryModule = path . resolve ( projectRootDir , entryModule ) ;
31- const resolvedBundleFile = path . resolve ( projectRootDir , bundleFile ) ;
54+ const outputPath = normalizeSlashes ( path . resolve ( projectRootDir , bundleFile ) ) ;
3255
33- // Resolve source files relative to common source directory.
34- const sourceRootDir = program . getCommonSourceDirectory ( ) ;
35- if ( ! transpiledFiles . some ( f => path . resolve ( sourceRootDir , f . fileName ) === resolvedEntryModule ) ) {
36- return [ [ diagnosticFactories . couldNotFindBundleEntryPoint ( entryModule ) ] , { fileName : bundleFile } ] ;
56+ if ( ! files . some ( f => f . fileName === resolvedEntryModule ) ) {
57+ diagnostics . push ( couldNotFindBundleEntryPoint ( entryModule ) ) ;
58+ return [ diagnostics , { outputPath, code : "" } ] ;
3759 }
3860
3961 // For each file: ["<module path>"] = function() <lua content> end,
40- const moduleTableEntries : SourceChunk [ ] = transpiledFiles . map ( f =>
41- moduleSourceNode ( f , createModulePath ( sourceRootDir , f . fileName ) )
42- ) ;
43-
44- // If any of the modules contains a require for lualib_bundle, add it to the module table.
45- const lualibRequired = transpiledFiles . some ( f => f . lua ?. includes ( 'require("lualib_bundle")' ) ) ;
46- if ( lualibRequired ) {
47- moduleTableEntries . push ( `["lualib_bundle"] = function() ${ getLuaLibBundle ( emitHost ) } end,\n` ) ;
48- }
62+ const moduleTableEntries = files . map ( f => moduleSourceNode ( f , createModulePath ( outDir , f . fileName ) ) ) ;
4963
5064 // Create ____modules table containing all entries from moduleTableEntries
5165 const moduleTable = createModuleTableNode ( moduleTableEntries ) ;
5266
53- // Override `require` to read from ____modules table.
54- const requireOverride = `
55- local ____modules = {}
56- local ____moduleCache = {}
57- local ____originalRequire = require
58- local function require(file)
59- if ____moduleCache[file] then
60- return ____moduleCache[file]
61- end
62- if ____modules[file] then
63- ____moduleCache[file] = ____modules[file]()
64- return ____moduleCache[file]
65- else
66- if ____originalRequire then
67- return ____originalRequire(file)
68- else
69- error("module '" .. file .. "' not found")
70- end
71- end
72- end\n` ;
73-
7467 // return require("<entry module path>")
75- const entryPoint = `return require(${ createModulePath ( sourceRootDir , resolvedEntryModule ) } )\n` ;
68+ const entryPoint = `return require(${ createModulePath ( outDir , resolvedEntryModule ) } )\n` ;
7669
7770 const bundleNode = joinSourceChunks ( [ requireOverride , moduleTable , entryPoint ] ) ;
7871 const { code, map } = bundleNode . toStringWithSourceMap ( ) ;
7972
8073 return [
8174 diagnostics ,
8275 {
83- fileName : normalizeSlashes ( resolvedBundleFile ) ,
84- lua : code ,
76+ outputPath ,
77+ code,
8578 sourceMap : map . toString ( ) ,
86- sourceMapNode : moduleTable ,
79+ sourceFiles : files . flatMap ( x => x . sourceFiles ?? [ ] ) ,
8780 } ,
8881 ] ;
8982}
9083
91- function moduleSourceNode ( transpiledFile : TranspiledFile , modulePath : string ) : SourceNode {
84+ function moduleSourceNode ( { code , sourceMapNode } : ProcessedFile , modulePath : string ) : SourceNode {
9285 const tableEntryHead = `[${ modulePath } ] = function() ` ;
9386 const tableEntryTail = "end,\n" ;
9487
95- if ( transpiledFile . lua && transpiledFile . sourceMapNode ) {
96- return joinSourceChunks ( [ tableEntryHead , transpiledFile . sourceMapNode , tableEntryTail ] ) ;
97- } else {
98- return joinSourceChunks ( [ tableEntryHead , tableEntryTail ] ) ;
99- }
88+ return joinSourceChunks ( [ tableEntryHead , sourceMapNode ?? code , tableEntryTail ] ) ;
10089}
10190
10291function createModuleTableNode ( fileChunks : SourceChunk [ ] ) : SourceNode {
0 commit comments