Skip to content

Instantly share code, notes, and snippets.

@mizchi
Last active February 23, 2021 07:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mizchi/1929d54ff93e32217ec985094b473fd5 to your computer and use it in GitHub Desktop.
Save mizchi/1929d54ff93e32217ec985094b473fd5 to your computer and use it in GitHub Desktop.
Cloud Function built by webpack
{
"functions": {
"source": "functions"
},
"hosting": {
"public": "build",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "/api/hello",
"function": "hello"
}
]
}
}
// server/index.js
import * as functions from 'firebase-functions'
export const hello = functions.https.onRequest((req, res) => {
res.send('hello').end()
})
{
"private": true,
"scripts": {
"deploy:functions": "webpack --config server/webpack.config.js && cd functions && yarn install && firebase deploy --only functions"
},
"devDependencies": {
"babel-loader": "^7.1.2",
"generate-json-webpack-plugin": "^0.2.2",
"webpack": "^3.8.1"
},
"dependencies": {
"firebase-admin": "^5.5.1",
"firebase-functions": "^0.7.3"
}
}
// server/webpack.config.js
const pkg = require('../package')
const GenerateJsonPlugin = require('generate-json-webpack-plugin')
const externals = ['firebase-admin', 'firebase-functions']
const genPackage = () => ({
name: 'functions',
private: true,
main: 'index.js',
license: 'MIT',
dependencies: externals.reduce(
(acc, name) =>
Object.assign({}, acc, {
[name]: pkg.dependencies[name] || pkg.devDependencies[name]
}),
{}
)
})
module.exports = {
entry: [__dirname + '/index.js'],
output: {
path: __dirname + '/../functions',
filename: 'index.js',
libraryTarget: 'commonjs'
},
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',
include: __dirname + '/src'
}
]
},
externals: externals.reduce(
(acc, name) => Object.assign({}, acc, { [name]: true }),
{}
),
plugins: [new GenerateJsonPlugin('package.json', genPackage())]
}
@arthabus
Copy link

Hi there. I'm trying to do the same thing but as it appeared firebase functions can only work if they are exported at the top level of the code, while webpack moves everything inside a function which prevents firebase to see any triggers/api functions. How did you overcome that? Here is a related SO questions. Any suggestions on that?

@jasan-s
Copy link

jasan-s commented Feb 23, 2021

i face the following error: [webpack-cli] TypeError: Cannot read property 'PROCESS_ASSETS_STAGE_ADDITIONAL' of undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment