Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
127 lines (123 loc) · 3.42 KB
/
Copy pathwebpack.config.js
File metadata and controls
127 lines (123 loc) · 3.42 KB
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
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const devCerts = require("office-addin-dev-certs");
const urlDev = "https://localhost:53000/";
const urlProd = "https://localhost:53000/";
async function getHttpsOptions() {
const httpsOptions = await devCerts.getHttpsServerOptions();
return { ca: httpsOptions.ca, key: httpsOptions.key, cert: httpsOptions.cert };
}
module.exports = async (env, options) => {
const dev = options.mode === "development";
const config = {
devtool: dev ? "inline-source-map" : false,
entry: {
taskpane: "./src/taskpane/taskpane.tsx",
commands: "./src/commands/commands.ts",
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name].bundle.js",
clean: true,
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
alias: {
"@": path.resolve(__dirname, "src"),
},
},
module: {
rules: [
{
test: /\.(ts|tsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
"@babel/preset-env",
["@babel/preset-react", { runtime: "automatic" }],
"@babel/preset-typescript",
],
},
},
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
{
test: /\.(png|jpg|jpeg|gif|ico)$/,
type: "asset/resource",
generator: {
filename: "assets/[name][ext]",
},
},
],
},
optimization: {
splitChunks: {
cacheGroups: {
// 将第三方依赖拆为 vendor chunk,提升 add-in 首屏缓存命中
vendor: {
test: /[\\/]node_modules[\\/]/,
name: "vendor",
chunks: "all",
priority: -10,
},
},
},
},
plugins: [
// dev 模式下随构建做类型检查(生产构建仍靠 tsc --noEmit / CI 把关)
...(dev ? [new ForkTsCheckerWebpackPlugin({ async: false })] : []),
new HtmlWebpackPlugin({
filename: "taskpane.html",
template: "./src/taskpane/taskpane.html",
chunks: ["taskpane"],
}),
new HtmlWebpackPlugin({
filename: "commands.html",
template: "./src/commands/commands.html",
chunks: ["commands"],
}),
new CopyWebpackPlugin({
patterns: [
{
from: "assets",
to: "assets",
globOptions: {
ignore: ["**/winsw/**"],
},
},
{
from: "manifest.xml",
to: "manifest.xml",
},
{
// 本地托管 office.js 及其运行时文件,避免依赖微软 CDN
from: "node_modules/@microsoft/office-js/dist",
to: "office-js",
},
],
}),
],
devServer: {
static: {
directory: path.join(__dirname, "dist"),
},
headers: {
"Access-Control-Allow-Origin": "*",
},
server: {
type: "https",
options: env?.WEBPACK_BUILD || !dev ? {} : await getHttpsOptions(),
},
port: 53000,
hot: true,
},
};
return config;
};