Skip to content

Commit

Permalink
feat(data): add flagging of type=module (#386)
Browse files Browse the repository at this point in the history
* feat(data): add flagging of type=module

I've looked around, and it seems like this is a sufficient heuristic to detect esm. I have explicitly left the most common case as unknown, so we can later find out which type those are.

References:
- https://nodejs.org/api/esm.html#esm_code_package_json_code_code_type_code_field
- https://github.com/rollup/rollup/wiki/pkg.module

There's also "jsnext:main", but that doesn't seem to be used anymore

* turn into array

* finish plural migration
  • Loading branch information
Haroenv committed Aug 7, 2019
1 parent 8463308 commit 7cd0765
Show file tree
Hide file tree
Showing 7 changed files with 486 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -121,6 +121,7 @@ For every single NPM package, we create a record in the Algolia index. The resul
ts: 'definitely-typed', // definitely-typed | included | false
definitelyTyped: '@types/babel__core',
},
moduleTypes: ['unknown'], // esm | cjs | unknown
humanDependents: '3.3k',
changelogFilename: null, // if babel-core had a changelog, it would be the raw GitHub url here
objectID: 'babel-core',
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/config.test.js.snap
Expand Up @@ -96,6 +96,7 @@ Object {
"searchable(owner.name)",
"deprecated",
"types.ts",
"moduleTypes",
],
"customRanking": Array [
"desc(_searchInternal.downloadsMagnitude)",
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/__snapshots__/formatPkg.test.js.snap
Expand Up @@ -46,6 +46,9 @@ Object {
},
"license": "Apache-2.0",
"modified": 1510790341980,
"moduleTypes": Array [
"unknown",
],
"name": "@atlaskit/input",
"objectID": "@atlaskit/input",
"originalAuthor": undefined,
Expand Down Expand Up @@ -261,6 +264,9 @@ Object {
},
"license": "MIT",
"modified": 1498153976616,
"moduleTypes": Array [
"unknown",
],
"name": "@atomic-package/tab",
"objectID": "@atomic-package/tab",
"originalAuthor": undefined,
Expand Down Expand Up @@ -383,6 +389,9 @@ Object {
},
"license": "MIT",
"modified": 1554896045753,
"moduleTypes": Array [
"unknown",
],
"name": "create-instantsearch-app",
"objectID": "create-instantsearch-app",
"originalAuthor": undefined,
Expand Down Expand Up @@ -457,6 +466,9 @@ Object {
"lastPublisher": null,
"license": null,
"modified": 1424001480609,
"moduleTypes": Array [
"unknown",
],
"name": "indexof",
"objectID": "indexof",
"originalAuthor": undefined,
Expand Down Expand Up @@ -540,6 +552,9 @@ Object {
},
"license": null,
"modified": NaN,
"moduleTypes": Array [
"unknown",
],
"name": "long-boy",
"objectID": "long-boy",
"originalAuthor": undefined,
Expand Down
66 changes: 66 additions & 0 deletions src/__tests__/formatPkg.test.js
@@ -1,5 +1,6 @@
import formatPkg from '../formatPkg.js';
import rawPackages from './rawPackages.json';
import preact from './preact-simplified.json';
import isISO8601 from 'validator/lib/isISO8601.js';

it('transforms correctly', () => {
Expand Down Expand Up @@ -441,3 +442,68 @@ describe('alternative names', () => {
`);
});
});

describe('moduleTypes', () => {
test('type=module', () => {
expect(
formatPkg({
name: 'irrelevant',
lastPublisher: { name: 'unknown' },
type: 'module',
}).moduleTypes
).toEqual(['esm']);
});

test('type=commonjs', () => {
expect(
formatPkg({
name: 'irrelevant',
lastPublisher: { name: 'unknown' },
type: 'commonjs',
}).moduleTypes
).toEqual(['cjs']);
});

test('module=xxx.js', () => {
expect(
formatPkg({
name: 'irrelevant',
lastPublisher: { name: 'unknown' },
module: 'index.js',
}).moduleTypes
).toEqual(['esm']);
});

test('main: index.mjs', () => {
expect(
formatPkg({
name: 'irrelevant',
lastPublisher: { name: 'unknown' },
main: 'index.mjs',
}).moduleTypes
).toEqual(['esm']);
});

test('main: index.cjs', () => {
expect(
formatPkg({
name: 'irrelevant',
lastPublisher: { name: 'unknown' },
main: 'index.cjs',
}).moduleTypes
).toEqual(['cjs']);
});

test('unknown', () => {
expect(
formatPkg({
name: 'irrelevant',
lastPublisher: { name: 'unknown' },
}).moduleTypes
).toEqual(['unknown']);
});

test('preact (esm & umd)', () => {
expect(formatPkg(preact).moduleTypes).toEqual(['esm']);
});
});

0 comments on commit 7cd0765

Please sign in to comment.