-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
137 lines (131 loc) · 4.54 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
const path = require('path');
const webpack = require('webpack');
const dotenv = require('dotenv');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
const { homepage } = require('./package.json');
const appManifest = require('./public/manifest.json');
// read .env file
const envLocal = dotenv.config({ path: './.env' }).parsed || {};
// collect all .env keys and values
const envKeys = Object.keys(envLocal).reduce((prev, next) => {
prev[`process.env.${next.trim()}`] = JSON.stringify(envLocal[next].trim());
return prev;
}, {});
module.exports = (env, argv) => {
const isEnvProduction = argv.mode === 'production';
const rootPath = homepage && isEnvProduction ? homepage : '/';
const plugins = [
new HtmlWebpackPlugin({
template: 'public/index.html',
favicon: 'public/img/logo.svg',
isEnvProduction,
rootPath,
description: appManifest.name,
title: appManifest.short_name,
language: 'sk',
GTM_ID: 'GTM-5Z6SFDZ',
}),
new WebpackManifestPlugin({
fileName: 'asset-manifest.json',
publicPath: homepage,
generate: (seed, files, entrypoints) => {
const manifestFiles = files.reduce((manifest, file) => {
const m = manifest;
m[file.name] = file.path;
return m;
}, seed);
const entrypointFiles = entrypoints.main.filter(
(fileName) => !fileName.endsWith('.map')
);
return {
files: manifestFiles,
entrypoints: entrypointFiles,
};
},
}),
new webpack.DefinePlugin(envKeys),
];
if (isEnvProduction) {
plugins.push(
new MiniCssExtractPlugin({
filename: 'static/css/[name].[contenthash:8].css',
chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
})
);
}
return {
devtool: 'source-map',
devServer: {
historyApiFallback: true,
hot: true,
open: [homepage],
port: 3000,
},
entry: './src/index.jsx',
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react'],
},
},
},
{
test: /\.(sa|sc|c)ss$/,
use: [
// Creates `style` nodes from JS strings
isEnvProduction
? MiniCssExtractPlugin.loader
: 'style-loader',
// Translates CSS into CommonJS
'css-loader',
// Compiles Sass to CSS
'sass-loader',
],
},
{
test: /\.svg$/i,
type: 'asset',
resourceQuery: /url/, // *.svg?url
},
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
resourceQuery: { not: [/url/] }, // exclude react component if *.svg?url
use: ['@svgr/webpack'],
},
{
test: /\.(jpe?g|gif|png)$/i,
type: 'asset',
},
{
test: /\.csv$/i,
type: 'asset/resource',
},
],
},
output: {
clean: true,
path: path.join(__dirname, '/build'),
pathinfo: !isEnvProduction,
publicPath: rootPath,
filename: isEnvProduction
? 'static/js/[name].[contenthash:8].js'
: 'static/js/bundle.js',
chunkFilename: isEnvProduction
? 'static/js/[name].[contenthash:8].chunk.js'
: 'static/js/[name].chunk.js',
assetModuleFilename: 'static/assets/[name].[hash][ext]',
},
plugins,
resolve: {
extensions: ['.js', '.jsx'],
},
};
};