-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
157 lines (157 loc) · 5.34 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
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { VanillaExtractPlugin } = require('@vanilla-extract/webpack-plugin');
const isDevelopment = process.env.NODE_ENV === 'development';
const isProduction = process.env.NODE_ENV === 'production';
module.exports = {
entry: './src/index.tsx',
devtool: 'inline-source-map',
devServer: {
contentBase: path.join(__dirname, 'public'),
hot: true, // Enable webpack's Hot Module Replacement feature (https://webpack.js.org/configuration/dev-server/#devserverhot)
compress: true, // Enable gzip compression for everything served (https://webpack.js.org/configuration/dev-server/#devservercompress)
open: true, // Tells dev-server to open the default browser after server had been started.(https://webpack.js.org/configuration/dev-server/#devserveropen)
port: 8001,
},
module: {
rules: [
{
test: /\.ts(x)?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
exclude: /\.(module|vanilla)\.css$/,
use: [
isProduction ? MiniCssExtractPlugin.loader : 'style-loader',
{
loader: 'css-loader',
options: {
sourceMap: isDevelopment,
},
},
],
},
{
test: /\.module\.css$/,
use: [
isProduction ? MiniCssExtractPlugin.loader : 'style-loader',
{
loader: 'css-loader',
options: {
modules: {
auto: true,
localIdentName: '[name]__[local]___[hash:base64:5]',
exportLocalsConvention: 'camelCase',
},
},
},
],
},
{
test: /\.s[ac]ss$/i,
exclude: /\.module\.(scss|sass)$/,
use: [
isProduction ? MiniCssExtractPlugin.loader : 'style-loader',
{
loader: 'css-loader',
options: {
sourceMap: isDevelopment,
},
},
{
loader: 'sass-loader',
options: {
sourceMap: isDevelopment,
},
},
],
},
{
test: /\.module\.s(a|c)ss$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
sourceMap: isDevelopment,
modules: {
auto: true,
localIdentName: '[name]__[local]___[hash:base64:5]',
exportLocalsConvention: 'camelCase',
},
},
},
{
loader: 'sass-loader',
options: {
sourceMap: isDevelopment,
},
},
],
},
{
test: /\.vanilla\.css$/i, // Targets only CSS files generated by vanilla-extract
use: [
MiniCssExtractPlugin.loader,
{
loader: require.resolve('css-loader'),
options: {
url: false, // Required as image imports should be handled via JS/TS import statements
},
},
],
},
],
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: 'Peggy - bootstrap your frontend project',
template: 'public/index.html',
hash: false,
}),
new MiniCssExtractPlugin(),
new ForkTsCheckerWebpackPlugin(),
new VanillaExtractPlugin(),
],
resolve: {
extensions: ['.tsx', '.ts', '.js', 'jsx', '.css', '.scss'],
modules: [path.resolve(__dirname, 'src'), 'node_modules'], // this is similar to tsconfig: "baseUrl": "src"(https://www.typescriptlang.org/v2/en/tsconfig#baseUrl)
// so I don't need to write something like: import greeter from './greater', in stead, I can write: import greeter from 'greeter';
// I don't need to write "../" or "./" when I import modules inside my project 'src' folder
// for more details, refer https://stackoverflow.com/questions/27502608/resolving-require-paths-with-webpack/36574982#36574982
// and https://webpack.js.org/configuration/resolve/#resolvemodules
},
output: {
filename: '[name].[fullhash].js',
path: path.resolve(__dirname, 'dist'),
},
optimization: {
splitChunks: {
/**
* extract third-party libraries, such as lodash or react, to a separate vendor chunk
* as they are less likely to change than our local source code.
* This step will allow clients to request even less from the server to stay up to date
* further reading:
* https://webpack.js.org/guides/caching/
* https://medium.com/@hpux/webpack-4-in-production-how-make-your-life-easier-4d03e2e5b081
*
* below settings comes from https://webpack.js.org/plugins/split-chunks-plugin/#split-chunks-example-2
* warning: This might result in a large chunk containing all external packages.
* It is recommended to only include your core frameworks and utilities and dynamically load the rest of the dependencies.
*/
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
},
},
};