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(data): add flagging of type=module #386

Merged
merged 3 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
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',
},
moduleType: 'cjs', // esm | cjs | unknown
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Object {
"searchable(owner.name)",
"deprecated",
"types.ts",
"moduleType",
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
],
"customRanking": Array [
"desc(_searchInternal.downloadsMagnitude)",
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/__snapshots__/formatPkg.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Object {
},
"license": "Apache-2.0",
"modified": 1510790341980,
"moduleType": "unknown",
"name": "@atlaskit/input",
"objectID": "@atlaskit/input",
"originalAuthor": undefined,
Expand Down Expand Up @@ -261,6 +262,7 @@ Object {
},
"license": "MIT",
"modified": 1498153976616,
"moduleType": "unknown",
"name": "@atomic-package/tab",
"objectID": "@atomic-package/tab",
"originalAuthor": undefined,
Expand Down Expand Up @@ -383,6 +385,7 @@ Object {
},
"license": "MIT",
"modified": 1554896045753,
"moduleType": "unknown",
"name": "create-instantsearch-app",
"objectID": "create-instantsearch-app",
"originalAuthor": undefined,
Expand Down Expand Up @@ -457,6 +460,7 @@ Object {
"lastPublisher": null,
"license": null,
"modified": 1424001480609,
"moduleType": "unknown",
"name": "indexof",
"objectID": "indexof",
"originalAuthor": undefined,
Expand Down Expand Up @@ -540,6 +544,7 @@ Object {
},
"license": null,
"modified": NaN,
"moduleType": "unknown",
"name": "long-boy",
"objectID": "long-boy",
"originalAuthor": undefined,
Expand Down
61 changes: 61 additions & 0 deletions src/__tests__/formatPkg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,64 @@ describe('alternative names', () => {
`);
});
});

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

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

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

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

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

test('unknown', () => {
expect(
formatPkg({
name: 'irrelevant',
lastPublisher: { name: 'unknown' },
}).moduleType
).toEqual('unknown');
});
});
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const defaultConfig = {
'searchable(owner.name)',
'deprecated',
'types.ts',
'moduleType',
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
],
customRanking: [
'desc(_searchInternal.downloadsMagnitude)',
Expand Down
17 changes: 17 additions & 0 deletions src/formatPkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default function formatPkg(pkg) {
const dependencies = cleaned.dependencies || {};
const devDependencies = cleaned.devDependencies || {};
const alternativeNames = getAlternativeNames(cleaned.name);
const moduleType = getModuleType(cleaned);

const tags = pkg['dist-tags'];

Expand Down Expand Up @@ -94,6 +95,7 @@ export default function formatPkg(pkg) {
owners: (cleaned.owners || []).map(formatUser),
bin: cleaned.bin,
types,
moduleType,
lastCrawl: new Date().toISOString(),
_searchInternal: {
alternativeNames,
Expand Down Expand Up @@ -431,3 +433,18 @@ function getAlternativeNames(name) {
new Set([concatenatedName, splitName, dotJSName, normalName])
);
}

function getModuleType(cleaned) {
const main = cleaned.main || '';
if (
typeof cleaned.module === 'string' ||
cleaned.type === 'module' ||
main.endsWith('.mjs')
) {
return 'esm';
}
if (cleaned.type === 'commonjs' || main.endsWith('.cjs')) {
return 'cjs';
}
return 'unknown';
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
}