10000 Add initial commit of gatsby-source-lodash plugin. · lodash/lodash.com@b462c3c · GitHub
[go: up one dir, main page]

Skip to content

Commit b462c3c

Browse files
committed
Add initial commit of gatsby-source-lodash plugin.
1 parent ef1164f commit b462c3c

File tree

8 files changed

+1114
-0
lines changed

8 files changed

+1114
-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",
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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
'use strict';
2+
3+
const fetch = require("node-fetch"),
4+
_ = require("lodash"),
5+
Entry = require('./lib/entry.js'),
6+
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+
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+
name = alias.getName();
39+
return `${member}.${name}`;
40+
})
41+
42+
const params = entry.getParams().map(
43+
([type, name, desc]) => ({ type, name, desc }));
44+
45+
const entryData = {
46+
aliases,
47+
version,
48+
params,
49+
call: entry.getCall(),
50+
category: entry.getCategory(),
51+
desc: entry.getDesc(),
52+
example: entry.getExample(),
53+
hash: entry.getHash(),
54+
lineNumber: entry.getLineNumber(),
55+
members: entry.getMembers(),
56+
name: entry.getName(),
57+
related: entry.getRelated(),
58+
returns: entry.getReturns(),
59+
since: entry.getSince(),
60+
type: entry.getType(),
61+
isAlias: entry.isAlias(),
62+
isCtor: entry.isCtor(),
63+
isFunction: entry.isFunction(),
64+
isLicense: entry.isLicense(),
65+
isPlugin: entry.isPlugin(),
66+
isStatic: entry.isStatic(),
67+
}
68+
69+
const nodeData = _.assign({}, entryData, {
70+
id: createNodeId(`lodash_method_${version}_${entryData.hash}_${entryData.lineNumber}`),
71+
parent: null,
72+
children: [],
73+
internal: {
74+
type: 'LodashMethod',
75+
content: JSON.stringify(entryData),
76+
contentDigest: createContentDigest(JSON.stringify(entryData)),
77+
}
78+
});
79+
80+
return nodeData;
81+
}
82+
83+
return (
84+
fetch(url)
85+
.then(res => res.text())
86+
.then(body => {
87+
const entries = getEntries(body);
88+
89+
// For each entry, create node.
90+
_.each(entries, entry => {
91+
entry = new Entry(entry, body);
92+
const nodeData = processEntry(entry);
93+
if (nodeData) {
94+
createNode(nodeData);
95+
}
96+
})
97+
})
98+
)
99+
}

0 commit comments

Comments
 (0)
0