@@ -11,24 +11,30 @@ import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
11
11
* to only include the build condition if found (e.g. "workerd") and remove everything else.
12
12
* If no build condition is found, it keeps everything as is.
13
13
* 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
15
15
* @param condition The build condition to look for
16
16
* @returns An object with the transformed exports and a boolean indicating if the build condition was found
17
17
*/
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
+ } {
19
25
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"
22
28
) ;
23
29
let hasBuildCondition = hasTopLevelBuildCondition ;
24
- for ( const [ key , value ] of Object . entries ( exports ) ) {
30
+ for ( const [ key , value ] of Object . entries ( conditionMap ) ) {
25
31
if ( typeof value === "object" && value != null ) {
26
32
const { transformedExports, hasBuildCondition : innerBuildCondition } = transformBuildCondition (
27
33
value as { [ key : string ] : unknown } ,
28
34
condition
29
35
) ;
30
36
transformed [ key ] = transformedExports ;
31
- hasBuildCondition = hasBuildCondition || innerBuildCondition ;
37
+ hasBuildCondition ||= innerBuildCondition ;
32
38
} else {
33
39
// If it doesn't have the build condition, we need to keep everything as is
34
40
// If it has the build condition, we need to keep only the build condition
@@ -55,17 +61,17 @@ interface PackageJson {
55
61
* @returns An object with the transformed package.json and a boolean indicating if the build condition was found
56
62
*/
57
63
export function transformPackageJson ( json : PackageJson ) {
58
- const transformed : PackageJson = { ... json } ;
64
+ const transformed : PackageJson = structuredClone ( json ) ;
59
65
let hasBuildCondition = false ;
60
66
if ( json . exports ) {
61
67
const exp = transformBuildCondition ( json . exports , "workerd" ) ;
62
68
transformed . exports = exp . transformedExports ;
63
- hasBuildCondition = exp . hasBuildCondition ;
69
+ hasBuildCondition || = exp . hasBuildCondition ;
64
70
}
65
71
if ( json . imports ) {
66
72
const imp = transformBuildCondition ( json . imports , "workerd" ) ;
67
73
transformed . imports = imp . transformedExports ;
68
- hasBuildCondition = hasBuildCondition || imp . hasBuildCondition ;
74
+ hasBuildCondition ||= imp . hasBuildCondition ;
69
75
}
70
76
return { transformed, hasBuildCondition } ;
71
77
}
@@ -86,7 +92,7 @@ export async function copyWorkerdPackages(options: BuildOptions, nodePackages: M
86
92
`Copying package using a workerd condition: ${ path . relative ( options . appPath , src ) } -> ${ path . relative ( options . appPath , dst ) } `
87
93
) ;
88
94
await fs . cp ( src , dst , { recursive : true , force : true } ) ;
89
- // Write the transformed package.json
95
+ // Overwrite with the transformed package.json
90
96
await fs . writeFile ( path . join ( dst , "package.json" ) , JSON . stringify ( transformed ) , "utf8" ) ;
91
97
}
92
98
} catch {
0 commit comments