-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.mjs
68 lines (65 loc) · 1.82 KB
/
webpack.config.mjs
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
// 引入路径模块
import { resolve as _resolve, dirname } from "path";
import HtmlWebpackPlugin from "html-webpack-plugin";
import openBrowser from "react-dev-utils/openBrowser.js";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export const entry = "./src/index.ts";
export const output = {
path: _resolve(__dirname, "dist"),
filename: "[name].[contenthash].js",
};
export default {
// 从哪里开始编译
entry: "./src/index.ts",
// 编译到哪里
output: {
path: _resolve(__dirname, "dist"),
filename: "[name].[contenthash].js",
},
// 配置模块规则
module: {
rules: [
{
test: /\.tsx?$/, // .ts或者tsx后缀的文件,就是typescript文件
use: "ts-loader", // 就是上面安装的ts-loader
exclude: "/node-modules/", // 排除node-modules目录
},
{
test: /\.css$/i,
use: ["style-loader", "css-loader"],
},
],
},
// 模式
mode: "development",
resolve: {
extensions: [".ts", ".js"], // 配置ts文件可以作为模块加载
},
devtool: "cheap-module-source-map",
devServer: {
hot: true, // 热更新
// open: true, // 服务启动后,自动打开浏览器
port: 8011, // 服务端口
// host: "127.0.0.1", // 服务
host: "0.0.0.0",
allowedHosts: [".e3b03v-8011.csb.app"], // 允许 codessandbox
onListening: function (devServer) {
if (!devServer) {
throw new Error("webpack-dev-server is not defined");
}
const addr = devServer.server.address();
openBrowser(`http://${addr.address}:${addr.port}`);
},
},
watchOptions: {
poll: 1000,
},
plugins: [
new HtmlWebpackPlugin({
title: "打工人的一天",
template: "public/index.html",
}),
],
};