-
-
Save eugenehp/01c629682a394dc4a107dbc7113056c9 to your computer and use it in GitHub Desktop.
gatsby in aws lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require("aws-sdk"); | |
const {link} = require("linkfs"); | |
const mock = require('mock-require'); | |
const fs = require('fs'); | |
const tmpDir = require('os').tmpdir(); | |
exports.handler = (event, context) => { | |
rewriteFs(); | |
invokeGatsby(context); | |
} | |
const invokeGatsby = (context) => { | |
const gatsby = require("gatsby/dist/commands/build"); | |
gatsby({ | |
directory: __dirname, | |
}) | |
.then(uploadFilesToS3) | |
.then(invalidateCloudFront) | |
.then(context.succeed) | |
.catch(context.fail); | |
}; | |
//TODO replace with in-memory-fs ?! | |
const rewriteFs = () => { | |
// redirect paths | |
const linkedFs = link(fs, [ | |
[`${__dirname}/.cache`, tmpDir+"/.cache"], | |
[`${__dirname}/public`, tmpDir+"/public"] | |
]); | |
// those are missing in linkfs | |
linkedFs.ReadStream = fs.ReadStream; | |
linkedFs.WriteStream = fs.WriteStream; | |
// replace fs with linkfs globally | |
mock('fs', linkedFs); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment