generated from ipfs-examples/example-fork-go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
178 lines (155 loc) · 4.4 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import path from 'path'
import { fileURLToPath } from 'url'
import CopyWebpackPlugin from 'copy-webpack-plugin'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import NodePolyfillPlugin from 'node-polyfill-webpack-plugin'
import webpack from 'webpack'
import { merge } from 'webpack-merge'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
/**
* HMR/Live Reloading broken after Webpack 5 rc.0 -> rc.1 update
* https://github.com/webpack/webpack-dev-server/issues/2758
*
* target: 'web' for the moment under your development mode.
*/
const paths = {
// Source files
src: path.resolve(__dirname, './src'),
// Production build files
build: path.resolve(__dirname, './dist'),
// Static files that get copied to build folder
public: path.resolve(__dirname, './public')
}
/**
* @type {Partial<import('webpack').Configuration>}
*/
const prod = {
mode: 'production',
devtool: false,
output: {
path: paths.build,
publicPath: '/',
filename: '[name].[contenthash].bundle.js'
},
performance: {
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000
},
// fix: https://github.com/webpack/webpack-dev-server/issues/2758
target: 'browserslist'
}
/**
* @type {Partial<import('webpack').Configuration>}
*/
const dev = {
// Set the mode to development or production
mode: 'development',
// Control how source maps are generated
devtool: 'inline-source-map',
// Where webpack outputs the assets and bundles
output: {
path: paths.build,
filename: '[name].bundle.js',
publicPath: '/'
},
// Spin up a server for quick development
devServer: {
historyApiFallback: true,
static: paths.build,
open: true,
compress: true,
hot: true,
port: 3000
},
plugins: [
// Only update what has changed on hot reload
new webpack.HotModuleReplacementPlugin()
]
}
/**
* @type {Partial<import('webpack').Configuration>}
*/
const common = {
// Where webpack looks to start building the bundle
entry: [paths.src + '/index.js'],
// Customize the webpack build process
plugins: [
// Copies files from target to destination folder
new CopyWebpackPlugin({
patterns: [
{
from: `${paths.public}/assets`,
to: 'assets',
globOptions: {
ignore: ['*.DS_Store']
},
noErrorOnMissing: true
}
]
}),
// fixes Module not found: Error: Can't resolve 'stream' in '.../node_modules/nofilter/lib'
new NodePolyfillPlugin(),
// Note: stream-browserify has assumption about `Buffer` global in its
// dependencies causing runtime errors. This is a workaround to provide
// global `Buffer` until https://github.com/isaacs/core-util-is/issues/29
// is fixed.
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
process: 'process/browser'
}),
// Generates an HTML file from a template
// Generates deprecation warning: https://github.com/jantimon/html-webpack-plugin/issues/1501
new HtmlWebpackPlugin({
title: 'Helia bundle by Webpack',
template: paths.public + '/index.html', // template file
filename: 'index.html', // output file,
minify: false
})
],
// Determine how modules within the project are treated
module: {
rules: [
// JavaScript: Use Babel to transpile JavaScript files
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
targets: {
esmodules: true
}
}
],
'@babel/preset-react'
]
}
}
},
// Images: Copy image files to build folder
{ test: /\.(?:ico|gif|png|jpg|jpeg)$/i, type: 'asset/resource' },
// Fonts and SVGs: Inline files
{ test: /\.(woff(2)?|eot|ttf|otf|svg|)$/, type: 'asset/inline' },
{ test: /\.(css)$/, use: ['style-loader', 'css-loader'] }
]
},
resolve: {
modules: [paths.src, 'node_modules'],
extensions: ['.js', '.jsx', '.json'],
alias: {
'@': paths.src
}
},
// fix: https://github.com/webpack/webpack-dev-server/issues/2758
target: 'web'
}
export default (cmd) => {
const production = cmd.production
const config = production ? prod : dev
return merge(common, config)
}