Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/sveltekit/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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