-
Notifications
You must be signed in to change notification settings - Fork 20
/
webpack.common.js
168 lines (165 loc) · 5.91 KB
/
webpack.common.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
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const BG_IMAGES_DIRNAME = 'bgimages';
module.exports = (env) => {
return {
context: __dirname,
entry: {
app: {
import: path.resolve(__dirname, 'src', 'index.tsx'),
dependOn: process.env.PREVIEW? 'mirage': undefined
},
...(process.env.PREVIEW? {mirage: path.resolve(__dirname, 'src', 'mirage', 'index.ts')}: {})
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'src', 'index.html'),
favicon: './src/app/assets/favicon.ico',
})
],
// https://medium.com/hackernoon/the-100-correct-way-to-split-your-chunks-with-webpack-f8a9df5b7758
optimization: {
runtimeChunk: 'single', // create a runtime file to be shared for all generated chunks
splitChunks: {
chunks: 'all', // https://webpack.js.org/plugins/split-chunks-plugin/#splitchunkschunks
maxInitialRequests: Infinity,
minSize: 0,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name(module) {
// get the name. E.g. node_modules/packageName/not/this/part.js
// or node_modules/packageName
const matches = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/);
if (!Array.isArray(matches)) {
return null;
}
if (matches.length < 2) {
return null;
}
const packageName = matches[1];
// npm package names are URL-safe, but some servers don't like @ symbols
return `npm.${packageName.replace('@', '')}`;
},
},
},
},
},
module: {
rules: [
{
test: /\.(tsx|ts|jsx)?$/,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: true,
experimentalWatchApi: true,
}
}
]
},
{
test: /\.(svg|ttf|eot|woff|woff2)$/,
// only process modules with this loader
// if they live under a 'fonts' or 'pficon' directory
include: [
path.resolve(__dirname, 'node_modules/patternfly/dist/fonts'),
path.resolve(__dirname, 'node_modules/@patternfly/react-core/dist/styles/assets/fonts'),
path.resolve(__dirname, 'node_modules/@patternfly/react-core/dist/styles/assets/pficon'),
path.resolve(__dirname, 'node_modules/@patternfly/patternfly/assets/fonts'),
path.resolve(__dirname, 'node_modules/@patternfly/patternfly/assets/pficon')
],
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 5000 // Limit at 50kb. larger files emited into separate files
}
},
generator: {
filename: 'fonts/[name][ext]'
}
},
{
test: /\.svg$/,
include: input => input.indexOf('background-filter.svg') > 1,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 5000
}
},
generator: {
filename: 'svgs/[name][ext]'
}
},
{
test: /\.svg$/,
// only process SVG modules with this loader if they live under a 'bgimages' directory
// this is primarily useful when applying a CSS background using an SVG
include: input => input.indexOf(BG_IMAGES_DIRNAME) > -1,
use: {
loader: 'svg-url-loader',
options: {}
}
},
{
test: /\.svg$/,
// only process SVG modules with this loader when they don't live under a 'bgimages',
// 'fonts', or 'pficon' directory, those are handled with other loaders
include: input => (
(input.indexOf(BG_IMAGES_DIRNAME) === -1) &&
(input.indexOf('fonts') === -1) &&
(input.indexOf('background-filter') === -1) &&
(input.indexOf('pficon') === -1)
),
exclude: [
path.resolve(__dirname, 'src/app/assets')
],
type: 'asset/source',
},
{
test: /\.(jpg|jpeg|png|gif|svg)$/i,
include: [
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'node_modules/patternfly'),
path.resolve(__dirname, 'node_modules/@patternfly/patternfly/assets/images'),
path.resolve(__dirname, 'node_modules/@patternfly/react-styles/css/assets/images'),
path.resolve(__dirname, 'node_modules/@patternfly/react-core/dist/styles/assets/images'),
path.resolve(__dirname, 'node_modules/@patternfly/react-core/node_modules/@patternfly/react-styles/css/assets/images'),
path.resolve(__dirname, 'node_modules/@patternfly/react-table/node_modules/@patternfly/react-styles/css/assets/images'),
path.resolve(__dirname, 'node_modules/@patternfly/react-inline-edit-extension/node_modules/@patternfly/react-styles/css/assets/images')
],
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 5000
}
},
generator: {
filename: 'images/[name][ext]'
},
}
]
},
output: {
filename: '[name].[contenthash].bundle.js',
chunkFilename: '[id].[contenthash].bundle.js', // lazy-load modules
hashFunction: "xxhash64",
publicPath: 'auto',
clean: true
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx'],
plugins: [
new TsconfigPathsPlugin({
configFile: path.resolve(__dirname, './tsconfig.json')
})
],
symlinks: false,
cacheWithContext: false
},
};
}