Skip to content

Instantly share code, notes, and snippets.

@kossnocorp
Created October 28, 2020 11:46
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kossnocorp/0e84c939aae8938a7a620a850e87b42b to your computer and use it in GitHub Desktop.
Save kossnocorp/0e84c939aae8938a7a620a850e87b42b to your computer and use it in GitHub Desktop.
My Firebase Functions esbuild setup
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')
),
])
)
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