8000 Add initial commit of gatsby-source-lodash plugin. by zackhall · Pull Request #202 · lodash/lodash.com · GitHub
[go: up one dir, main page]

Skip to content

Add initial commit of gatsby-source-lodash plugin. #202

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

Merged
merged 2 commits into from
Apr 18, 2019
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
9 changes: 9 additions & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ module.exports = {
path: `${__dirname}/src/images`,
},
},
{
resolve: "gatsby-source-lodash",
options: {
versions: [{
version: '4.17.11',
url: 'https://raw.githubusercontent.com/lodash/lodash/4.17.11-npm/lodash.js',
}]
},
},
`gatsby-plugin-react-svg`,
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
Expand Down
25 changes: 25 additions & 0 deletions plugins/gatsby-source-lodash/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Gatsby Source Lodash

A simple Gatsby Source Plugin to grab method metadata from Lodash.

## Usage

In the `gatsby-config.js` file, add `gatsby-source-lodash` to the `module.exports`.

```js
module.exports = {
plugins: [
...
{
resolve: "gatsby-source-lodash",
options: {
versions: [{
version: '4.17.11',
url: 'https://raw.githubusercontent.com/lodash/lodash/4.17.11-npm/lodash.js',
}]
},
},
...
]
}
```
101 changes: 101 additions & 0 deletions plugins/gatsby-source-lodash/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"use strict"

const fetch = require("node-fetch")
const _ = require("lodash")
const Entry = require("./lib/entry.js")
const getEntries = Entry.getEntries

exports.sourceNodes = (
{ actions, createNodeId, createContentDigest },
configOptions
) => {
const { createNode } = actions

// Gatsby adds a configOption that's not needed for this plugin, delete it
delete configOptions.plugins

// TODO: Extend this to include multiple versions
const url = configOptions.versions[0].url
const version = configOptions.versions[0].version

const processEntry = entry => {
try {
// Exit early if the entry is private or has no name
if (!entry || !entry.getName() || entry.isPrivate()) {
return
}
} catch (err) {
// Some of the non-lodash methods were throwing a hard to trace error
// from the lib code. Rather than trace back, it was easier to catch
// since they aren't part of lodash.
return
}

// Special handling of aliases to get call names. Without this, there are
// circular JS object references.
const aliases = entry.getAliases().map(alias => {
const member = entry.getMembers(0) || ""
const name = alias.getName()
return `${member}.${name}`
})

const params = entry
.getParams()
.map(([type, name, desc]) => ({ type, name, desc }))

const entryData = {
aliases,
call: entry.getCall(),
category: entry.getCategory(),
desc: entry.getDesc(),
example: entry.getExample(),
hash: entry.getHash(),
isAlias: entry.isAlias(),
isCtor: entry.isCtor(),
isFunction: entry.isFunction(),
isLicense: entry.isLicense(),
isPlugin: entry.isPlugin(),
isStatic: entry.isStatic(),
lineNumber: entry.getLineNumber(),
members: entry.getMembers(),
name: entry.getName(),
params,
related: entry.getRelated(),
returns: entry.getReturns(),
since: entry.getSince(),
type: entry.getType(),
version,
}

const nodeData = {
...entryData,
children: [],
id: createNodeId(
`lodash_method_${version}_${entryData.hash}_${entryData.lineNumber}`
),
internal: {
content: JSON.stringify(entryData),
contentDigest: createContentDigest(JSON.stringify(entryData)),
type: "LodashMethod",
},
parent: null,
}

return nodeData
}

return fetch(url)
.then(res => res.text())
.then(body => {
const entries = getEntries(body)

// For each entry, create node.
_.each(entries, entry => {
entry = new Entry(entry, body)
const nodeData = processEntry(entry)
if (nodeData) {
createNode(nodeData)
}
})
})
}
Loading
0