Skip to content

Commit 6d32228

Browse files
authored
fix(sveltekit): Add conditional exports (#9872)
Looks like Vite 5 module resolution for `@sentry/sveltekit` only works with defining conditional exports. Tested this locally with a Sverdle kit@1, kit@2 and the syntax website.
1 parent 4e0c460 commit 6d32228

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

packages/sveltekit/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
"module": "build/esm/index.server.js",
1414
"browser": "build/esm/index.client.js",
1515
"types": "build/types/index.types.d.ts",
16+
"exports": {
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+
}
25+
},
1626
"publishConfig": {
1727
"access": "public"
1828
},

scripts/prepack.ts

+30-18
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,28 @@ const NPM_IGNORE = fs.existsSync('.npmignore') ? '.npmignore' : '../../.npmignor
1515

1616
const ASSETS = ['README.md', 'LICENSE', 'package.json', NPM_IGNORE] as const;
1717
const ENTRY_POINTS = ['main', 'module', 'types', 'browser'] as const;
18+
const CONDITIONAL_EXPORT_ENTRY_POINTS = ['import', 'require', ...ENTRY_POINTS] as const;
1819
const EXPORT_MAP_ENTRY_POINT = 'exports';
1920
const TYPES_VERSIONS_ENTRY_POINT = 'typesVersions';
2021

2122
const packageWithBundles = process.argv.includes('--bundles');
2223
const buildDir = packageWithBundles ? NPM_BUILD_DIR : BUILD_DIR;
2324

2425
type PackageJsonEntryPoints = Record<(typeof ENTRY_POINTS)[number], string>;
26+
type ConditionalExportEntryPoints = Record<(typeof CONDITIONAL_EXPORT_ENTRY_POINTS)[number], string>;
2527

2628
interface TypeVersions {
2729
[key: string]: {
2830
[key: string]: string[];
2931
};
3032
}
3133

34+
type PackageJsonExports = Partial<ConditionalExportEntryPoints> & {
35+
[key: string]: Partial<ConditionalExportEntryPoints>;
36+
};
37+
3238
interface PackageJson extends Record<string, unknown>, PackageJsonEntryPoints {
33-
[EXPORT_MAP_ENTRY_POINT]: {
34-
[key: string]: {
35-
import: string;
36-
require: string;
37-
types: string;
38-
node: string;
39-
browser: string;
40-
default: string;
41-
};
42-
};
39+
[EXPORT_MAP_ENTRY_POINT]: PackageJsonExports;
4340
[TYPES_VERSIONS_ENTRY_POINT]: TypeVersions;
4441
}
4542

@@ -75,22 +72,37 @@ ENTRY_POINTS.filter(entryPoint => newPkgJson[entryPoint]).forEach(entryPoint =>
7572
newPkgJson[entryPoint] = newPkgJson[entryPoint].replace(`${buildDir}/`, '');
7673
});
7774

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+
7892
if (newPkgJson[EXPORT_MAP_ENTRY_POINT]) {
79-
Object.entries(newPkgJson[EXPORT_MAP_ENTRY_POINT]).forEach(([key, val]) => {
80-
newPkgJson[EXPORT_MAP_ENTRY_POINT][key] = Object.entries(val).reduce(
81-
(acc, [key, val]) => {
82-
return { ...acc, [key]: val.replace(`${buildDir}/`, '') };
83-
},
84-
{} as typeof val,
85-
);
93+
Object.keys(newPkgJson[EXPORT_MAP_ENTRY_POINT]).forEach(key => {
94+
rewriteConditionalExportEntryPoint(newPkgJson[EXPORT_MAP_ENTRY_POINT], key);
8695
});
8796
}
8897

8998
if (newPkgJson[TYPES_VERSIONS_ENTRY_POINT]) {
9099
Object.entries(newPkgJson[TYPES_VERSIONS_ENTRY_POINT]).forEach(([key, val]) => {
91100
newPkgJson[TYPES_VERSIONS_ENTRY_POINT][key] = Object.entries(val).reduce((acc, [key, val]) => {
92101
const newKey = key.replace(`${buildDir}/`, '');
93-
return { ...acc, [newKey]: val.map(v => v.replace(`${buildDir}/`, '')) };
102+
return {
103+
...acc,
104+
[newKey]: val.map(v => v.replace(`${buildDir}/`, '')),
105+
};
94106
}, {});
95107
});
96108
}

0 commit comments

Comments
 (0)