出于学习目的,我编写了一个反转节点内容的 Gatsby 转换器。我将它与 gatsby-source-filesystem
一起使用,仅在 .test
文件上运行:
const crypto = require(`crypto`)
module.exports = function onCreateNode({ node, loadNodeContent, boundActionCreators }) {
const { createNode, createParentChildLink } = boundActionCreators;
if (node.extension !== 'test') {
return;
}
const nodeContent = loadNodeContent(node);
const nodeContentRev = nodeContent.split('').reverse().join('');
const contentDigest = crypto
.createHash(`md5`)
.update(JSON.stringify(nodeContent))
.digest(`hex`);
const RevNode = {
'contentRev': nodeContentRev,
id: '9087657865-just-something-here-for-now',
parent: null,
children: [],
internal: {
type: 'Rev',
contentDigest,
content,
},
};
createNode(RevNode);
}
不幸的是,我对 Gatsby 还是很陌生,所以我正在努力弄清楚这是否有效。看看官方的转换器,他们不仅实现了gatsby-node.js
(我有),而且还实现了extend-node-type.js
,但我找不到关于此的任何文档。
如何查询转换器的结果?
最佳答案
假设您首先验证以下内容:
- 您的代码被加载并运行而没有引发异常。如果不是,请先使用 console.log 消息或调试器验证这一点。
- 您的源插件已正确配置并且正在其配置目录中获取文件。
假设两者均已验证并且转换器插件已加载并正常运行,您将在 gatsby 的数据层 中创建新节点。 可以使用 graphql 探索数据层。
运行 gatsby develop
后在 http://localhost:8000/___graphql 本地访问(参见 gatsby tutorial )
关于 Gatsby : How to query result of transformer plugin?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46370498/