@@ -14,6 +14,8 @@ import { getConfig, getPaths, projectSideIsEsm } from '@cedarjs/project-config'
1414
1515import { findApiFiles } from '../files.js'
1616
17+ import { applyContextWrapping } from './esbuild-plugin-cedar-context-wrapping.js'
18+
1719let BUILD_CTX : BuildContext | null = null
1820
1921export const buildApi = async ( ) => {
@@ -55,8 +57,20 @@ const runCedarBabelTransformsPlugin = {
5557 } ) ,
5658 )
5759 if ( transformedCode ?. code ) {
60+ // Apply the context-wrapping safeguard to API function handlers. This
61+ // is the standalone-esbuild equivalent of the Vite
62+ // cedarContextWrappingPlugin and the (Jest-only) babel plugin it
63+ // replaced.
64+ const functionsDir = normalizePath (
65+ path . join ( getPaths ( ) . api . src , 'functions' ) ,
66+ )
67+ const code = normalizePath ( args . path ) . startsWith ( functionsDir + '/' )
68+ ? ( applyContextWrapping ( transformedCode . code , {
69+ projectIsEsm : projectSideIsEsm ( 'api' ) ,
70+ } ) ?? transformedCode . code )
71+ : transformedCode . code
5872 return {
59- contents : transformedCode . code ,
73+ contents : code ,
6074 loader : 'js' ,
6175 }
6276 }
@@ -99,8 +113,16 @@ function createCedarViteApiPlugin(): Plugin {
99113 )
100114
101115 if ( transformedCode ?. code ) {
116+ const functionsDir = normalizePath (
117+ path . join ( cedarPaths . api . src , 'functions' ) ,
118+ )
119+ const code = normalizePath ( id ) . startsWith ( functionsDir + '/' )
120+ ? ( applyContextWrapping ( transformedCode . code , {
121+ projectIsEsm : isEsm ,
122+ } ) ?? transformedCode . code )
123+ : transformedCode . code
102124 return {
103- code : transformedCode . code ,
125+ code,
104126 map : transformedCode . map ?? null ,
105127 }
106128 }
0 commit comments