This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
93 lines (82 loc) · 2.96 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const path = require('path');
const webpack = require('webpack');
const Dotenv = require('dotenv-webpack');
const nodeExternals = require('webpack-node-externals');
const GenerateJsonPlugin = require('generate-json-webpack-plugin');
const pkg = require('./package.json');
module.exports = (env, argv) => {
const envPlugin = createEnvVariablesPlugin(env);
return {
target: 'node',
mode: env.dev ? 'development' : 'production',
entry: './src/main/services.js',
externals: [nodeExternals()],
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist', 'functions'),
libraryTarget: 'commonjs'
},
node: {
__dirname: false,
__filename: false
},
optimization: {
minimize: false
},
plugins: [
envPlugin,
new GenerateJsonPlugin('package.json', genFirebaseFunctionsPackage())
]
};
};
function genFirebaseFunctionsPackage () {
return {
name: 'functions',
private: true,
main: 'index.js',
license: 'MIT',
engines: {
node: '14'
},
dependencies: pkg.dependencies
};
};
function createEnvVariablesPlugin (envArgs) {
const envCase = establishEnvironment(envArgs);
switch (envCase) {
case 'WEBPACK_DEV': return createPluginToLoadFromEnvDevFile();
case 'WEBPACK_PROD_LOCAL': return createPluginReducingFromEnvDevFile();
case 'WEBPACK_PROD_CI': return createPluginFromEnvInMemory();
default: return console.info('no environment variable case was determined');
}
};
function establishEnvironment (envArgs) {
if (envArgs.WEBPACK_BUNDLE && envArgs.dev) return 'WEBPACK_DEV';
if (envArgs.WEBPACK_BUNDLE && envArgs.prodLocal) return 'WEBPACK_PROD_LOCAL';
if (envArgs.WEBPACK_BUNDLE && envArgs.prodCi) return 'WEBPACK_PROD_CI';
return 'UNDEFINED';
}
const envDevFile = path.resolve(process.cwd(), '.env');
function createPluginToLoadFromEnvDevFile () {
console.info(`attempting to inject env vars from "${envDevFile}" file using webpack plugin`);
const dotEnvPlugin = new Dotenv({ path: envDevFile });
return dotEnvPlugin;
}
function createPluginReducingFromEnvDevFile () {
console.info(`attempting to inject env vars reducing from "${envDevFile}" file using webpack plugin`);
const dotEnvPlugin = new Dotenv({ path: envDevFile });
// the removal of emulator env variable is relevant to the productions app
// since firebase node sdk automatically attempts connection to emulator host
// if variable is set
delete dotEnvPlugin.definitions['process.env.FIRESTORE_EMULATOR_HOST'];
return dotEnvPlugin;
}
function createPluginFromEnvInMemory () {
console.info('attempting to inject env vars from memory using webpack plugin');
const dotEnvPlugin = new webpack.DefinePlugin({
'process.env.SCALE_SERP_KEY': JSON.stringify(process.env.SCALE_SERP_KEY),
'process.env.SEND_GRID_KEY': JSON.stringify(process.env.SEND_GRID_KEY),
'process.env.DEFAULT_FROM_EMAIL': JSON.stringify(process.env.DEFAULT_FROM_EMAIL)
});
return dotEnvPlugin;
}