-
Notifications
You must be signed in to change notification settings - Fork 7
/
webpack.config.js
150 lines (146 loc) · 3.72 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import HTMLWebpackPlugin from "html-webpack-plugin";
import StylelintWebpackPlugin from "stylelint-webpack-plugin";
import EslintWebpackPlugin from "eslint-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import HtmlInlineScriptPlugin from "html-inline-script-webpack-plugin";
import path from "path";
import ngrok from "ngrok";
import { fileURLToPath } from "url";
import { createRequire } from "module";
import { readFile } from "fs/promises";
import open from "open";
const require = createRequire(import.meta.url);
const { name } = require("./package.json");
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const isDev = process.env.NODE_ENV === "development";
const config = {
mode: isDev ? "development" : "production",
entry: "./src/index.tsx",
output: {
publicPath: "",
filename: isDev ? "[name].js" : "[contenthash:6].[name].js",
assetModuleFilename: "images/[contenthash:6].[ext]",
path: path.join(__dirname, "dist"),
clean: true,
},
module: {
strictExportPresence: true,
rules: [
{
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
},
{
test: /\.(ts|tsx)$/,
use: "babel-loader",
exclude: /node_modules/,
},
{
test: /\.module.css$/,
include: /src/,
use: [isDev ? "style-loader" : MiniCssExtractPlugin.loader].concat([
{
loader: "css-loader",
options: {
esModule: true,
modules: {
namedExport: true,
localIdentName: "[hash:base64:4]",
},
},
},
"postcss-loader",
]),
},
{
test: /\.css$/,
exclude: /src/,
use: [isDev ? "style-loader" : MiniCssExtractPlugin.loader].concat([
"css-loader",
]),
},
{
test: /\.component.svg$/,
use: ["svg-react-loader"].concat(isDev ? [] : ["svgo-loader"]),
},
{
test: /\.(png|jpg|gif|woff|woff2|ttf)$/i,
type: "asset",
},
{
test: /\.svg$/i,
use: isDev ? [] : ["svgo-loader"],
type: "asset",
},
],
},
optimization: {
splitChunks: {
cacheGroups: {
vendors: {
chunks: "all",
name: "vendors",
test: /node_modules/,
},
},
},
},
plugins: [
new StylelintWebpackPlugin(),
new EslintWebpackPlugin({
extensions: ["ts", "tsx"],
}),
new HTMLWebpackPlugin({
template: "./src/index.html",
title: name,
}),
new HtmlInlineScriptPlugin({
scriptMatchPattern: [/telegram-web-apps/],
}),
],
resolve: {
extensions: [".ts", ".tsx", ".js"],
alias: {
react: path.resolve(__dirname, "./node_modules/react"),
"react-dom": path.resolve(__dirname, "./node_modules/react-dom"),
},
},
devServer: {
allowedHosts: "all",
port: 9000,
client: {
overlay: {
errors: true,
warnings: false,
},
},
onListening: async function (devServer) {
const port = devServer.server.address().port;
try {
const ngrokConfig = JSON.parse(
await readFile(new URL("./ngrok.json", import.meta.url))
);
const url = await ngrok.connect({
host_header: "rewrite",
proto: "http",
addr: port,
...ngrokConfig,
});
await open(url);
} catch (e) {
await open(`http://localhost:${port}`);
}
},
},
};
if (!isDev) {
config.plugins.push(
new MiniCssExtractPlugin({
filename: isDev ? "[name].js" : "[contenthash:6].[name].css",
})
);
}
export default config;