Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
executable file
·120 lines (118 loc) · 3.73 KB
/
Copy pathwebpack.config.js
File metadata and controls
executable file
·120 lines (118 loc) · 3.73 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
const path = require("path");
const {EsbuildPlugin} = require("esbuild-loader");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const ZipPlugin = require("zip-webpack-plugin");
const pluginManifest = require("./plugin.json");
// 图标与预览图是可选的:plugin.json 里声明了就用声明的文件名,
// 没声明才回退到包根目录的 icon.png / preview.png。
// 注意两者的严格程度不同:
// - 已声明:该文件必须存在,缺失视为打包配置错误,构建应当失败
// - 未声明:遗留同名文件可有可无,缺失时静默跳过
const packageImagePatterns = [
["icon", "icon.png"],
["preview", "preview.png"],
].map(([field, legacyName]) => {
const declared = pluginManifest[field];
return {
from: declared || legacyName,
to: "./dist/",
noErrorOnMissing: !declared,
};
});
module.exports = (env, argv) => {
const production = argv.mode === "production";
const plugins = [
new MiniCssExtractPlugin({
filename: production ? "dist/index.css" : "index.css",
}),
];
if (production) {
plugins.push(
new CopyPlugin({
patterns: [
...packageImagePatterns,
{from: "README*.md", to: "./dist/"},
{from: "plugin.json", to: "./dist/"},
{from: "src/i18n/", to: "./dist/i18n/"},
],
}),
);
plugins.push(
new ZipPlugin({
filename: "package.zip",
algorithm: "gzip",
include: [/dist/],
pathMapper: (assetPath) => {
return assetPath.replace("dist/", "");
},
}),
);
} else {
plugins.push(
new CopyPlugin({
patterns: [
{from: "src/i18n/", to: "./i18n/"},
],
}),
);
}
return {
mode: argv.mode || "development",
watch: !production,
devtool: production ? false : "eval-source-map",
output: {
filename: "[name].js",
path: path.resolve(__dirname),
libraryTarget: "commonjs2",
library: {
type: "commonjs2",
},
},
externals: {
siyuan: "siyuan",
},
entry: {
[production ? "dist/index" : "index"]: "./src/index.ts",
},
optimization: {
minimize: production,
minimizer: [
new EsbuildPlugin(),
],
},
resolve: {
extensions: [".ts", ".scss", ".js", ".json"],
},
module: {
rules: [
{
test: /\.ts(x?)$/,
include: [path.resolve(__dirname, "src")],
use: [
{
loader: "esbuild-loader",
options: {
target: "es2020",
},
},
],
},
{
test: /\.scss$/,
include: [path.resolve(__dirname, "src")],
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader", // translates CSS into CommonJS
},
{
loader: "sass-loader", // compiles Sass to CSS
},
],
},
],
},
plugins,
};
};