@@ -2,17 +2,9 @@ import ts from 'typescript';
22
33import { createSoundStdlibCompilerHost } from '../bundled/sound_stdlib.ts' ;
44import { loadConfig , type LoadedConfig } from '../project/config.ts' ;
5- import {
6- getAlwaysAvailableBuiltinMacroDefinitions ,
7- getAlwaysAvailableBuiltinMacroExports ,
8- getBuiltinMacroDefinitionsBySpecifier ,
9- getBuiltinMacroExportsBySpecifier ,
10- getBuiltinMacroFactoriesBySpecifier ,
11- } from '../frontend/builtin_macro_support.ts' ;
12- import { SemanticMacroExpansionRequiredError } from '../frontend/macro_errors.ts' ;
13- import { createProjectMacroEnvironment } from '../frontend/project_macro_support.ts' ;
5+ import { createBuiltinExpandedProgram } from '../frontend/builtin_macro_support.ts' ;
146import { dirname , join } from '../platform/path.ts' ;
15- import { createPreparedProgram , toSourceFileName } from '../frontend/project_frontend.ts' ;
7+ import { toSourceFileName } from '../frontend/project_frontend.ts' ;
168import { resolveSoundScriptAwareModule } from '../project/soundscript_packages.ts' ;
179import { type RuntimeTransformArtifact , transpileTypeScriptModuleToEsm } from './transform.ts' ;
1810
@@ -150,11 +142,11 @@ function getProjectContext(
150142 } ;
151143}
152144
153- function createPreparedRuntimeProgram (
145+ function createExpandedRuntimeProgram (
154146 projectContext : TransformProjectContext ,
155147 rootNames : readonly string [ ] ,
156148) {
157- return createPreparedProgram ( {
149+ return createBuiltinExpandedProgram ( {
158150 baseHost : createSoundStdlibCompilerHost (
159151 projectContext . loadedConfig . commandLine . options ,
160152 dirname ( projectContext . projectPath ) ,
@@ -167,80 +159,18 @@ function createPreparedRuntimeProgram(
167159 } ) ;
168160}
169161
170- function expandPreparedRuntimeProgram (
171- preparedProgram : ReturnType < typeof createPreparedRuntimeProgram > ,
172- options : { readonly deferToSemanticExpansion : boolean } ,
173- ) : ReadonlyMap < string , ts . SourceFile > {
174- const macroEnvironment = createProjectMacroEnvironment (
175- preparedProgram ,
176- getBuiltinMacroDefinitionsBySpecifier ( ) ,
177- getBuiltinMacroExportsBySpecifier (
178- options . deferToSemanticExpansion ? undefined : preparedProgram ,
179- { deferToSemanticExpansion : options . deferToSemanticExpansion } ,
180- ) ,
181- getBuiltinMacroFactoriesBySpecifier ( ) ,
182- getAlwaysAvailableBuiltinMacroDefinitions ( ) ,
183- getAlwaysAvailableBuiltinMacroExports (
184- options . deferToSemanticExpansion ? undefined : preparedProgram ,
185- { deferToSemanticExpansion : options . deferToSemanticExpansion } ,
186- ) ,
187- { deferToSemanticExpansion : options . deferToSemanticExpansion } ,
188- ) ;
189- try {
190- return macroEnvironment . expandPreparedProgram ( ) ;
191- } finally {
192- macroEnvironment . dispose ( ) ;
193- }
194- }
195-
196162export function createOnDemandTransformer (
197163 options : OnDemandTransformerOptions = { } ,
198164) : SyncOnDemandTransformer {
199165 const soundscriptCache = new Map < string , OnDemandTransformResult > ( ) ;
200166 const typeScriptCache = new Map < string , OnDemandTransformResult > ( ) ;
201167
202- function createFastSoundscriptTransform (
203- fileName : string ,
204- projectContext : TransformProjectContext ,
205- sourceText : string ,
206- ) : OnDemandTransformResult | null {
207- const sourceHash = ts . sys . createHash ?.( sourceText ) ?? sourceText ;
208- const cacheKey = `${ projectContext . projectPath } \u0000fast\u0000${ fileName } \u0000${ sourceHash } ` ;
209- const cached = soundscriptCache . get ( cacheKey ) ;
210- if ( cached ) {
211- return cached ;
212- }
213-
214- const preparedProgram = createPreparedRuntimeProgram ( projectContext , [ fileName ] ) ;
215- try {
216- const preparedSource = preparedProgram . preparedHost . getPreparedSourceFile ( fileName ) ;
217- if ( ( preparedSource ?. rewriteResult . macrosById . size ?? 0 ) > 0 ) {
218- return null ;
219- }
220- const artifact = transpileTypeScriptModuleToEsm (
221- fileName ,
222- `${ fileName } .js` ,
223- preparedSource ?. rewrittenText ?? sourceText ,
224- {
225- module : ts . ModuleKind . ES2022 ,
226- moduleSpecifierMode : 'preserve' ,
227- target : ts . ScriptTarget . ES2022 ,
228- } ,
229- ) ;
230- const result = {
231- ...artifact ,
232- projectPath : projectContext . projectPath ,
233- } ;
234- soundscriptCache . set ( cacheKey , result ) ;
235- return result ;
236- } finally {
237- preparedProgram . dispose ( ) ;
238- }
239- }
240-
241168 function transformModuleSync ( fileName : string ) : OnDemandTransformResult {
242169 const projectContext = getProjectContext ( fileName , options . projectPath ) ;
243- if ( projectContext && projectContext . loadedConfig . isSoundscriptSourceFile ( fileName ) ) {
170+ const isSoundscriptRuntimeSource = fileName . endsWith ( '.sts' ) ||
171+ ( projectContext ?. loadedConfig . isSoundscriptSourceFile ( fileName ) ?? false ) ;
172+
173+ if ( projectContext && isSoundscriptRuntimeSource ) {
244174 const sourceText = ts . sys . readFile ( fileName ) ;
245175 if ( sourceText === undefined ) {
246176 throw new Error ( `Could not read source file ${ fileName } .` ) ;
@@ -252,24 +182,11 @@ export function createOnDemandTransformer(
252182 if ( cachedFull ) {
253183 return cachedFull ;
254184 }
255- const fastTransformed = createFastSoundscriptTransform ( fileName , projectContext , sourceText ) ;
256- if ( fastTransformed ) {
257- return fastTransformed ;
258- }
259- const deferredCacheKey =
260- `${ projectContext . projectPath } \u0000deferred\u0000${ fileName } \u0000${ sourceHash } ` ;
261- const cachedDeferred = soundscriptCache . get ( deferredCacheKey ) ;
262- if ( cachedDeferred ) {
263- return cachedDeferred ;
264- }
265- const preparedProgram = createPreparedRuntimeProgram ( projectContext , [ fileName ] ) ;
185+ const expandedProgram = createExpandedRuntimeProgram ( projectContext , [ fileName ] ) ;
266186 try {
267- const transpileExpanded = (
268- expandedFiles : ReadonlyMap < string , ts . SourceFile > ,
269- ) : OnDemandTransformResult => {
270- const programFileName = preparedProgram . toProgramFileName ( fileName ) ;
271- const sourceFile = expandedFiles . get ( programFileName ) ??
272- preparedProgram . program . getSourceFile ( programFileName ) ;
187+ const transpileExpanded = ( ) : OnDemandTransformResult => {
188+ const programFileName = expandedProgram . preparedProgram . toProgramFileName ( fileName ) ;
189+ const sourceFile = expandedProgram . program . getSourceFile ( programFileName ) ;
273190 if ( ! sourceFile ) {
274191 throw new Error ( `Missing expanded source file for ${ fileName } .` ) ;
275192 }
@@ -289,29 +206,11 @@ export function createOnDemandTransformer(
289206 } ;
290207 } ;
291208
292- try {
293- const result = transpileExpanded (
294- expandPreparedRuntimeProgram ( preparedProgram , {
295- deferToSemanticExpansion : true ,
296- } ) ,
297- ) ;
298- soundscriptCache . set ( deferredCacheKey , result ) ;
299- return result ;
300- } catch ( error ) {
301- if ( ! ( error instanceof SemanticMacroExpansionRequiredError ) ) {
302- throw error ;
303- }
304- }
305-
306- const result = transpileExpanded (
307- expandPreparedRuntimeProgram ( preparedProgram , {
308- deferToSemanticExpansion : false ,
309- } ) ,
310- ) ;
209+ const result = transpileExpanded ( ) ;
311210 soundscriptCache . set ( fullCacheKey , result ) ;
312211 return result ;
313212 } finally {
314- preparedProgram . dispose ( ) ;
213+ expandedProgram . dispose ( ) ;
315214 }
316215 }
317216
0 commit comments