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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import path from 'node:path';
import { toPosixPath } from '../../../utils/path';
import { isSubDirectory, toPosixPath } from '../../../utils/path';
import type { BuilderState } from '../builder-state';
import type { NormalizedEntryPoint, NormalizedLibraryOptions, PackageJsonData } from '../types';
import type { BundleEntryPointInput } from './bundler';
Expand Down Expand Up @@ -55,11 +55,17 @@ function normalizeEntryPoint(
}

const subpath = isPrimary ? '.' : `./${name}`;
const displayName = (isPrimary ? packageName : `${packageName}/${name}`).toLowerCase();
const displayName = isPrimary ? packageName : `${packageName}/${name}`;
const bundleName = getEntryPointBundleName(packageName, name);
const entryFilePath = toPosixPath(path.resolve(projectRoot, targetPath));

if (!/(?<!\.d)\.(?:ts|mts)$/.test(entryFilePath)) {
if (!isSubDirectory(projectRoot, entryFilePath)) {
throw new Error(
`Entry point '${key}' file path '${entryFilePath}' is outside the project root '${projectRoot}'.`,
);
}
Comment thread
alan-agius4 marked this conversation as resolved.

if (!/(?<!\.d)\.m?ts$/.test(entryFilePath)) {
throw new Error(
`Entry point '${key}' file path must be a TypeScript file ('.ts' or '.mts'): '${entryFilePath}'.`,
);
Expand Down Expand Up @@ -157,8 +163,8 @@ export function normalizeEntryPoints(
continue;
}

if (!isPrimary && !/\.m?ts$/.test(target)) {
// Static asset, stylesheet, or package.json export; preserve in package.json without compiling.
if (!isPrimary && !/(?<!\.d)\.m?ts$/.test(target)) {
// Static asset, stylesheet, declaration file, or package.json export; preserve in package.json without compiling.
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,46 @@ describe('entry-points pipeline', () => {
);
});

it('should ignore non-TypeScript exports for secondary subpaths', () => {
it('should preserve casing in displayName while lowercasing bundleName', () => {
const rawExports = {
'.': './src/public-api.ts',
'./myFeature': './my-feature/src/public-api.ts',
};

const entryPoints = normalizeEntryPoints(
rawExports,
projectRoot,
packageJsonPath,
'@My-Scope/My-Lib',
);

expect(entryPoints.get('.')).toEqual({
subpath: '.',
name: '.',
displayName: '@My-Scope/My-Lib',
bundleName: 'my-scope-my-lib',
entryFilePath: join(projectRoot, 'src/public-api.ts'),
isPrimary: true,
});

expect(entryPoints.get('myFeature')).toEqual({
subpath: './myFeature',
name: 'myFeature',
displayName: '@My-Scope/My-Lib/myFeature',
bundleName: 'my-scope-my-lib-myfeature',
entryFilePath: join(projectRoot, 'my-feature/src/public-api.ts'),
isPrimary: false,
});
});

it('should ignore non-TypeScript exports and static declaration files for secondary subpaths', () => {
const rawExports = {
'.': './src/public-api.ts',
'./package.json': './package.json',
'./styles.css': './styles.css',
'./theme': { sass: './theme.scss' },
'./custom-types': './src/custom-types.d.ts',
'./custom-esm-types': { default: './src/custom-types.d.mts' },
};

const entryPoints = normalizeEntryPoints(
Expand All @@ -260,6 +294,31 @@ describe('entry-points pipeline', () => {
expect(entryPoints.has('package.json')).toBeFalse();
expect(entryPoints.has('styles.css')).toBeFalse();
expect(entryPoints.has('theme')).toBeFalse();
expect(entryPoints.has('custom-types')).toBeFalse();
expect(entryPoints.has('custom-esm-types')).toBeFalse();
});

it('should throw when entry point file path escapes projectRoot', () => {
expect(() =>
normalizeEntryPoints(
{ '.': '../other-lib/src/public-api.ts' },
projectRoot,
packageJsonPath,
packageName,
),
).toThrowError(/is outside the project root/);

expect(() =>
normalizeEntryPoints(
{
'.': './src/public-api.ts',
'./testing': '../../outside/public-api.ts',
},
projectRoot,
packageJsonPath,
packageName,
),
).toThrowError(/is outside the project root/);
});

it('should throw when exports is missing or not an object/string', () => {
Expand Down
Loading