Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { createRequire } from 'node:module';
import path from 'node:path';
import { toPosixPath } from '../../../utils/path';
import type { NormalizedLibraryOptions, PackageJsonData } from '../types';
import {
FESM_OUTPUT_DIR,
Expand All @@ -17,6 +18,25 @@ import {
loadPackageJson,
} from './utils';

const TS_SOURCE_FILE_REGEX = /(?<!\.d)\.m?ts$/;

/**
* Normalizes an export subpath key to a canonical relative subpath (e.g., `'.'` or `'./testing'`).
*
* @param key Raw export subpath key from `package.json`.
* @returns The normalized POSIX subpath key.
*/
function normalizeExportSubpathKey(key: string): string {
const posixKey = toPosixPath(key);
if (posixKey === '.') {
return '.';
}

const normalized = (posixKey.startsWith('./') ? posixKey : `./${posixKey}`).replace(/\/+$/, '');

return normalized || '.';
}

/**
* Generates the APF package.json and secondary entry point package.json manifests.
*
Expand All @@ -42,13 +62,6 @@ export async function generatePackageManifests(
...restPackageJson
} = rawPackageJson;

const exportsMap: Record<string, unknown> = {
...(typeof userExports === 'object' && userExports !== null && !Array.isArray(userExports)
? userExports
: {}),
'./package.json': { default: './package.json' },
};

const primaryEntryPoint = options.entryPoints.get('.');
if (!primaryEntryPoint) {
throw new Error(`Primary entry point '.' was not found in entryPoints.`);
Expand All @@ -60,7 +73,29 @@ export async function generatePackageManifests(
const primaryFesm = `./${FESM_OUTPUT_DIR}/${primaryName}.mjs`;
const primaryDts = `./${TYPES_OUTPUT_DIR}/${primaryName}.d.ts`;

exportsMap['.'] = createExportConditions(exportsMap['.'], primaryDts, primaryFesm);
const normalizedUserExports: Record<string, unknown> = {};
if (typeof userExports === 'object' && userExports !== null && !Array.isArray(userExports)) {
const keys = Object.keys(userExports);
if (keys.length > 0 && !keys.some((k) => k.startsWith('.'))) {
normalizedUserExports['.'] = userExports;
} else {
for (const [key, value] of Object.entries(userExports)) {
normalizedUserExports[normalizeExportSubpathKey(key)] = value;
}
}
}

const {
'.': primaryUserExport,
'./package.json': _pkgJsonExport,
...otherUserExports
} = normalizedUserExports;

const exportsMap: Record<string, unknown> = {
'.': createExportConditions(primaryUserExport, primaryDts, primaryFesm),
'./package.json': { default: './package.json' },
...otherUserExports,
};

const distPackageJson: PackageJsonData = {
...restPackageJson,
Expand Down Expand Up @@ -174,11 +209,22 @@ function createExportConditions(
? (existingConditions as Record<string, unknown>)
: {};

const { types: _types, default: _default, ...otherConditions } = existing;
const customConditions: Record<string, unknown> = {};
for (const [key, value] of Object.entries(existing)) {
if (key === 'types' || key === 'default') {
continue;
}

if (typeof value === 'string' && TS_SOURCE_FILE_REGEX.test(value)) {
continue;
}

customConditions[key] = value;
}

return {
types: dtsPath,
...otherConditions,
...customConditions,
default: fesmPath,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ describe('generatePackageManifests', () => {
'./styles.css': './styles.css',
'./scss/*': './scss/*',
'.': {
development: './src/index.ts',
development: './fesm2022/my-lib-dev.mjs',
},
},
},
Expand All @@ -277,12 +277,152 @@ describe('generatePackageManifests', () => {
'./package.json': { default: './package.json' },
'.': {
types: './types/my-lib.d.ts',
development: './src/index.ts',
development: './fesm2022/my-lib-dev.mjs',
default: './fesm2022/my-lib.mjs',
},
});
});

it('should handle root conditional exports without spreading condition keys at top level', async () => {
const options = createOptions({
packageJson: {
name: 'my-lib',
version: '1.0.0',
exports: {
default: './src/public-api.ts',
custom: './custom.mjs',
},
},
});

const files = await generatePackageManifests(options, false);
const result = getRootPackageJson(files);

expect(result.exports).toEqual({
'.': {
types: './types/my-lib.d.ts',
custom: './custom.mjs',
default: './fesm2022/my-lib.mjs',
},
'./package.json': { default: './package.json' },
});
});

it('should normalize subpath keys in user exports and merge conditions for secondary entry points', async () => {
const options1 = createOptions(
{
packageJson: {
name: 'my-lib',
version: '1.0.0',
exports: {
'.': './src/public-api.ts',
testing: './testing/src/public-api.ts',
},
},
},
true,
);

const result1 = getRootPackageJson(await generatePackageManifests(options1, false));
expect(result1.exports).toEqual({
'.': {
types: './types/my-lib.d.ts',
default: './fesm2022/my-lib.mjs',
},
'./package.json': { default: './package.json' },
'./testing': {
types: './types/my-lib-testing.d.ts',
default: './fesm2022/my-lib-testing.mjs',
},
});

const options2 = createOptions(
{
packageJson: {
name: 'my-lib',
version: '1.0.0',
exports: {
'./': './src/public-api.ts',
'./testing/': {
custom: './testing/custom.mjs',
default: './testing/src/public-api.ts',
},
},
},
},
true,
);

const result2 = getRootPackageJson(await generatePackageManifests(options2, false));
expect(result2.exports).toEqual({
'.': {
types: './types/my-lib.d.ts',
default: './fesm2022/my-lib.mjs',
},
'./package.json': { default: './package.json' },
'./testing': {
types: './types/my-lib-testing.d.ts',
custom: './testing/custom.mjs',
default: './fesm2022/my-lib-testing.mjs',
},
});
});

it('should omit user export conditions pointing to .ts or .mts source files', async () => {
const options = createOptions({
packageJson: {
name: 'my-lib',
version: '1.0.0',
exports: {
'.': {
import: './src/public-api.ts',
module: './src/public-api.mts',
development: './src/index.ts',
customTypes: './custom.d.ts',
custom: './custom.mjs',
default: './src/public-api.ts',
},
},
},
});

const files = await generatePackageManifests(options, false);
const result = getRootPackageJson(files);

expect(result.exports).toEqual({
'.': {
types: './types/my-lib.d.ts',
customTypes: './custom.d.ts',
custom: './custom.mjs',
default: './fesm2022/my-lib.mjs',
},
'./package.json': { default: './package.json' },
});
});

it('should ensure "." and "./package.json" appear first in exports even if "./package.json" is listed before "."', async () => {
const options = createOptions({
packageJson: {
name: 'my-lib',
version: '1.0.0',
exports: {
'./styles.css': './styles.css',
'./package.json': './package.json',
'.': './src/public-api.ts',
},
},
});

const files = await generatePackageManifests(options, false);
const result = getRootPackageJson(files);

expect(Object.keys(result.exports as Record<string, unknown>)).toEqual([
'.',
'./package.json',
'./styles.css',
]);
});

it('should default sideEffects to false if not specified, and preserve when set', async () => {
const files1 = await generatePackageManifests(
createOptions({
Expand All @@ -306,6 +446,18 @@ describe('generatePackageManifests', () => {
false,
);
expect(getRootPackageJson(files2).sideEffects).toEqual(['*.css']);

const files3 = await generatePackageManifests(
createOptions({
packageJson: {
name: 'my-lib',
version: '1.0.0',
sideEffects: true,
},
}),
false,
);
expect(getRootPackageJson(files3).sideEffects).toBeTrue();
});

it('should conditionally inject tslib into dependencies only when hasTslibImport is true', async () => {
Expand Down
Loading