I am using webpack to build my cloud functions (several reasons to this as path aliases, typescript and some other configs)
I have my index.ts file as well as src folder containing individual files for each function.
My index.ts file then exports them like this
import admin from 'firebase-admin';
admin.initializeApp();
export const func1 = require('./src/func1').default;
export const func2 = require('./src/func2').default;
The issue with webpack compared to something like tsc is that it requires all the code from required files and bundles it in index file, which for cloud functions is not ideal due to cold start performance.
What would be correct way to split these files into separate ones and ensure good cold start performance (i.e. only code for function that is being called is required).
My webpack config for firebase looks like this
const config = {
entry: {
index: path.resolve(__dirname, './index.ts')
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, './dist'),
libraryTarget: 'commonjs'
},
mode: 'production',
optimization: {
nodeEnv: 'production',
splitChunks: {
chunks: 'all'
},
minimize: true,
minimizer: [
new ESBuildMinifyPlugin({
target: 'es2020',
minifyWhitespace: true,
minifyIdentifiers: true,
minifySyntax: true
})
]
},
resolve: {
alias: {
$tools: path.resolve(__dirname, '../../tools'),
$types: path.resolve(__dirname, '../../types')
},
extensions: ['.tsx', '.ts', '.js', '.mjs', '.json']
},
externals: ['firebase-functions', 'firebase-admin'],
target: 'node',
module: {
rules: [
{
test: /\.(m?js|ts)$/u,
exclude: /(node_modules)$/u,
use: {
loader: 'esbuild-loader',
options: {
loader: 'ts',
target: 'es2020',
tsconfigRaw: require('./tsconfig.json')
}
}
}
]
},
plugins: [new CleanWebpackPlugin(), new ESBuildPlugin()]
};