@@ -28,55 +28,46 @@ export async function buildBackendFunctions(
2828) : Promise < string > {
2929 const outDir = await mkdtemp ( path . join ( tmpdir ( ) , 'dd-apps-backend-' ) ) ;
3030
31- const virtualEntries : Record < string , string > = { } ;
32- const input : Record < string , string > = { } ;
31+ log . debug ( `Building ${ functions . length } backend function(s) via vite.build()` ) ;
3332
33+ // Build each function individually so that each output is a single
34+ // self-contained JS file
3435 for ( const func of functions ) {
3536 const virtualId = `${ VIRTUAL_PREFIX } ${ func . name } ` ;
36- virtualEntries [ virtualId ] = generateVirtualEntryContent (
37- func . name ,
38- func . entryPath ,
39- buildRoot ,
40- ) ;
41- input [ func . name ] = virtualId ;
42- }
43-
44- log . debug ( `Building ${ functions . length } backend function(s) via vite.build()` ) ;
37+ const virtualContent = generateVirtualEntryContent ( func . name , func . entryPath , buildRoot ) ;
4538
46- const baseConfig = getBaseBackendBuildConfig ( buildRoot , virtualEntries ) ;
39+ const baseConfig = getBaseBackendBuildConfig ( buildRoot , { [ virtualId ] : virtualContent } ) ;
4740
48- // Production: build all functions in one vite.build() call, writing each to
49- // disk as a named file so the archive/upload step can collect them.
50- // Uses multi-entry input (one per function) with \0-prefixed virtual IDs —
51- // the \0 convention prevents other plugins from processing these IDs.
52- const result = await viteBuild ( {
53- ...baseConfig ,
54- build : {
55- ...baseConfig . build ,
56- write : true ,
57- outDir,
58- emptyOutDir : false ,
59- rollupOptions : {
60- ...baseConfig . build . rollupOptions ,
61- input,
62- output : { ...baseConfig . build . rollupOptions . output , entryFileNames : '[name].js' } ,
41+ // eslint-disable-next-line no-await-in-loop
42+ const result = await viteBuild ( {
43+ ...baseConfig ,
44+ build : {
45+ ...baseConfig . build ,
46+ write : true ,
47+ outDir,
48+ emptyOutDir : false ,
49+ rollupOptions : {
50+ ...baseConfig . build . rollupOptions ,
51+ input : { [ func . name ] : virtualId } ,
52+ output : {
53+ ...baseConfig . build . rollupOptions . output ,
54+ entryFileNames : '[name].js' ,
55+ } ,
56+ } ,
6357 } ,
64- } ,
65- } ) ;
58+ } ) ;
6659
67- const output = Array . isArray ( result ) ? result [ 0 ] : result ;
60+ const output = Array . isArray ( result ) ? result [ 0 ] : result ;
6861
69- // viteBuild always returns RolldownOutput here since we don't set build.watch.
70- // RolldownWatcher would only be returned if watch mode were enabled.
71- if ( 'output' in output ) {
72- for ( const chunk of output . output ) {
73- if ( chunk . type !== 'chunk' || ! chunk . isEntry ) {
74- continue ;
62+ if ( 'output' in output ) {
63+ for ( const chunk of output . output ) {
64+ if ( chunk . type !== 'chunk' || ! chunk . isEntry ) {
65+ continue ;
66+ }
67+ const absolutePath = path . resolve ( outDir , chunk . fileName ) ;
68+ backendOutputs . set ( func . name , absolutePath ) ;
69+ log . debug ( `Backend function "${ func . name } " output: ${ absolutePath } ` ) ;
7570 }
76- const funcName = chunk . name ;
77- const absolutePath = path . resolve ( outDir , chunk . fileName ) ;
78- backendOutputs . set ( funcName , absolutePath ) ;
79- log . debug ( `Backend function "${ funcName } " output: ${ absolutePath } ` ) ;
8071 }
8172 }
8273
0 commit comments