-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
138 lines (125 loc) · 4.04 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const path = require('path');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const Dotenv = require('dotenv-webpack');
let mode = 'development';
const plugins = [
new Dotenv({
path: './.env.local',
}),
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({ filename: './static/css/main.css' }),
new HtmlWebpackPlugin({
template: './public/index.html',
favicon: './public/favicon.ico',
}),
new WebpackManifestPlugin({
basePath: './public',
fileName: 'manifest.json'
})
];
if (process.env.NODE_ENV === 'production') {
mode = 'production';
}
if (process.env.SERVE) {
plugins.push(new ReactRefreshWebpackPlugin());
}
module.exports = {
// mode defaults to 'production' if not set
mode: mode,
entry: './src/index.js',
output: {
publicPath: "/", //Without publicPath resources might not be loaded properly /article/uuid, only index.html.
filename: './static/js/[name].bundle.js',
// output path is required for `clean-webpack-plugin`
path: path.resolve(__dirname, 'dist'),
// this places all images processed in an image folder
assetModuleFilename: './static/media/[hash][ext][query]',
},
module: {
rules: [
{
test: /\.(png|jpe?g|gif)$/i,
/**
* The `type` setting replaces the need for "url-loader"
* and "file-loader" in Webpack 5.
*
* setting `type` to "asset" will automatically pick between
* outputing images to a file, or inlining them in the bundle as base64
* with a default max inline size of 8kb
*/
type: 'asset', // 'asset/resource' 'asset/inline'
/**
* If you want to inline larger images, you can set
* a custom `maxSize` for inline like so:
*/
// parser: {
// dataUrlCondition: {
// maxSize: 30 * 1024,
// },
// },
},
{
test: /\.svg$/i,
type: 'asset',
resourceQuery: /url/, // *.svg?url https://react-svgr.com/docs/webpack/
},
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
resourceQuery: { not: [/url/] }, // exclude react component if *.svg?url
use: ['@svgr/webpack'],
},
{
test: /\.(s[ac]|c)ss$/i,
use: [
{
loader: MiniCssExtractPlugin.loader,
// This is required for asset imports in CSS, such as url()
options: { publicPath: '../../' },
// publicPath is the relative path of the resource to the context
// e.g. for ./css/admin/main.css the publicPath will be ../../
// while for ./css/main.css the publicPath will be ../
},
'css-loader',
'postcss-loader',
// according to the docs, sass-loader should be at the bottom, which
// loads it first to avoid prefixes in your sourcemaps and other issues.
'sass-loader',
],
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
// without additional settings, this will reference .babelrc
loader: 'babel-loader',
options: {
/**
* From the docs: When set, the given directory will be used
* to cache the results of the loader. Future webpack builds
* will attempt to read from the cache to avoid needing to run
* the potentially expensive Babel recompilation process on each run.
*/
cacheDirectory: true,
},
},
},
],
},
plugins: plugins,
devtool: 'source-map',
resolve: {
extensions: ['.js', '.jsx'],
},
devServer: {
static: {
directory: path.join(__dirname, 'dist'),
},
hot: true,
historyApiFallback: true, // for routes on single page app
},
};