-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
144 lines (135 loc) · 5.27 KB
/
Copy pathwebpack.config.js
File metadata and controls
144 lines (135 loc) · 5.27 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
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
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const path = require("path");
const localIdentName = "[path][local]";
module.exports = {
// The main entry point source/index.tsx
entry: path.resolve(__dirname, "source"),
// Generated bundle location
output: {
path: path.resolve(__dirname, "client"),
filename: "[name].js",
chunkFilename: "[name].js",
publicPath: "/client/"
},
// Source files take into account
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx", ".css"]
},
optimization: {
// Customize bundles
splitChunks: {
cacheGroups: {
// vendor.js
vendors: {
test: /[\\/]node_modules[\\/]/,
name: "vendor",
chunks: "all"
},
// common.js
common: {
// Bundle name
name: "common",
// Find common module inside dynamic import bundle
chunks: "async",
// Add module if it used at least twice in different dynamic import bundle
minChunks: 2,
// Force Webpack to apply this rules even if the rule is not in optimal regarding webpack default behavior
enforce: true
}
// Add this option to build an unique css bundle
// styles.css && styles.js (class names mapping for js runtime)
// styles: {
// name: "styles",
// test: /\.css$/,
// chunks: "all",
// enforce: true
// }
}
},
// Add Css minimizer used on prod build
// Add JS minimizer because optimization minimizer is manually defined
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true
}),
new OptimizeCSSAssetsPlugin({})
]
},
// Configure css extraction from JS bundle
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css"
})
],
module: {
rules: [
// postcss-cssnext transform new css syntax into css understable today by the browser.
// postcss-import transform @import rules by inlining content, useful for shared variables.
// css-loader import css files and generate scoped class names regarding localIdentName option.
// mini-css-extract-plugin extract transformed css into dedicated css bundles.
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
modules: true,
importLoaders: 1,
localIdentName: localIdentName
}
},
{
loader: "postcss-loader"
}
]
},
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
// 2. babel-preset-react transform React jsx and babel-preset-env es2015 syntax into code understandable by the browser.
// babel-plugin-react-css-modules transform styleName attribute into className React attribute.
// syntax-dynamic-import allow babel to parse dynamic import syntax but not transform it.
// react-loadable/babel declare wich modules are being loaded.
{
loader: "babel-loader",
options: {
babelrc: false,
presets: [
"react",
[
"env",
{
modules: false
}
]
],
plugins: [
[
"react-css-modules",
{
generateScopedName: localIdentName
}
],
"syntax-dynamic-import",
"react-loadable/babel"
]
}
},
// 1. TypeScript type check and emit JavaScript es2015 (TypeScript without types) consumable by Babel.
{
loader: "ts-loader",
options: {
configFile: require.resolve("./tsconfig.json"),
context: __dirname
}
}
]
}
]
}
};