Skip to content

Commit

Permalink
fix(esm): avoid errors, slightly deal with arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Haroenv committed Aug 14, 2019
1 parent bc81351 commit f5eefa9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
33 changes: 32 additions & 1 deletion src/__tests__/formatPkg.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import formatPkg, { getRepositoryInfo } from '../formatPkg.js';
import formatPkg, { getRepositoryInfo, getMains } from '../formatPkg.js';
import rawPackages from './rawPackages.json';
import preact from './preact-simplified.json';
import isISO8601 from 'validator/lib/isISO8601.js';
Expand Down Expand Up @@ -502,4 +502,35 @@ describe('moduleTypes', () => {
test('preact (esm & umd)', () => {
expect(formatPkg(preact).moduleTypes).toEqual(['esm']);
});

test('silly broken package', () => {
expect(
formatPkg({
name: 'whoever',
lastPublisher: { name: 'unknown' },
main: [{ personalMain: 'index.mjs' }],
}).moduleTypes
).toEqual(['unknown']);
});
});

describe('getMain', () => {
test('main === string', () => {
expect(getMains({ main: 'index.js' })).toEqual(['index.js']);
});

test('first if array', () => {
expect(getMains({ main: ['index.js', 'ondex.jsx'] })).toEqual([
'index.js',
'ondex.jsx',
]);
});

test('index.js if undefined', () => {
expect(getMains({ main: undefined })).toEqual(['index.js']);
});

test('nothing if object', () => {
expect(getMains({ main: { something: 'cool.js' } })).toEqual([]);
});
});
43 changes: 31 additions & 12 deletions src/formatPkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ function getTypes(pkg) {
return { ts: 'included' };
}

const main = pkg.main || 'index.js';
// we only look at the first entry in main here
const main = getMains(pkg)[0];
if (typeof main === 'string' && main.endsWith('.js')) {
const dtsMain = main.replace(/js$/, 'd.ts');
return {
Expand Down Expand Up @@ -438,19 +439,37 @@ function getAlternativeNames(name) {
);
}

function getModuleTypes(cleaned) {
const main = cleaned.main || '';
const moduleTypes = [];
if (
typeof cleaned.module === 'string' ||
cleaned.type === 'module' ||
main.endsWith('.mjs')
) {
moduleTypes.push('esm');
export function getMains(pkg) {
if (Array.isArray(pkg.main)) {
// we can not deal with non-string mains for now
return pkg.main.filter(main => typeof main === 'string');
}
if (typeof pkg.main === 'string') {
return [pkg.main];
}
if (cleaned.type === 'commonjs' || main.endsWith('.cjs')) {
moduleTypes.push('cjs');
if (typeof pkg.main === 'undefined') {
return ['index.js'];
}
// we can not deal with non-array ||non-string mains for now
return [];
}

function getModuleTypes(pkg) {
const mains = getMains(pkg);
const moduleTypes = [];

mains.forEach(main => {
if (
typeof pkg.module === 'string' ||
pkg.type === 'module' ||
main.endsWith('.mjs')
) {
moduleTypes.push('esm');
}
if (pkg.type === 'commonjs' || main.endsWith('.cjs')) {
moduleTypes.push('cjs');
}
});

if (moduleTypes.length === 0) {
moduleTypes.push('unknown');
Expand Down

0 comments on commit f5eefa9

Please sign in to comment.