-
Notifications
You must be signed in to change notification settings - Fork 88
/
webpack.browser.config.js
135 lines (133 loc) · 4.1 KB
/
webpack.browser.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
const path = require("path");
const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { StatsWriterPlugin } = require("webpack-stats-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
const { HotModuleReplacementPlugin } = require("webpack");
const isOffline = !!process.env.IS_OFFLINE;
module.exports = {
entry: {
main: path.join(__dirname, "src/browser/index.tsx"),
},
target: "web",
mode: isOffline ? "development" : "production",
node: {
__dirname: true,
__filename: true,
},
devServer: {
hot: true,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
"Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization",
},
watchFiles: {
paths: ["**/*"],
options: {
ignored: ["**/node_modules", "**/dist", "**/.webpack", "**/.serverless"],
},
},
devMiddleware: {
writeToDisk: (filePath) => {
// Always write the stats.json to disk, so we can load it in code
return /\bstats\.json$/.test(filePath);
},
},
},
performance: {
// Turn off size warnings for entry points
hints: false,
},
optimization: {
runtimeChunk: "single",
splitChunks: {
cacheGroups: {
// TODO: Customize code splitting to your needs
vendor: {
name: "vendor",
test: /[\\/]node_modules[\\/]/,
chunks: "all",
},
components: {
name: "components",
test: /[\\/]src[\\/]components[\\/]/,
chunks: "all",
minSize: 0,
},
},
},
},
// React recommends `cheap-module-source-map` for development
devtool: isOffline ? "cheap-module-source-map" : "nosources-source-map",
plugins: [
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{
// Copy content from `./public/` folder to our output directory
context: "./public/",
from: "**/*",
},
],
}),
new MiniCssExtractPlugin({
filename: isOffline ? "[name].css" : "[name].[contenthash:8].css",
}),
new StatsWriterPlugin({
filename: "stats.json",
transform(data, _opts) {
const assets = data.assetsByChunkName;
const stats = JSON.stringify(
{
scripts: Object.entries(assets).flatMap(([_asset, files]) => {
return files.filter((filename) => filename.endsWith(".js") && !/\.hot-update\./.test(filename));
}),
styles: Object.entries(assets).flatMap(([_asset, files]) => {
return files.filter((filename) => filename.endsWith(".css") && !/\.hot-update\./.test(filename));
}),
},
null,
2,
);
return stats;
},
}),
isOffline && new HotModuleReplacementPlugin(),
isOffline && new ReactRefreshWebpackPlugin(),
].filter(Boolean),
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/, // we shouldn't need processing `node_modules`
use: "babel-loader",
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test: /\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$/,
use: [
{
loader: "url-loader",
options: { limit: 8192 },
},
],
},
],
},
resolve: {
// TsconfigPathsPlugin applies the path aliases defined in `.tsconfig.json`
plugins: [new TsconfigPathsPlugin()],
extensions: [".browser.tsx", ".browser.ts", ".browser.jsx", ".browser.js", ".tsx", ".ts", ".jsx", ".js"],
},
output: {
path: path.join(__dirname, "dist"),
filename: isOffline ? "[name].js" : "[name].[contenthash:8].js",
crossOriginLoading: "anonymous", // enable cross-origin loading of chunks
},
};