-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.common.js
106 lines (96 loc) · 2.63 KB
/
webpack.common.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
94
95
96
97
98
99
100
101
102
103
104
105
106
const HtmlWebPackPlugin = require('html-webpack-plugin');
const path = require('path');
const dotenv = require('dotenv');
const fs = require('fs'); // to check if the file exists
const webpack = require('webpack');
module.exports = (env) => {
// Get the root path (assuming your webpack config is in the root of your project!)
const envFolderPath = `${path.join(__dirname)}/env`;
// Create the fallback path (the production .env)
const prodEnv = envFolderPath + '/.env';
// We're concatenating the environment name to our filename to specify the correct env file!
let envKeys = {};
if(env && env.ENVIRONMENT)
{
const envPath = `${envFolderPath}/${env.ENVIRONMENT}.env`;
if(fs.existsSync(envPath))
{
const finalPath = fs.existsSync(envPath) ? envPath : prodEnv;
const fileEnv = dotenv.config({ path: finalPath }).parsed;
envKeys = Object.keys(fileEnv).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(fileEnv[next]);
return prev;
}, {});
}
}
const commonPath = `${envFolderPath}/.env`;
const fileCommon = dotenv.config({ path: commonPath}).parsed;
const commonKeys = Object.keys(fileCommon).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(fileCommon[next]);
return prev;
}, {});
const allKeys = {...commonKeys, ...envKeys };
console.log(allKeys);
return {
module: {
rules: [
{
test: /\.css/,
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.scss$/,
use: [
'style-loader',
'css-loader',
'sass-loader'
]
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
resolve: {
extensions: ['.js', '.jsx']
},
use: {
loader: 'babel-loader'
}
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader'
}
]
},
{
// Load all images as base64 encoding if they are smaller than 8192 bytes
test: /\.(png|jpg|gif|svg)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'images'
}
}
]
},
]
},
resolve: {
extensions: ['.js', '.jsx']
},
plugins: [
new webpack.DefinePlugin(allKeys),
new HtmlWebPackPlugin({
template: './src/index.html',
filename: './index.html'
}),
]
};
};