Created
October 28, 2020 11:46
-
-
Save kossnocorp/0e84c939aae8938a7a620a850e87b42b to your computer and use it in GitHub Desktop.
My Firebase Functions esbuild setup
This file contains 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
import { build } from 'estrella' | |
import fs from 'fs' | |
import { promisify } from 'util' | |
import packageJSON from '../package.json' | |
import listFunctions from './_lib/listFunctions' | |
const writeFile = promisify(fs.writeFile) | |
const external = Object.keys(packageJSON.dependencies) | |
const appEnv = process.env.APP_ENV | |
const rootPath = process.cwd() | |
listFunctions().then((fns) => | |
Promise.all([ | |
build({ | |
cwd: rootPath, | |
entry: `app/server/functions/_lib/init/index.ts`, | |
outfile: `build/${appEnv}/functions/init.js`, | |
bundle: true, | |
platform: 'node', | |
target: 'node9', | |
sourcemap: true, | |
clear: false, | |
tslint: false, | |
external, | |
}), | |
Promise.all( | |
fns.map((fn) => | |
build({ | |
cwd: rootPath, | |
entry: `app/server/functions/${fn}/index.ts`, | |
outfile: `build/${appEnv}/functions/${fn}.js`, | |
bundle: true, | |
platform: 'node', | |
target: 'node10', | |
sourcemap: true, | |
clear: false, | |
tslint: false, | |
external, | |
}) | |
) | |
), | |
writeFile( | |
`build/${appEnv}/functions/index.js`, | |
[ | |
"require('./init')", | |
'function shouldRequire(fn) { return process.env.FUNCTION_TARGET === undefined || process.env.FUNCTION_TARGET === fn }', | |
] | |
.concat( | |
fns.map( | |
(fn) => | |
`if (shouldRequire('${fn}')) module.exports.${fn} = require('./${fn}').${fn}` | |
) | |
) | |
.join('\n\n') | |
), | |
]) | |
) |
This file contains 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
import fs from 'fs' | |
import { promisify } from 'util' | |
const readDir = promisify(fs.readdir) | |
const ignore = ['_lib', 'index.ts'] | |
const pickedFns = process.env.FUNCTION | |
export default async function listFunctions() { | |
if (pickedFns) return pickedFns.split(',') | |
const fns = await readDir('app/server/functions') | |
return fns.filter((fn) => !ignore.includes(fn)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment