Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
executable file
·111 lines (105 loc) · 3.44 KB
/
Copy pathwebpack.config.js
File metadata and controls
executable file
·111 lines (105 loc) · 3.44 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const path = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
const fse = require("fs-extra")
const isProduction = process.env.NODE_ENV == 'production'
// Access the fields to configure webpack
const pkgVars = require('./package.json')
// Destructure variables from pkgVars.config
const {entry, sourceDir, buildDir, port} = pkgVars.config
const browserCacheHandler = isProduction ? 'bundle.[contenthash].js' : 'bundle.js'
const outputBundleHandler = isProduction ? buildDir : sourceDir
const stylesHandler = isProduction ? MiniCssExtractPlugin.loader : 'style-loader'
class RunAfterCompile{
apply(compiler) {
compiler.hooks.done.tap('Copy images', () => {
fse.copySync(`./${sourceDir}/assets/images`, `./${buildDir}/assets/images`)
})
}
}
// SHARED CONFIG
const config = {
entry: `./${sourceDir}/index.js`,
output: {
filename: browserCacheHandler,
path: path.resolve(__dirname, outputBundleHandler),
},
module: {
rules: [
{
test: /\.(js|jsx)$/i,
exclude: /node_modules/,
use: { loader: 'babel-loader',
options: {
presets: [['@babel/preset-react', {'runtime': 'automatic'} ], '@babel/preset-env']
}
},
},
{
test: /\.css$/i,
use: [ stylesHandler, 'css-loader' ]
},
{
test: /\.s[ac]ss$/i,
use: [ stylesHandler, 'css-loader', 'sass-loader']
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
},
{
test: /\.hbs$/,
use: [
'handlebars-loader'
]
}
],
},
plugins: [
// new CleanWebpackPlugin({
// cleanOnceBeforeBuildPatterns: [
// "**/*", // Default Option
// // path.join(process.cwd(), 'build/**/*')
// ]
// }),
new HtmlWebpackPlugin({
title: 'React Essentials',
template: `./${sourceDir}/index.hbs`,
description: 'Showcases the fundamental concepts in React'
})
],
optimization: {
minimizer: [
// For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
// `...`,
],
}
}
module.exports = () => {
// PRODUCTION CONFIG
if (isProduction) {
config.mode = 'production';
config.plugins.push(
new MiniCssExtractPlugin({ filename: 'styles.[contenthash].css' }),
new CleanWebpackPlugin(),
new RunAfterCompile()
);
config.optimization.minimizer.push(new CssMinimizerPlugin())
// DEVELOPMENT CONFIG
} else {
config.mode = 'development';
config.devServer = {
static: `./${buildDir}/` ,
// watchFiles: [`./${sourceDir}/index.hbs`],
hot: true,
port,
open: true,
compress: true,
historyApiFallback: true,
host: '0.0.0.0'
}
}
return config
}