forked from MetaMask/snaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
206 lines (195 loc) · 4.72 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
import ReactRefreshPlugin from '@pmmmwh/react-refresh-webpack-plugin';
// eslint-disable-next-line import/default
import CopyPlugin from 'copy-webpack-plugin';
import express from 'express';
import FaviconsWebpackPlugin from 'favicons-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MonacoEditorWebpackPlugin from 'monaco-editor-webpack-plugin';
import { resolve } from 'path';
import TerserPlugin from 'terser-webpack-plugin';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import type { Configuration } from 'webpack';
import {
ProvidePlugin,
DllPlugin,
DllReferencePlugin,
EnvironmentPlugin,
} from 'webpack';
import type { Configuration as DevServerConfiguration } from 'webpack-dev-server';
import { merge } from 'webpack-merge';
import packageJson from './package.json';
const vendor = Object.entries(packageJson.dependencies)
.filter(([, version]) => !version.startsWith('workspace:'))
.map(([name]) => name);
// This is purposefully not in the `dist` folder, as it will be copied to the
// `dist` folder by the `CopyPlugin` below. This avoids including it twice in
// the final package.
const VENDOR_PATH = resolve(__dirname, 'vendor');
const VENDOR_MANIFEST_PATH = resolve(VENDOR_PATH, 'vendor-manifest.json');
const baseConfig: Configuration = {
stats: 'errors-only',
module: {
rules: [
{
test: /\.tsx?$/u,
use: {
loader: 'swc-loader',
},
},
{
test: /\.m?js$/u,
include: /node_modules/u,
type: 'javascript/auto',
resolve: {
fullySpecified: false,
},
},
],
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
plugins: [
new TsconfigPathsPlugin({
configFile: resolve(__dirname, 'tsconfig.json'),
baseUrl: __dirname,
}),
],
fallback: {
stream: require.resolve('stream-browserify'),
},
},
plugins: [
new ProvidePlugin({
// These Node.js modules are used in some of the stream libs used.
process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
}),
],
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
minify: TerserPlugin.swcMinify,
}),
],
},
};
const vendorConfig: Configuration = merge(baseConfig, {
name: 'vendor',
devtool: false,
entry: {
vendor,
},
output: {
path: VENDOR_PATH,
filename: '[name].js',
library: '[name]_[fullhash]',
},
module: {
rules: [
{
test: /\.css$/u,
use: ['style-loader', 'css-loader'],
},
],
},
resolve: {
extensions: ['.js', '.jsx'],
fallback: {
http: false,
https: false,
zlib: false,
},
},
plugins: [
new DllPlugin({
path: VENDOR_MANIFEST_PATH,
name: '[name]_[fullhash]',
}),
new MonacoEditorWebpackPlugin({
languages: ['json', 'typescript'],
features: ['bracketMatching', 'clipboard', 'hover'],
}),
],
});
const baseAppConfig = merge<Configuration & DevServerConfiguration>(
baseConfig,
{
entry: './src/entry.tsx',
module: {
rules: [
{
test: /\.(png|jpe?g|gif|svg)$/u,
type: 'asset',
},
{
test: /\.woff2?$/u,
type: 'asset/resource',
},
{
test: /\.css$/u,
use: ['style-loader', 'css-loader'],
},
],
},
plugins: [
new DllReferencePlugin({
manifest: VENDOR_MANIFEST_PATH,
}),
new ReactRefreshPlugin(),
new HtmlWebpackPlugin({
template: './src/index.html',
}),
new FaviconsWebpackPlugin('./src/assets/favicon.svg'),
// Copy the Webpack vendor files to the output folder.
new CopyPlugin({
patterns: [
{
from: VENDOR_PATH,
to: 'vendor',
toType: 'dir',
},
],
}),
],
cache: {
type: 'filesystem',
buildDependencies: {
config: [__filename],
},
},
devServer: {
port: 8000,
historyApiFallback: true,
setupMiddlewares: (middlewares, { app }) => {
app?.use('/vendor', express.static(VENDOR_PATH));
return middlewares;
},
},
},
);
const mainConfig = merge(baseAppConfig, {
name: 'main',
output: {
path: resolve(__dirname, 'dist', 'webpack', 'main'),
},
plugins: [
new EnvironmentPlugin({
SNAPS_TEST: false,
}),
],
});
const testConfig = merge(baseAppConfig, {
name: 'test',
devtool: false,
output: {
path: resolve(__dirname, 'dist', 'webpack', 'test'),
},
plugins: [
new EnvironmentPlugin({
SNAPS_TEST: true,
}),
],
});
const configs = [mainConfig, testConfig, vendorConfig];
export default configs;