-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
147 lines (139 loc) · 4.31 KB
/
webpack.config.ts
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
/* eslint-disable @typescript-eslint/ban-ts-comment */
import * as express from "express";
import webpack from "webpack";
import * as path from "path";
import HtmlWebpackPlugin from "html-webpack-plugin";
import { CleanWebpackPlugin } from "clean-webpack-plugin";
import ManifestPlugin from "webpack-manifest-plugin";
import TerserPlugin from "terser-webpack-plugin";
import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer";
import resolvePkg from "resolve-pkg";
const ForkTsCheckerNotifierWebpackPlugin = require("fork-ts-checker-notifier-webpack-plugin");
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin");
const rootPath = path.resolve(__dirname, "./");
const appPath = (nextPath: string) => path.join(rootPath, nextPath);
const pkg = require("./package.json");
const find = (inputPath: string) => {
const result = resolvePkg(inputPath);
if (!result) {
throw new Error(`Not found: ${inputPath}`);
}
return result;
};
export const generateConfig = (isProduction: boolean): webpack.Configuration => {
const isCI = process.env.CI;
const tsLoader: webpack.RuleSetUse = {
loader: "ts-loader",
options: {
configFile: "tsconfig.json",
transpileOnly: true,
},
};
const babelLoader: webpack.RuleSetUse = {
loader: "babel-loader",
options: {
cacheDirectory: true,
presets: ["@babel/preset-env"],
},
};
return {
mode: isProduction ? "production" : "development",
target: "web",
optimization: {
minimize: isProduction,
runtimeChunk: false,
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: true,
},
},
}),
],
splitChunks: {
chunks: "initial",
cacheGroups: {
default: false,
vendors: false,
lib: {
name: "lib",
chunks: "initial",
minChunks: 2,
test: ({ resource: filePath, context: dirPath }, chunk) => {
return [/src/].some((pattern) => pattern.test(filePath));
},
enforce: true,
},
vendor: {
name: "vendor",
chunks: "initial",
test: /node_modules/,
enforce: true,
},
},
},
},
entry: {
application: ["core-js", "regenerator-runtime/runtime", "./src/DevServer.tsx"],
},
// @ts-ignore
devServer: {
watchContentBase: true,
contentBase: appPath("public"),
compress: true,
port: 9000,
open: true,
before: (app: express.Application, _server: any) => {
app.use("/scripts/react.development.js", express.static(find("react/umd/react.development.js")));
app.use("/scripts/react-dom.development.js", express.static(find("react-dom/umd/react-dom.development.js")));
},
},
devtool: "cheap-source-map",
plugins: [
isProduction && !isCI && new BundleAnalyzerPlugin(),
new FriendlyErrorsWebpackPlugin(),
new ForkTsCheckerWebpackPlugin(),
new ForkTsCheckerNotifierWebpackPlugin({ excludeWarnings: true }),
new webpack.HotModuleReplacementPlugin(),
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: pkg.name,
template: "public/index.html",
React: isProduction ? "/scripts/react.production.min.js" : "/scripts/react.development.js",
ReactDOM: isProduction ? "/scripts/react-dom.production.min.js" : "/scripts/react-dom.development.js",
}),
new ManifestPlugin(),
].filter(Boolean),
output: {
filename: "scripts/[name].bundle.js",
path: appPath("dist"),
},
externals: {
react: "React",
"react-dom": "ReactDOM",
},
resolve: {
extensions: [".js", ".ts", ".tsx", ".json"],
alias: {
React: appPath("node_modules/react"),
ReactDOM: appPath("node_modules/react-dom"),
},
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: [/__tests__/, /node_modules/],
loaders: [babelLoader, tsLoader],
},
{
test: /\.js$/,
loader: babelLoader,
},
],
},
};
};
export default generateConfig(process.env.NODE_ENV === "production");