@@ -11,7 +11,7 @@ import { createTransformerWatcher } from "Project/transformers/createTransformer
1111import { getPluginConfigs } from "Project/transformers/getPluginConfigs" ;
1212import { getCustomPreEmitDiagnostics } from "Project/util/getCustomPreEmitDiagnostics" ;
1313import { LogService } from "Shared/classes/LogService" ;
14- import { PathTranslator } from "Shared/classes/PathTranslator" ;
14+ import { PathHint , PathTranslator } from "Shared/classes/PathTranslator" ;
1515import { ProjectType } from "Shared/constants" ;
1616import { warnings } from "Shared/diagnostics" ;
1717import { AirshipBuildFile , ProjectData } from "Shared/types" ;
@@ -20,6 +20,7 @@ import { benchmarkIfVerbose } from "Shared/util/benchmark";
2020import {
2121 AirshipBuildState ,
2222 BUILD_FILE ,
23+ CompliationContext ,
2324 EDITOR_FILE ,
2425 MultiTransformState ,
2526 transformSourceFile ,
@@ -46,6 +47,16 @@ function getReverseSymlinkMap(program: ts.Program) {
4647 return result ;
4748}
4849
50+ export function isPackage ( relativePath : string ) {
51+ return relativePath . startsWith ( "AirshipPackages" + path . sep ) ;
52+ }
53+
54+ interface FileWriteEntry {
55+ sourceFile : ts . SourceFile ;
56+ source : string ;
57+ context ?: CompliationContext ;
58+ }
59+
4960/**
5061 * 'transpiles' TypeScript project into a logically identical Luau project.
5162 *
@@ -58,7 +69,7 @@ export function compileFiles(
5869 buildState : AirshipBuildState ,
5970 sourceFiles : Array < ts . SourceFile > ,
6071) : ts . EmitResult {
61- const asJson = data . projectOptions . json ;
72+ const { json : asJson , publish : isPublish } = data . projectOptions ;
6273 const compilerOptions = program . getCompilerOptions ( ) ;
6374
6475 const watch = compilerOptions . watch ?? false ;
@@ -92,7 +103,7 @@ export function compileFiles(
92103
93104 LogService . writeLineIfVerbose ( `Now running TypeScript compiler:` ) ;
94105
95- const fileWriteQueue = new Array < { sourceFile : ts . SourceFile ; source : string } > ( ) ;
106+ const fileWriteQueue = new Array < FileWriteEntry > ( ) ;
96107 const fileMetadataWriteQueue = new Map < ts . SourceFile , string > ( ) ;
97108
98109 const progressMaxLength = `${ sourceFiles . length } /${ sourceFiles . length } ` . length ;
@@ -175,11 +186,24 @@ export function compileFiles(
175186 } ) ;
176187 }
177188
189+ if ( isPublish ) {
190+ const sharedDirectory = pathTranslator . getOutDir ( PathHint . Shared ) ;
191+ if ( fs . pathExistsSync ( sharedDirectory ) ) fs . removeSync ( sharedDirectory ) ;
192+
193+ const clientDirectory = pathTranslator . getOutDir ( PathHint . Client ) ;
194+ if ( fs . pathExistsSync ( clientDirectory ) ) fs . removeSync ( clientDirectory ) ;
195+
196+ const serverDirectory = pathTranslator . getOutDir ( PathHint . Server ) ;
197+ if ( fs . pathExistsSync ( serverDirectory ) ) fs . removeSync ( serverDirectory ) ;
198+ }
199+
178200 for ( let i = 0 ; i < sourceFiles . length ; i ++ ) {
179201 const sourceFile = proxyProgram . getSourceFile ( sourceFiles [ i ] . fileName ) ;
180202 assert ( sourceFile ) ;
181203 const progress = `${ i + 1 } /${ sourceFiles . length } ` . padStart ( progressMaxLength ) ;
182- benchmarkIfVerbose ( `${ progress } compile ${ path . relative ( process . cwd ( ) , sourceFile . fileName ) } ` , ( ) => {
204+ const relativePath = path . relative ( process . cwd ( ) , sourceFile . fileName ) ;
205+
206+ benchmarkIfVerbose ( `${ progress } compile ${ relativePath } ` , ( ) => {
183207 DiagnosticService . addDiagnostics ( ts . getPreEmitDiagnostics ( proxyProgram , sourceFile ) ) ;
184208 DiagnosticService . addDiagnostics ( getCustomPreEmitDiagnostics ( data , sourceFile ) ) ;
185209 if ( DiagnosticService . hasErrors ( ) ) return ;
@@ -200,12 +224,37 @@ export function compileFiles(
200224 sourceFile ,
201225 ) ;
202226
203- const luauAST = transformSourceFile ( transformState , sourceFile ) ;
204- if ( DiagnosticService . hasErrors ( ) ) return ;
227+ if ( isPublish && ! isPackage ( relativePath ) ) {
228+ const serverWriteEntry = transformState . useContext ( CompliationContext . Server , context => {
229+ const luauAST = transformSourceFile ( transformState , sourceFile ) ;
230+ if ( DiagnosticService . hasErrors ( ) ) return ;
231+ const source = renderAST ( luauAST ) ;
232+ return { sourceFile, source, context } satisfies FileWriteEntry ;
233+ } ) ;
234+
235+ if ( DiagnosticService . hasErrors ( ) || ! serverWriteEntry ) return ;
205236
206- const source = renderAST ( luauAST ) ;
237+ const clientWriteEntry = transformState . useContext ( CompliationContext . Client , context => {
238+ const luauAST = transformSourceFile ( transformState , sourceFile ) ;
239+ if ( DiagnosticService . hasErrors ( ) ) return ;
240+ const source = renderAST ( luauAST ) ;
241+ return { sourceFile, source, context } satisfies FileWriteEntry ;
242+ } ) ;
207243
208- fileWriteQueue . push ( { sourceFile, source } ) ;
244+ if ( ! clientWriteEntry ) return ;
245+
246+ if ( clientWriteEntry . source !== serverWriteEntry . source ) {
247+ fileWriteQueue . push ( clientWriteEntry ) ;
248+ fileWriteQueue . push ( serverWriteEntry ) ;
249+ } else {
250+ fileWriteQueue . push ( { ...serverWriteEntry , context : CompliationContext . Shared } ) ;
251+ }
252+ } else {
253+ const luauAST = transformSourceFile ( transformState , sourceFile ) ;
254+ if ( DiagnosticService . hasErrors ( ) ) return ;
255+ const source = renderAST ( luauAST ) ;
256+ fileWriteQueue . push ( { sourceFile, source } ) ;
257+ }
209258
210259 const airshipBehaviours = transformState . airshipBehaviours ;
211260
@@ -231,7 +280,8 @@ export function compileFiles(
231280 const airshipBehaviourMetadata = behaviour . metadata ;
232281
233282 if ( airshipBehaviourMetadata ) {
234- assert ( ! fileMetadataWriteQueue . has ( sourceFile ) ) ;
283+ if ( fileMetadataWriteQueue . has ( sourceFile ) ) continue ;
284+
235285 fileMetadataWriteQueue . set ( sourceFile , JSON . stringify ( airshipBehaviourMetadata , null , "\t" ) ) ;
236286 }
237287
@@ -263,8 +313,23 @@ export function compileFiles(
263313 let writeCount = 0 ;
264314 let metadataCount = 0 ;
265315
266- for ( const { sourceFile, source } of fileWriteQueue ) {
267- const outPath = pathTranslator . getOutputPath ( sourceFile . fileName ) ;
316+ for ( const { sourceFile, source, context } of fileWriteQueue ) {
317+ let pathHint : PathHint | undefined ;
318+ if ( context !== undefined ) {
319+ switch ( context ) {
320+ case CompliationContext . Client :
321+ pathHint = PathHint . Client ;
322+ break ;
323+ case CompliationContext . Server :
324+ pathHint = PathHint . Server ;
325+ break ;
326+ case CompliationContext . Shared :
327+ pathHint = PathHint . Shared ;
328+ break ;
329+ }
330+ }
331+
332+ const outPath = pathTranslator . getOutputPath ( sourceFile . fileName , pathHint ) ;
268333 const hasMetadata = fileMetadataWriteQueue . has ( sourceFile ) ;
269334 const metadataPathOutPath = outPath + ".json~" ;
270335
0 commit comments