-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.shared.cjs
268 lines (256 loc) · 7.24 KB
/
webpack.shared.cjs
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2019-2022 @subwallet/sub-connect authors & contributors
// SPDX-License-Identifier: Apache-2.0
const path = require('path');
const webpack = require('webpack');
const CopyPlugin = require('copy-webpack-plugin');
const pkgJson = require('./package.json');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const args = process.argv.slice(2);
let mode = 'production';
if (args) {
args.forEach((p, index) => {
if (p === '--mode') {
mode = args[index + 1] || mode;
}
});
}
console.log('You are using ' + mode + ' mode.');
const packages = [
'sub-connect',
];
const cssRegex = /\.css$/;
const cssModuleRegex = /\.module\.css$/;
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;
const getStyleLoaders = (cssOptions, preProcessor) => {
const loaders = [
isEnvDevelopment && require.resolve('style-loader'),
isEnvProduction && {
loader: MiniCssExtractPlugin.loader,
// css is located in `static/css`, use '../../' to locate index.html folder
// in production `paths.publicUrlOrPath` can be a relative path
options: paths.publicUrlOrPath.startsWith('.')
? {publicPath: '../../'}
: {},
},
{
loader: require.resolve('css-loader'),
options: cssOptions,
},
{
// Options for PostCSS as we reference these options twice
// Adds vendor prefixing based on your specified browser support in
// package.json
loader: require.resolve('postcss-loader'),
options: {
postcssOptions: {
// Necessary for external CSS imports to work
// https://github.com/facebook/create-react-app/issues/2677
ident: 'postcss',
config: false,
plugins: !useTailwind
? [
'postcss-flexbugs-fixes',
[
'postcss-preset-env',
{
autoprefixer: {
flexbox: 'no-2009',
},
stage: 3,
},
],
// Adds PostCSS Normalize as the reset css with default options,
// so that it honors browserslist config in package.json
// which in turn let's users customize the target behavior as per their needs.
'postcss-normalize',
]
: [
'tailwindcss',
'postcss-flexbugs-fixes',
[
'postcss-preset-env',
{
autoprefixer: {
flexbox: 'no-2009',
},
stage: 3,
},
],
],
},
sourceMap: isEnvProduction ? shouldUseSourceMap : isEnvDevelopment,
},
},
].filter(Boolean);
if (preProcessor) {
loaders.push(
{
loader: require.resolve('resolve-url-loader'),
options: {
sourceMap: isEnvProduction ? shouldUseSourceMap : isEnvDevelopment,
root: paths.appSrc,
},
},
{
loader: require.resolve(preProcessor),
options: {
sourceMap: true,
},
}
);
}
return loaders;
};
module.exports = (entry, alias = {}, useSplitChunk = false) => {
const result = {
context: __dirname,
devtool: false,
entry,
devServer: {
port: 7777,
hot: false
},
module: {
rules: [
{
exclude: /(node_modules)/,
test: /\.(js|mjs|ts|tsx)$/,
use: [
{
loader: require.resolve('babel-loader'),
options: require('@polkadot/dev/config/babel-config-webpack.cjs')
}
]
},
{
test: /\.(js|mjs|ts|tsx)$/,
resolve: {
fullySpecified: false,
},
},
{
test: /\.(sa|sc|c)ss$/i,
use: [
// Creates `style` nodes from JS strings
"style-loader",
// Translates CSS into CommonJS
{
loader: 'css-loader',
options: {
modules: {
// Callback must return "local", "global", or "pure" values
mode: (resourcePath) => {
return 'global';
},
},
},
},
// Compiles Sass to CSS
"sass-loader",
],
},
{
test: [/\.svg$/, /\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/, /\.woff2?$/],
use: [
{
loader: require.resolve('url-loader'),
options: {
esModule: false,
limit: 10000,
name: 'static/[name].[ext]'
}
}
]
}
]
},
output: {
chunkFilename: '[name].[contenthash].js',
filename: '[name].[contenthash].js',
globalObject: '(typeof self !== \'undefined\' ? self : this)',
path: path.join(__dirname, 'build'),
publicPath: ''
},
performance: {
hints: false
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
process: 'process/browser.js'
}),
new webpack.IgnorePlugin({
resourceRegExp: /@subwallet\/react-ui\/node_modules\/@google\/model-viewer\/lib\/three-components\/TextureUtils\/.js\//
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(mode),
PKG_NAME: JSON.stringify(pkgJson.name),
PKG_VERSION: JSON.stringify(pkgJson.version)
}
}),
new CopyPlugin({
patterns: [{
from: 'public',
globOptions: {
ignore: [
'**/*.html'
]
}
}]
}),
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'public/index.html',
chunks: ['index']
})
],
resolve: {
alias: packages.reduce((alias, p) => ({
...alias,
[`@subwallet/${p}`]: path.resolve(__dirname, `../${p}/src`)
}), {
...alias,
'react/jsx-runtime': require.resolve('react/jsx-runtime')
}),
extensions: ['.js', '.jsx', '.ts', '.tsx', '.*.js' , '.*.mjs'],
fallback: {
crypto: require.resolve('crypto-browserify'),
path: require.resolve('path-browserify'),
stream: require.resolve('stream-browserify'),
os: require.resolve('os-browserify/browser'),
http: require.resolve('stream-http'),
buffer: require.resolve('buffer'),
https: require.resolve('https-browserify'),
assert: require.resolve('assert'),
url: require.resolve('url'),
zlib: false,
}
},
watch: false,
ignoreWarnings: [
(e) => e.message.includes("Critical dependency: the request of a dependency is an expression"),
],
};
if (useSplitChunk) {
result.optimization = {
splitChunks: {
chunks: 'all',
maxSize: 2000000,
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10
},
default: {
priority: -20,
reuseExistingChunk: true
}
}
}
};
}
return result;
};