-
Notifications
You must be signed in to change notification settings - Fork 4
/
webpack.config.ts
154 lines (148 loc) · 3.75 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
148
149
150
151
152
153
154
import "webpack-dev-server"
import { Configuration, DefinePlugin, ProvidePlugin } from "webpack"
import { merge } from "webpack-merge"
import Dotenv from "dotenv-webpack"
import HtmlWebpackPlugin from "html-webpack-plugin"
import ForkTsCheckerPlugin from "fork-ts-checker-webpack-plugin"
import CopyPlugin from "copy-webpack-plugin"
import "dotenv-defaults/config"
import path from "path"
import fs from "fs/promises"
import child_proces from "child_process"
import packageJson from "./package.json"
const config: Configuration = {
entry: ["./src/index.tsx"],
devtool: "source-map",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
clean: true,
},
resolve: {
modules: [path.resolve(__dirname, "src"), "node_modules"],
extensions: [".tsx", ".ts", ".js"],
preferAbsolute: true,
alias: {
"@": path.resolve(__dirname, "src"),
buffer: "buffer",
crypto: "crypto-browserify",
assert: "assert",
http: "stream-http",
https: "https-browserify",
os: "os-browserify/browser",
process: "process/browser",
stream: "stream-browserify",
util: "util",
},
fallback: {
path: require.resolve("path-browserify"),
},
},
experiments: {
asyncWebAssembly: true,
},
module: {
rules: [
{
test: /\.tsx?$/,
use: "babel-loader",
exclude: /node_modules/,
},
{
test: /\.(png|webp|svg|jpg|gif|woff2|mp4)$/,
type: "asset/resource",
},
],
},
plugins: [
new Dotenv({
defaults: true,
systemvars: true,
}),
new HtmlWebpackPlugin({
template: "./src/public/index.html",
favicon: "./src/public/favicon.svg",
inject: true,
publicPath: "/",
}),
new ForkTsCheckerPlugin({
typescript: {
diagnosticOptions: {
semantic: true,
syntactic: true,
},
mode: "write-references",
},
}),
new ProvidePlugin({
process: "process/browser",
Buffer: ["buffer", "Buffer"],
}),
new CopyPlugin({
patterns: [{ from: "src/assets/", to: "assets/" }],
}),
new DefinePlugin({
"process.env.VERSION": JSON.stringify(packageJson.version),
}),
new DefinePlugin({
"process.env.COMMIT_HASH": JSON.stringify(
child_proces.execSync("git rev-parse --short HEAD").toString().trim()
),
}),
],
devServer: {
static: {
directory: path.join(__dirname, "public"),
},
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",
},
historyApiFallback: true,
compress: true,
port: 9000,
client: {
overlay: false,
},
},
optimization: {
splitChunks: { automaticNameDelimiter: "-" },
},
}
export default async (
_webpackEnv: Record<string, string | boolean>,
argv: {
mode: "development" | "production"
[otherWebpackOption: string]: unknown
}
) => {
const { mode } = argv
const deploymentInfo: { name: string; expectedAddress: string }[] =
JSON.parse(
await fs
.readFile(path.resolve(__dirname, "./deployment-info.json"), "utf-8")
.catch(() => "[]")
)
const overrides: Record<string, Configuration> = {
all: {
plugins: [
new DefinePlugin(
Object.fromEntries(
deploymentInfo.map(({ name, expectedAddress }) => [
`CONTRACT_${name}`,
`"${expectedAddress}"`,
])
)
),
],
},
production: {
output: {
chunkLoading: false,
},
},
}
return merge(config, overrides.all, overrides[mode] || {})
}