-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebpack.config.js
136 lines (130 loc) · 3.77 KB
/
webpack.config.js
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
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const Dotenv = require('dotenv-webpack');
const HtmlInlineScriptPlugin = require('html-inline-script-webpack-plugin');
const repoRoot = path.join(process.cwd());
const monorepoRoot = '../../../';
const envResult = require('dotenv').config({ path: path.resolve(monorepoRoot, '.env') });
if (envResult.error) {
throw envResult.error;
}
const asset_entry = repoRoot + '/src/index.tsx';
const publicPath = monorepoRoot + 'public';
module.exports = (env, argv, dAppConfig) => {
const envFile = path.resolve(monorepoRoot, '.env');
console.log(`\n👉 ${envFile}\n`);
return {
target: 'web',
mode: argv.mode || 'production',
entry: {
index: asset_entry,
},
devtool: argv.mode ? 'inline-source-map' : false,
optimization: {
minimize: !argv.mode,
minimizer: [new TerserPlugin()],
},
resolveLoader: {
modules: [path.join(__dirname, 'node_modules')],
},
resolve: {
modules: [path.join(__dirname, 'node_modules'), path.join(repoRoot, 'node_modules')],
extensions: ['.js', '.ts', '.jsx', '.tsx'],
alias: {
react: path.join(__dirname, 'node_modules/react'),
},
fallback: {
fs: false,
path: false,
crypto: require.resolve('crypto-browserify'),
stream: require.resolve('stream-browserify'),
},
},
output: {
filename: dAppConfig?.name ? `${dAppConfig?.name}.js` : 'index.js',
path: path.join(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.(js|jsx|ts|tsx)$/,
use: {
loader: 'babel-loader',
options: {
rootMode: 'upward',
cacheDirectory: true,
envName: argv.mode || 'production',
},
},
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
{
test: /\.svg$/,
use: ['@svgr/webpack'],
},
{
test: /\.(eot|otf|ttf|woff|woff2|png|jpg)$/,
loader: require.resolve('file-loader'),
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
},
],
},
plugins: [
new Dotenv({
path: envFile,
}),
new webpack.ProvidePlugin({
React: 'react',
}),
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
new webpack.ProvidePlugin({
process: 'process/browser',
}),
new HtmlWebpackPlugin(
argv?.mode === 'development'
? {
template: path.resolve(publicPath, 'index.html'),
inject: 'body',
publicPath: `/`,
}
: {
template: path.resolve(publicPath, 'index.html'),
filename: dAppConfig?.name ? `${dAppConfig?.name}.html` : 'index.html',
inject: 'body',
},
),
new HtmlInlineScriptPlugin(),
new ForkTsCheckerWebpackPlugin({
async: false,
typescript: {
configFile: path.resolve(monorepoRoot, 'tsconfig.json'),
},
}),
],
// proxy /api to port 8080 during development
devServer: {
static: {
directory: path.resolve(__dirname, './'),
watch: true,
},
historyApiFallback: true,
hot: 'only',
compress: true,
allowedHosts: ['localhost'],
open: `-/${process.env.NFT_CANISTER_ID || 'mludz-biaaa-aaaal-qbhwa-cai'}/collection/-/${
dAppConfig?.name
}`,
port: process.env.DEV_SERVER_PORT,
},
};
};