Skip to content

Commit 7b7a664

Browse files
committed
adjust exports field based on review suggestions
adjust prepack script
1 parent e9f7678 commit 7b7a664

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

packages/sveltekit/package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
"browser": "build/esm/index.client.js",
1515
"types": "build/types/index.types.d.ts",
1616
"exports": {
17-
"browser": "./build/esm/index.client.js",
18-
"node": "./build/cjs/index.server.js"
17+
"./package.json": "./package.json",
18+
".": {
19+
"browser": {
20+
"import": "./build/esm/index.client.js",
21+
"require": "./build/cjs/index.client.js"
22+
},
23+
"node": "./build/cjs/index.server.js"
24+
}
1925
},
2026
"publishConfig": {
2127
"access": "public"

scripts/prepack.ts

+24-17
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ interface TypeVersions {
3131
};
3232
}
3333

34+
type PackageJsonExports = Partial<ConditionalExportEntryPoints> & {
35+
[key: string]: Partial<ConditionalExportEntryPoints>;
36+
};
37+
3438
interface PackageJson extends Record<string, unknown>, PackageJsonEntryPoints {
35-
[EXPORT_MAP_ENTRY_POINT]: Partial<ConditionalExportEntryPoints> & {
36-
[key: string]: Partial<ConditionalExportEntryPoints>;
37-
};
39+
[EXPORT_MAP_ENTRY_POINT]: PackageJsonExports;
3840
[TYPES_VERSIONS_ENTRY_POINT]: TypeVersions;
3941
}
4042

@@ -70,21 +72,26 @@ ENTRY_POINTS.filter(entryPoint => newPkgJson[entryPoint]).forEach(entryPoint =>
7072
newPkgJson[entryPoint] = newPkgJson[entryPoint].replace(`${buildDir}/`, '');
7173
});
7274

75+
/**
76+
* Recursively traverses the exports object and rewrites all string values to remove the build directory.
77+
*/
78+
function rewriteConditionalExportEntryPoint(
79+
exportsObject: Record<string, string | Record<string, string>>,
80+
key: string,
81+
): void {
82+
const exportsField = exportsObject[key];
83+
if (typeof exportsField === 'string') {
84+
exportsObject[key] = exportsField.replace(`${buildDir}/`, '');
85+
return;
86+
}
87+
Object.keys(exportsField).forEach(subfieldKey => {
88+
rewriteConditionalExportEntryPoint(exportsField, subfieldKey);
89+
});
90+
}
91+
7392
if (newPkgJson[EXPORT_MAP_ENTRY_POINT]) {
74-
Object.entries(newPkgJson[EXPORT_MAP_ENTRY_POINT]).forEach(([key, val]) => {
75-
if (typeof val === 'string') {
76-
// case 1: key is already a conditional export entry point
77-
// @ts-expect-error I'm too dumb for TS :'D
78-
newPkgJson[EXPORT_MAP_ENTRY_POINT][key] = val.replace(`${buildDir}/`, '');
79-
return;
80-
}
81-
// case 2: key is a sub-path export
82-
newPkgJson[EXPORT_MAP_ENTRY_POINT][key] = Object.entries(val).reduce(
83-
(acc, [key, val]) => {
84-
return { ...acc, [key]: val.replace(`${buildDir}/`, '') };
85-
},
86-
{} as typeof val,
87-
);
93+
Object.keys(newPkgJson[EXPORT_MAP_ENTRY_POINT]).forEach(key => {
94+
rewriteConditionalExportEntryPoint(newPkgJson[EXPORT_MAP_ENTRY_POINT], key);
8895
});
8996
}
9097

0 commit comments

Comments
 (0)