@@ -5,11 +5,11 @@ import * as ts from "typescript";
55import { CompilerOptions , isBundleEnabled , LuaTarget } from "../CompilerOptions" ;
66import { getLuaLibBundle } from "../LuaLib" ;
77import { assert , cast , isNonNull , normalizeSlashes , trimExtension } from "../utils" ;
8- import { getBundleResult } from "./bundle" ;
8+ import { getBundleChunk } from "./bundle" ;
99import { createResolutionErrorDiagnostic } from "./diagnostics" ;
10- import { replaceResolveMacroInSource , replaceResolveMacroSourceNodes , ResolveMacroReplacer } from "./macro" ;
11- import { getProgramTranspileResult , TranspileOptions } from "./transpile" ;
12- import { EmitFile , EmitHost , ProcessedFile } from "./utils" ;
10+ import { replaceResolveMacroInSource , replaceResolveMacroSourceNodes , MacroDependencyResolver } from "./macro" ;
11+ import { emitProgramModules , TranspileOptions } from "./transpile" ;
12+ import { Chunk , EmitHost , Module } from "./utils" ;
1313
1414export interface TranspilerOptions {
1515 emitHost ?: EmitHost ;
@@ -32,8 +32,8 @@ export class Transpiler {
3232
3333 public emit ( emitOptions : EmitOptions ) : EmitResult {
3434 const { program, writeFile = this . emitHost . writeFile } = emitOptions ;
35- const { diagnostics, transpiledFiles } = getProgramTranspileResult ( this . emitHost , writeFile , emitOptions ) ;
36- const emitPlan = this . getEmitPlan ( program , diagnostics , transpiledFiles ) ;
35+ const { diagnostics, modules } = emitProgramModules ( this . emitHost , writeFile , emitOptions ) ;
36+ const emitPlan = this . getEmitPlan ( program , diagnostics , modules ) ;
3737
3838 const options = program . getCompilerOptions ( ) ;
3939 const emitBOM = options . emitBOM ?? false ;
@@ -47,7 +47,7 @@ export class Transpiler {
4747 return { diagnostics, emitSkipped : emitPlan . length === 0 } ;
4848 }
4949
50- private getEmitPlan ( program : ts . Program , diagnostics : ts . Diagnostic [ ] , transpiledFiles : ProcessedFile [ ] ) {
50+ private getEmitPlan ( program : ts . Program , diagnostics : ts . Diagnostic [ ] , transpiledFiles : Module [ ] ) {
5151 const transpilation = new Transpilation ( this . emitHost , program ) ;
5252 const emitPlan = transpilation . emit ( transpiledFiles ) ;
5353 diagnostics . push ( ...transpilation . diagnostics ) ;
@@ -58,7 +58,7 @@ export class Transpiler {
5858class Transpilation {
5959 public readonly diagnostics : ts . Diagnostic [ ] = [ ] ;
6060 private seenFiles = new Set < string > ( ) ;
61- private files : ProcessedFile [ ] = [ ] ;
61+ private modules : Module [ ] = [ ] ;
6262
6363 private options = this . program . getCompilerOptions ( ) as CompilerOptions ;
6464 private rootDir : string ;
@@ -75,39 +75,39 @@ class Transpilation {
7575 this . outDir = this . options . outDir ?? this . rootDir ;
7676 }
7777
78- public emit ( transpiledFiles : ProcessedFile [ ] ) : EmitFile [ ] {
79- transpiledFiles . forEach ( file => this . seenFiles . add ( file . fileName ) ) ;
80- transpiledFiles . forEach ( file => this . handleProcessedFile ( file ) ) ;
78+ public emit ( programModules : Module [ ] ) : Chunk [ ] {
79+ programModules . forEach ( file => this . seenFiles . add ( file . fileName ) ) ;
80+ programModules . forEach ( file => this . addModule ( file ) ) ;
8181
82- const lualibRequired = this . files . some ( f => f . code . includes ( 'require("lualib_bundle")' ) ) ;
82+ const lualibRequired = this . modules . some ( f => f . code . includes ( 'require("lualib_bundle")' ) ) ;
8383 if ( lualibRequired ) {
8484 const fileName = normalizeSlashes ( path . resolve ( this . rootDir , "lualib_bundle.lua" ) ) ;
85- this . files . unshift ( { fileName, code : getLuaLibBundle ( this . emitHost ) } ) ;
85+ this . modules . unshift ( { fileName, code : getLuaLibBundle ( this . emitHost ) } ) ;
8686 }
8787
8888 if ( isBundleEnabled ( this . options ) ) {
89- const [ bundleDiagnostics , bundleFile ] = getBundleResult ( this . program , this . emitHost , this . files , file =>
89+ const [ bundleDiagnostics , bundleChunk ] = getBundleChunk ( this . program , this . emitHost , this . modules , file =>
9090 this . createModuleId ( file . fileName )
9191 ) ;
9292 this . diagnostics . push ( ...bundleDiagnostics ) ;
93- return [ bundleFile ] ;
93+ return [ bundleChunk ] ;
9494 } else {
95- return this . files . map ( file => ( {
95+ return this . modules . map ( file => ( {
9696 ...file ,
9797 outputPath : this . moduleIdToOutputPath ( this . createModuleId ( file . fileName ) ) ,
9898 } ) ) ;
9999 }
100100 }
101101
102- private handleProcessedFile ( file : ProcessedFile ) {
103- const replacer : ResolveMacroReplacer = ( request : string ) => {
102+ private addModule ( module : Module ) {
103+ const dependencyResolver : MacroDependencyResolver = ( request : string ) => {
104104 let resolvedPath : string ;
105105 try {
106- const result = this . resolver . resolveSync ( { } , path . dirname ( file . fileName ) , request ) ;
106+ const result = this . resolver . resolveSync ( { } , path . dirname ( module . fileName ) , request ) ;
107107 assert ( typeof result === "string" ) ;
108108 resolvedPath = result ;
109109 } catch ( error ) {
110- this . diagnostics . push ( createResolutionErrorDiagnostic ( error . message , request , file . fileName ) ) ;
110+ this . diagnostics . push ( createResolutionErrorDiagnostic ( error . message , request , module . fileName ) ) ;
111111 return { error : error . message } ;
112112 }
113113
@@ -117,11 +117,11 @@ class Transpilation {
117117 resolvedPath . endsWith ( ".json" )
118118 ) {
119119 const message = `Resolved source file '${ resolvedPath } ' is not a part of the project.` ;
120- this . diagnostics . push ( createResolutionErrorDiagnostic ( message , request , file . fileName ) ) ;
120+ this . diagnostics . push ( createResolutionErrorDiagnostic ( message , request , module . fileName ) ) ;
121121 return { error : message } ;
122122 } else {
123123 this . seenFiles . add ( resolvedPath ) ;
124- this . handleProcessedFile ( {
124+ this . addModule ( {
125125 fileName : resolvedPath ,
126126 code : cast ( this . emitHost . readFile ( resolvedPath ) , isNonNull ) ,
127127 // TODO: Load source map files
@@ -132,16 +132,16 @@ class Transpilation {
132132 return this . createModuleId ( resolvedPath ) ;
133133 } ;
134134
135- if ( file . sourceMapNode ) {
136- replaceResolveMacroSourceNodes ( file . sourceMapNode , replacer ) ;
137- const { code, map } = file . sourceMapNode . toStringWithSourceMap ( ) ;
138- file . code = code ;
139- file . sourceMap = JSON . stringify ( map . toJSON ( ) ) ;
135+ if ( module . sourceMapNode ) {
136+ replaceResolveMacroSourceNodes ( module . sourceMapNode , dependencyResolver ) ;
137+ const { code, map } = module . sourceMapNode . toStringWithSourceMap ( ) ;
138+ module . code = code ;
139+ module . sourceMap = JSON . stringify ( map . toJSON ( ) ) ;
140140 } else {
141- file . code = replaceResolveMacroInSource ( file . code , replacer ) ;
141+ module . code = replaceResolveMacroInSource ( module . code , dependencyResolver ) ;
142142 }
143143
144- this . files . push ( file ) ;
144+ this . modules . push ( module ) ;
145145 }
146146
147147 private readonly scriptExtensions = [ ".ts" , ".tsx" , ".js" , ".jsx" ] ;
0 commit comments