Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(modules): detect exports for moduleTypes #673

Merged
merged 3 commits into from
Jul 28, 2021
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
8 changes: 7 additions & 1 deletion src/@types/nice-package.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { GetPackage, GetUser, PackageRepo } from '../npm/types';
import type {
GetPackage,
GetUser,
GetVersion,
PackageRepo,
} from '../npm/types';

export interface NicePackageType {
_hasShrinkwrap?: false;
Expand All @@ -19,6 +24,7 @@ export interface NicePackageType {
main?: string | string[];
modified: string;
module?: string;
exports?: GetVersion['exports'];
name: string;
other: {
_id?: string;
Expand Down
62 changes: 61 additions & 1 deletion src/__tests__/formatPkg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import formatPkg, {
getRepositoryInfo,
getMains,
getVersions,
getExportKeys,
} from '../formatPkg';
import type { GetPackage } from '../npm/types';

Expand Down Expand Up @@ -560,13 +561,14 @@ describe('moduleTypes', () => {
formatPkg({
...BASE,
name: 'whoever',
// @ts-expect-error
main: [{ personalMain: 'index.mjs' }],
}).moduleTypes
).toEqual(['unknown']);
});
});

describe('getMain', () => {
describe('getMains', () => {
test('main === string', () => {
expect(getMains({ main: 'index.js' })).toEqual(['index.js']);
});
Expand All @@ -583,10 +585,68 @@ describe('getMain', () => {
});

test('nothing if object', () => {
// @ts-expect-error
expect(getMains({ main: { something: 'cool.js' } })).toEqual([]);
});
});

describe('getExportKeys', () => {
test('exports is missing', () => {
expect(getExportKeys(undefined)).toEqual([]);
});

test('exports is one level', () => {
expect(getExportKeys({ import: './lol.js', require: './cjs.js' })).toEqual([
'import',
'require',
]);
});

test('exports is two levels', () => {
expect(
getExportKeys({ '.': { import: './lol.js', require: './cjs.js' } })
).toEqual(['.', 'import', 'require']);
});

test('exports is repeated', () => {
expect(
getExportKeys({
something: { import: './lol.js', require: './cjs.js' },
bazoo: { import: './bazoo.js', require: './cjs.js' },
})
).toEqual(['something', 'bazoo', 'import', 'require', 'import', 'require']);
});

test('exports is many levels', () => {
expect(
getExportKeys({
something: { import: './lol.js', require: './cjs.js' },
bazoo: {
lol: { import: './bazoo.js', require: './cjs.js' },
kol: 'test.js',
mol: {
bol: {
condition: 'test.js',
},
},
},
})
).toEqual([
'something',
'bazoo',
'import',
'require',
'lol',
'kol',
'mol',
'import',
'require',
'bol',
'condition',
]);
});
});

describe('getVersions', () => {
test("renames 'time' to versions", () => {
expect(
Expand Down
64 changes: 50 additions & 14 deletions src/formatPkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,26 +575,62 @@ export function getMains(pkg: Pick<NicePackageType, 'main'>): string[] {
return [];
}

export function getExportKeys(
exp: NicePackageType['exports'] | string
): string[] {
if (typeof exp !== 'object') {
return [];
}
const keys = Object.keys(exp);
const nestedKeys = keys.flatMap((key) => getExportKeys(exp[key]));
return [...keys, ...nestedKeys];
}

const typeToModuleTypeMapping: Record<
Required<NicePackageType>['type'],
ModuleType
> = {
commonjs: 'cjs',
module: 'esm',
};

function getModuleTypes(pkg: NicePackageType): ModuleType[] {
const mains = getMains(pkg);
const moduleTypes: ModuleType[] = [];
const moduleTypes: Set<ModuleType> = new Set();

mains.forEach((main) => {
if (
typeof pkg.module === 'string' ||
pkg.type === 'module' ||
main.endsWith('.mjs')
) {
moduleTypes.push('esm');
// type is declared
if (pkg.type) {
moduleTypes.add(typeToModuleTypeMapping[pkg.type]);
}

// get all explicit exports (supporting cjs in esm or other way round)
// reference: https://nodejs.org/api/packages.html
const exportKeys = getExportKeys(pkg.exports);
if (exportKeys.includes('import')) {
moduleTypes.add('esm');
}
if (exportKeys.includes('require')) {
moduleTypes.add('cjs');
}

// module (non-standard) is declared
if (typeof pkg.module === 'string') {
moduleTypes.add('esm');
}

// check the extension of each of the "main" values
getMains(pkg).forEach((main) => {
if (main.endsWith('.mjs')) {
moduleTypes.add('esm');
}
if (pkg.type === 'commonjs' || main.endsWith('.cjs')) {
moduleTypes.push('cjs');
if (main.endsWith('.cjs')) {
moduleTypes.add('cjs');
}
});

if (moduleTypes.length === 0) {
moduleTypes.push('unknown');
// add a default value to make filtering possible
if (moduleTypes.size === 0) {
moduleTypes.add('unknown');
}

return moduleTypes;
return [...moduleTypes];
}
13 changes: 12 additions & 1 deletion src/npm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ export interface GetVersion {
tarball: string;
};
license?: string;

type?: 'module' | 'commonjs';
module?: string;
main?: string;
exports?: PackageExports;

maintainers: GetUser[];
name: string;
scripts?: Record<string, string>;
version: string;
deprecated?: string | boolean;
schematics?: string;
types?: string;
typings?: string;
}

export interface PackageRepo {
Expand All @@ -45,6 +53,10 @@ export interface PackageRepo {
directory?: string;
}

export interface PackageExports {
[key: string]: string | PackageExports;
}

export interface GetPackage {
_id: string;
_rev: string;
Expand All @@ -65,7 +77,6 @@ export interface GetPackage {
keywords?: string[] | string;
contributors?: Array<{ name: string }>;
repository?: PackageRepo;
schematics?: string;
}

export interface GetPackageLight {
Expand Down