Skip to content

Commit

Permalink
fix(sveltekit): Add conditional exports (#9872)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lms24 committed Dec 19, 2023
1 parent 4e0c460 commit 6d32228
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
10 changes: 10 additions & 0 deletions packages/sveltekit/package.json
Expand Up @@ -13,6 +13,16 @@
"module": "build/esm/index.server.js",
"browser": "build/esm/index.client.js",
"types": "build/types/index.types.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"browser": {
"import": "./build/esm/index.client.js",
"require": "./build/cjs/index.client.js"
},
"node": "./build/cjs/index.server.js"
}
},
"publishConfig": {
"access": "public"
},
Expand Down
48 changes: 30 additions & 18 deletions scripts/prepack.ts
Expand Up @@ -15,31 +15,28 @@ const NPM_IGNORE = fs.existsSync('.npmignore') ? '.npmignore' : '../../.npmignor

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

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

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

interface TypeVersions {
[key: string]: {
[key: string]: string[];
};
}

type PackageJsonExports = Partial<ConditionalExportEntryPoints> & {
[key: string]: Partial<ConditionalExportEntryPoints>;
};

interface PackageJson extends Record<string, unknown>, PackageJsonEntryPoints {
[EXPORT_MAP_ENTRY_POINT]: {
[key: string]: {
import: string;
require: string;
types: string;
node: string;
browser: string;
default: string;
};
};
[EXPORT_MAP_ENTRY_POINT]: PackageJsonExports;
[TYPES_VERSIONS_ENTRY_POINT]: TypeVersions;
}

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

/**
* Recursively traverses the exports object and rewrites all string values to remove the build directory.
*/
function rewriteConditionalExportEntryPoint(
exportsObject: Record<string, string | Record<string, string>>,
key: string,
): void {
const exportsField = exportsObject[key];
if (typeof exportsField === 'string') {
exportsObject[key] = exportsField.replace(`${buildDir}/`, '');
return;
}
Object.keys(exportsField).forEach(subfieldKey => {
rewriteConditionalExportEntryPoint(exportsField, subfieldKey);
});
}

if (newPkgJson[EXPORT_MAP_ENTRY_POINT]) {
Object.entries(newPkgJson[EXPORT_MAP_ENTRY_POINT]).forEach(([key, val]) => {
newPkgJson[EXPORT_MAP_ENTRY_POINT][key] = Object.entries(val).reduce(
(acc, [key, val]) => {
return { ...acc, [key]: val.replace(`${buildDir}/`, '') };
},
{} as typeof val,
);
Object.keys(newPkgJson[EXPORT_MAP_ENTRY_POINT]).forEach(key => {
rewriteConditionalExportEntryPoint(newPkgJson[EXPORT_MAP_ENTRY_POINT], key);
});
}

if (newPkgJson[TYPES_VERSIONS_ENTRY_POINT]) {
Object.entries(newPkgJson[TYPES_VERSIONS_ENTRY_POINT]).forEach(([key, val]) => {
newPkgJson[TYPES_VERSIONS_ENTRY_POINT][key] = Object.entries(val).reduce((acc, [key, val]) => {
const newKey = key.replace(`${buildDir}/`, '');
return { ...acc, [newKey]: val.map(v => v.replace(`${buildDir}/`, '')) };
return {
...acc,
[newKey]: val.map(v => v.replace(`${buildDir}/`, '')),
};
}, {});
});
}
Expand Down

0 comments on commit 6d32228

Please sign in to comment.