-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.js
46 lines (42 loc) · 1.55 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
const webpack = require('webpack');
const dotenv = require('dotenv');
const fs = require('fs'); // to check if the file exists
const path = require('path'); // to get the current path
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = env => {
const currentPath = path.join(__dirname);
const basePath = currentPath + '/.env';
const envPath = basePath + '.' + env.ENVIRONMENT;
const finalPath = fs.existsSync(envPath) ? envPath : basePath;
const fileEnv = dotenv.config({ path: finalPath }).parsed;
const envKeys = Object.keys(fileEnv).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(fileEnv[next]);
return prev;
}, {});
return {
entry : './src/index.js',
output : {
path : path.resolve(__dirname , 'dist'),
filename: 'index_bundle.js',
},
devServer: {
historyApiFallback: true
},
module : {
rules : [
{test : /\.(js)$/, use:'babel-loader'},
{test : /\.scss|css$/, use:['style-loader', 'css-loader', 'sass-loader']},
{test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/, use: 'file-loader' }
]
},
mode:env.ENVIRONMENT,
plugins : [
new webpack.DefinePlugin(envKeys),
new CleanWebpackPlugin(),
new HtmlWebpackPlugin ({
template : './src/index.html'
})
]
};
};