8000 Add initial commit of gatsby-source-lodash plugin. (#202) · lodash/lodash.com@7c90a8a · GitHub
[go: up one dir, main page]

Skip to content

Commit 7c90a8a

Browse files
zackhallveksen
authored andcommitted
Add initial commit of gatsby-source-lodash plugin. (#202)
1 parent ef1164f commit 7c90a8a

File tree

8 files changed

+1124
-0
lines changed

8 files changed

+1124
-0
lines changed

gatsby-config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ module.exports = {
1919
path: `${__dirname}/src/images`,
2020
},
2121
},
22+
{
23+
resolve: "gatsby-source-lodash" 10000 ,
24+
options: {
25+
versions: [{
26+
version: '4.17.11',
27+
url: 'https://raw.githubusercontent.com/lodash/lodash/4.17.11-npm/lodash.js',
28+
}]
29+
},
30+
},
2231
`gatsby-plugin-react-svg`,
2332
`gatsby-transformer-sharp`,
2433
`gatsby-plugin-sharp`,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Gatsby Source Lodash
2+
3+
A simple Gatsby Source Plugin to grab method metadata from Lodash.
4+
5+
## Usage
6+
7+
In the `gatsby-config.js` file, add `gatsby-source-lodash` to the `module.exports`.
8+
9+
```js
10+
module.exports = {
11+
plugins: [
12+
...
13+
{
14+
resolve: "gatsby-source-lodash",
15+
options: {
16+
versions: [{
17+
version: '4.17.11',
18+
url: 'https://raw.githubusercontent.com/lodash/lodash/4.17.11-npm/lodash.js',
19+
}]
20+
},
21+
},
22+
...
23+
]
24+
}
25+
```
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"use strict"
2+
3+
const fetch = require("node-fetch")
4+
const _ = require("lodash")
5+
const Entry = require("./lib/entry.js")
6+
const getEntries = Entry.getEntries
7+
8+
exports.sourceNodes = (
9+
{ actions, createNodeId, createContentDigest },
10+
configOptions
11+
) => {
12+
const { createNode } = actions
13+
14+
// Gatsby adds a configOption that's not needed for this plugin, delete it
15+
delete configOptions.plugins
16+
17+
// TODO: Extend this to include multiple versions
18+
const url = configOptions.versions[0].url
19+
const version = configOptions.versions[0].version
20+
21+
const processEntry = entry => {
22+
try {
23+
// Exit early if the entry is private or has no name
24+
if (!entry || !entry.getName() || entry.isPrivate()) {
25+
return
26+
}
27+
} catch (err) {
28+
// Some of the non-lodash methods were throwing a hard to trace error
29+
// from the lib code. Rather than trace back, it was easier to catch
30+
// since they aren't part of lodash.
31+
return
32+
}
33+
34+
// Special handling of aliases to get call names. Without this, there are
35+
// circular JS object references.
36+
const aliases = entry.getAliases().map(alias => {
37+
const member = entry.getMembers(0) || ""
38+
const name = alias.getName()
39+
return `${member}.${name}`
40+
})
41+
42+
const params = entry
43+
.getParams()
44+
.map(([type, name, desc]) => ({ type, name, desc }))
45+
46+
const entryData = {
47+
aliases,
48+
call: entry.getCall(),
49+
category: entry.getCategory(),
50+
desc: entry.getDesc(),
51+
example: entry.getExample(),
52+
hash: entry.getHash(),
53+
isAlias: entry.isAlias(),
54+
isCtor: entry.isCtor(),
55+
isFunction: entry.isFunction(),
56+
isLicense: entry.isLicense(),
57+
isPlugin: entry.isPlugin(),
58+
isStatic: entry.isStatic(),
59+
lineNumber: entry.getLineNumber(),
60+
members: entry.getMembers(),
61+
name: entry.getName(),
62+
params,
63+
related: entry.getRelated(),
64+
returns: entry.getReturns(),
65+
since: entry.getSince(),
66+
type: entry.getType(),
67+
version,
68+
}
69+
70+
const nodeData = {
71+
...entryData,
72+
children: [],
73+
id: createNodeId(
74+
`lodash_method_${version}_${entryData.hash}_${entryData.lineNumber}`
75+
),
76+
internal: {
77+
content: JSON.stringify(entryData),
78+
contentDigest: createContentDigest(JSON.stringify(entryData)),
79+
type: "LodashMethod",
80+
},
81+
parent: null,
82+
}
83+
84+
return nodeData
85+
}
86+
87+
return fetch(url)
88+
.then(res => res.text())
89+
.then(body => {
90+
const entries = getEntries(body)
91+
92+
// For each entry, create node.
93+
_.each(entries, entry => {
94+
entry = new Entry(entry, body)
95+
const nodeData = processEntry(entry)
96+
if (nodeData) {
97+
createNode(nodeData)
98+
}
99+
})
100+
})
101+
}

0 commit comments

Comments
 (0)
0