Adds SourceCode nodes to gatsby. You can specify which MIME Types to process in the transformer options.
npm install --save gatsby-transformer-source-code
Note: You also need to have gatsby-source-filesystem
installed and configured so it points to your files.
In your gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-transformer-source-code`,
options: {
mimeTypes: ['application/javascript', 'text/plain']
}
},
{
resolve: `gatsby-source-filesystem`,
options: {
path: `./src/data/`
}
}
]
};
Where the source folder ./src/data/
contains .js/.txt files.
NOTE: If you do not provide a mimeTypes
array in options, all files will be processed.
You can query the nodes using GraphQL, like from the GraphiQL browser: http://localhost:8000/___graphql
.
{
allSourceCode {
nodes {
content
}
}
}
Returns:
{
"data": {
"allSourceCode": {
"nodes": [
{
"content": "content of file"
},
{
"content": "content of second file"
}
]
}
}
}
{
file(relativePath: { eq: "LICENSE" }) {
childSourceCode {
... on SourceCode {
content
}
}
}
}
Returns:
{
"data": {
"file": {
"childSourceCode": {
"content": "MIT License"
}
}
}
}