-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
executable file
·89 lines (80 loc) · 2.36 KB
/
webpack.config.js
File metadata and controls
executable file
·89 lines (80 loc) · 2.36 KB
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
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { DefinePlugin } = require('webpack')
const dotenv = require('dotenv')
const fs = require('fs')
const path = require('path')
module.exports = (env) => {
// Get the root path (assuming your webpack config is in the root of your project!)
const currentPath = path.join(__dirname)
// Create the fallback path (the production .env)
const basePath = currentPath + '/.env'
// We're concatenating the environment name to our filename to specify the correct env file!
const envPath = basePath + '.' + env.NODE_ENV
console.log(envPath)
// Check if the file exists, otherwise fall back to the production .env
const finalPath = fs.existsSync(envPath) ? envPath : basePath
console.log(finalPath)
// Set the path parameter in the dotenv config
const fileEnv = dotenv.config({ path: finalPath }).parsed
// reduce it to a nice object, the same as before (but with the variables from the file)
const envKeys = Object.keys(fileEnv).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(fileEnv[next])
return prev
}, {})
return {
output: {
filename: 'js/app.bundle.js'
},
plugins: [
new HtmlWebpackPlugin({
title: 'your title',
template: 'public/index.html'
}),
new MiniCssExtractPlugin({
filename: 'css/[name].[hash].css',
chunkFilename: 'css/[id].[hash].css'
}),
new DefinePlugin(envKeys)
// new Dotenv()
],
devServer: {
historyApiFallback: true
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
},
{
test: /\.(s*)css$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
'css-loader',
'sass-loader'
]
},
{
test: /\.jpg|png|gif|woff|eot|ttf|svg|mp4|webm$/,
use: {
loader: 'url-loader',
options: {
limit: 10000,
name: '[hash].[ext]',
outputPath: 'assets'
}
}
}
]
}
}
}