@@ -11,24 +11,30 @@ import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
1111 * to only include the build condition if found (e.g. "workerd") and remove everything else.
1212 * If no build condition is found, it keeps everything as is.
1313 * It also returns a boolean indicating if the build condition was found.
14- * @param exports The exports (or imports) object from the package.json
14+ * @param conditionMap The exports (or imports) object from the package.json
1515 * @param condition The build condition to look for
1616 * @returns An object with the transformed exports and a boolean indicating if the build condition was found
1717 */
18- export function transformBuildCondition ( exports : { [ key : string ] : unknown } , condition : string ) {
18+ export function transformBuildCondition (
19+ conditionMap : { [ key : string ] : unknown } ,
20+ condition : string
21+ ) : {
22+ transformedExports : { [ key : string ] : unknown } ;
23+ hasBuildCondition : boolean ;
24+ } {
1925 const transformed : { [ key : string ] : unknown } = { } ;
20- const hasTopLevelBuildCondition = Object . keys ( exports ) . some (
21- ( key ) => key === condition && typeof exports [ key ] === "string"
26+ const hasTopLevelBuildCondition = Object . keys ( conditionMap ) . some (
27+ ( key ) => key === condition && typeof conditionMap [ key ] === "string"
2228 ) ;
2329 let hasBuildCondition = hasTopLevelBuildCondition ;
24- for ( const [ key , value ] of Object . entries ( exports ) ) {
30+ for ( const [ key , value ] of Object . entries ( conditionMap ) ) {
2531 if ( typeof value === "object" && value != null ) {
2632 const { transformedExports, hasBuildCondition : innerBuildCondition } = transformBuildCondition (
2733 value as { [ key : string ] : unknown } ,
2834 condition
2935 ) ;
3036 transformed [ key ] = transformedExports ;
31- hasBuildCondition = hasBuildCondition || innerBuildCondition ;
37+ hasBuildCondition ||= innerBuildCondition ;
3238 } else {
3339 // If it doesn't have the build condition, we need to keep everything as is
3440 // If it has the build condition, we need to keep only the build condition
@@ -55,17 +61,17 @@ interface PackageJson {
5561 * @returns An object with the transformed package.json and a boolean indicating if the build condition was found
5662 */
5763export function transformPackageJson ( json : PackageJson ) {
58- const transformed : PackageJson = { ... json } ;
64+ const transformed : PackageJson = structuredClone ( json ) ;
5965 let hasBuildCondition = false ;
6066 if ( json . exports ) {
6167 const exp = transformBuildCondition ( json . exports , "workerd" ) ;
6268 transformed . exports = exp . transformedExports ;
63- hasBuildCondition = exp . hasBuildCondition ;
69+ hasBuildCondition || = exp . hasBuildCondition ;
6470 }
6571 if ( json . imports ) {
6672 const imp = transformBuildCondition ( json . imports , "workerd" ) ;
6773 transformed . imports = imp . transformedExports ;
68- hasBuildCondition = hasBuildCondition || imp . hasBuildCondition ;
74+ hasBuildCondition ||= imp . hasBuildCondition ;
6975 }
7076 return { transformed, hasBuildCondition } ;
7177}
@@ -86,7 +92,7 @@ export async function copyWorkerdPackages(options: BuildOptions, nodePackages: M
8692 `Copying package using a workerd condition: ${ path . relative ( options . appPath , src ) } -> ${ path . relative ( options . appPath , dst ) } `
8793 ) ;
8894 await fs . cp ( src , dst , { recursive : true , force : true } ) ;
89- // Write the transformed package.json
95+ // Overwrite with the transformed package.json
9096 await fs . writeFile ( path . join ( dst , "package.json" ) , JSON . stringify ( transformed ) , "utf8" ) ;
9197 }
9298 } catch {
0 commit comments