Skip to content

Commit

Permalink
Add impoved ESM loading of contributors
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 4, 2021
1 parent 1e3ff16 commit 11e9345
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 25 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,12 @@ async function indexContributors(cwd, contributors) {
path = resolve.sync(contributors, {basedir: process.cwd()})
}

const exported = (await import(path)).default
const exported = await import(path)

if (Array.isArray(exported)) {
contributors = exported
} else if (typeof exported === 'object' && exported !== null) {
contributors = exported.contributors || exported.default
if (Array.isArray(exported.contributors)) {
contributors = exported.contributors
} else if (Array.isArray(exported.default)) {
contributors = exported.default
}
}

Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ To augment user metadata, configure the plugin in your `package.json`:
Where `contributors` is either:

* An array in the form of `[{ email, name, … }, … ]`;
* A module id or path to a file that exports `contributors` or
`{ contributors }`.
* A module id or path to a file that exports `contributors` as either the
default export or as a `contributors` named export specifier.

Note that `remark-git-contributors` excludes people that are not in Git history.
This way the `contributors` metadata can be reused in multiple projects.
Expand Down
6 changes: 5 additions & 1 deletion test/fixture/contributors-default.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
exports.default = [{email: 'test@localhost', github: 'test', twitter: 'test'}]
const contributors = [
{email: 'test@localhost', github: 'test', twitter: 'test'}
]

export default contributors
4 changes: 3 additions & 1 deletion test/fixture/contributors-duplicates.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
exports.default = ['test <test@localhost>', 'test two <test@localhost>']
const contributors = ['test <test@localhost>', 'test two <test@localhost>']

export default contributors
4 changes: 3 additions & 1 deletion test/fixture/contributors-invalid-exports.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module.exports = false
const contributors = false

export default contributors
6 changes: 5 additions & 1 deletion test/fixture/contributors-main.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module.exports = [{email: 'test@localhost', github: 'test', twitter: 'test'}]
const contributors = [
{email: 'test@localhost', github: 'test', twitter: 'test'}
]

export default contributors
2 changes: 1 addition & 1 deletion test/fixture/contributors-named.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exports.contributors = [
export const contributors = [
{email: 'test@localhost', github: 'test', twitter: 'test'}
]
4 changes: 3 additions & 1 deletion test/fixture/contributors-string.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
exports.default = ['test <test@localhost>']
const contributors = ['test <test@localhost>']

export default contributors
23 changes: 11 additions & 12 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,21 +391,20 @@ function run(fixture, options_, test) {
execFileSync('git', ['init', '.'], {cwd, stdio: 'ignore'})
}

if (pkgAuthor || pkgContributors || pkgBroken) {
let pkg = JSON.stringify({
name: 'example',
private: true,
author: pkgAuthor,
contributors: pkgContributors
})

if (pkgBroken) {
pkg = pkg.slice(1)
}
let pkg = JSON.stringify({
name: 'example',
type: 'module',
private: true,
author: pkgAuthor,
contributors: pkgContributors
})

fs.writeFileSync(path.join(cwd, 'package.json'), pkg)
if (pkgBroken) {
pkg = pkg.slice(1)
}

fs.writeFileSync(path.join(cwd, 'package.json'), pkg)

let index = -1
while (++index < gitUsers.length) {
const [name, email] = gitUsers[index]
Expand Down

0 comments on commit 11e9345

Please sign in to comment.