forked from tahowallet/extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.ts
222 lines (215 loc) · 6.95 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import path from "path"
import webpack, {
Configuration,
DefinePlugin,
WebpackOptionsNormalized,
WebpackPluginInstance,
} from "webpack"
import { merge as webpackMerge } from "webpack-merge"
import Dotenv from "dotenv-webpack"
import SizePlugin from "size-plugin"
import TerserPlugin from "terser-webpack-plugin"
import LiveReloadPlugin from "webpack-livereload-plugin"
import CopyPlugin, { ObjectPattern } from "copy-webpack-plugin"
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin"
import WebExtensionArchivePlugin from "./build-utils/web-extension-archive-webpack-plugin"
import InjectWindowProvider from "./build-utils/inject-window-provider"
const supportedBrowsers = ["brave", "chrome", "edge", "firefox", "opera"]
// Replicated and adjusted for each target browser and the current build mode.
const baseConfig: Configuration = {
devtool: "source-map",
stats: "errors-only",
entry: {
ui: "./src/ui.ts",
"tab-ui": "./src/tab-ui.ts",
background: "./src/background.ts",
"background-ui": "./src/background-ui.ts",
"window-provider": "./src/window-provider.ts",
"provider-bridge": "./src/provider-bridge.ts",
},
module: {
rules: [
{
test: /\.(tsx|ts|jsx)?$/,
exclude: /node_modules(?!\/@tallyho)|webpack/,
use: [
{
loader: "babel-loader",
options: {
cacheDirectory: true,
},
},
],
},
],
},
output: {
// path: is set browser-specifically below
filename: "[name].js",
},
resolve: {
extensions: [".tsx", ".ts", ".js", ".jsx"],
fallback: {
stream: require.resolve("stream-browserify"),
process: require.resolve("process/browser"),
// these are required for @tallyho/keyring-controller
crypto: require.resolve("crypto-browserify"),
},
},
plugins: [
new InjectWindowProvider(),
new Dotenv({
defaults: true,
systemvars: true,
safe: true,
}),
new ForkTsCheckerWebpackPlugin({
typescript: {
diagnosticOptions: {
semantic: true,
syntactic: true,
},
mode: "write-references",
},
}),
// polyfill the process and Buffer APIs
new webpack.ProvidePlugin({
Buffer: ["buffer", "Buffer"],
process: ["process"],
}),
new SizePlugin({}),
new CopyPlugin({
patterns: [
{
from: "node_modules/@tallyho/tally-ui/_locales/",
to: "_locales/",
},
{
from: "node_modules/@tallyho/tally-ui/public/",
},
],
// FIXME Forced cast below due to an incompatibility between the webpack
// FIXME version refed in @types/copy-webpack-plugin and our local
// FIXME webpack version.
}) as unknown as WebpackPluginInstance,
new DefinePlugin({
"process.env.VERSION": JSON.stringify(process.env.npm_package_version),
}),
],
optimization: {
splitChunks: { automaticNameDelimiter: "-" },
},
}
// Configuration adjustments for specific build modes, customized by browser.
const modeConfigs: {
[mode: string]: (browser: string) => Partial<Configuration>
} = {
development: () => ({
plugins: [
new LiveReloadPlugin({}),
new CopyPlugin({
patterns: ["dev-utils/*.js"],
// FIXME Forced cast below due to an incompatibility between the webpack
// FIXME version refed in @types/copy-webpack-plugin and our local
// FIXME webpack version.
}) as unknown as WebpackPluginInstance,
],
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
mangle: false,
compress: false,
output: {
beautify: true,
indent_level: 2, // eslint-disable-line camelcase
},
},
}),
],
},
}),
production: (browser) => ({
plugins: [
new WebExtensionArchivePlugin({
filename: browser,
}),
],
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
mangle: browser === "firefox",
compress: browser === "firefox",
output:
browser === "firefox"
? undefined
: {
beautify: true,
indent_level: 2, // eslint-disable-line camelcase
},
},
}),
],
},
}),
}
// One config per supported browser, adjusted by mode.
export default (
_: unknown,
{ mode }: WebpackOptionsNormalized
): webpack.Configuration[] =>
supportedBrowsers.map((browser) => {
const distPath = path.join(__dirname, "dist", browser)
// Try to find a build mode config adjustment and call it with the browser.
const modeSpecificAdjuster =
typeof mode !== "undefined" ? modeConfigs[mode] : undefined
const modeSpecificAdjustment =
typeof modeSpecificAdjuster !== "undefined"
? modeSpecificAdjuster(browser)
: {}
return webpackMerge(baseConfig, modeSpecificAdjustment, {
name: browser,
output: {
path: distPath,
},
plugins: [
// Handle manifest adjustments. Adjustments are looked up and merged:
// - by mode (`manifest.<mode>.json`)
// - by browser (`manifest.<browser>.json`)
// - by mode and browser both (`manifest.<mode>.<browser>.json`)
//
// Files that don't exist are ignored, while files with invalid data
// throw an exception. The merge order means that e.g. a mode+browser
// adjustment will override a browser adjustment, which will override a
// mode adjustment in turn.
//
// Merging currently only supports adding keys, overriding existing key
// values if their values are not arrays, or adding entries to arrays.
// It does not support removing keys or array values. webpackMerge is
// used for this.
new CopyPlugin({
patterns: [
{
from: `manifest/manifest(|.${mode}|.${browser}|.${browser}.${mode}).json`,
to: "manifest.json",
transformAll: (assets: { data: Buffer }[]) => {
const combinedManifest = webpackMerge(
{},
...assets
.map((asset) => asset.data.toString("utf8"))
// JSON.parse chokes on empty strings
.filter((assetData) => assetData.trim().length > 0)
.map((assetData) => JSON.parse(assetData))
)
return JSON.stringify(combinedManifest, null, 2)
},
} as unknown as ObjectPattern, // ObjectPattern doesn't include transformAll in current types
],
// FIXME Forced cast below due to an incompatibility between the webpack
// FIXME version refed in @types/copy-webpack-plugin and our local
// FIXME webpack version.
}) as unknown as WebpackPluginInstance,
],
})
})