-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsbuild.config.ts
147 lines (131 loc) · 3.25 KB
/
rsbuild.config.ts
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
import { defineConfig } from "@rsbuild/core";
import { pluginVue } from "@rsbuild/plugin-vue";
import klawSync from "klaw-sync";
import path from "node:path";
import os from "node:os";
import convert from "xml-js";
import fs from "node:fs";
import AutoImport from "unplugin-auto-import/rspack";
import Components from "unplugin-vue-components/rspack";
import { VantResolver } from "@vant/auto-import-resolver";
const isDev = process.env.NODE_ENV === "development";
// Collect the pages
function pages() {
const pages = {};
const vues = klawSync("./src/pages", {
nodir: true,
traverseAll: true,
filter(file) {
return path.extname(file.path) === ".vue";
},
});
const pagesIndex = path.resolve(__dirname, "src/pages").length;
vues.forEach((vue) => {
let filename;
if (process.platform !== "win32") {
filename = vue.path.substring(pagesIndex + 1).replace(/\//g, "-");
} else {
filename = vue.path.substring(pagesIndex + 1).replace(/\\/g, "-");
}
const page = filename.substring(0, filename.lastIndexOf("."));
pages[page] = {
import: vue.path,
library: {
name: "apicloud",
type: "window",
},
};
});
return pages;
}
// Get the local ip
function getLocalIP() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const inface of interfaces[name]!) {
const { address, family, internal } = inface;
if (family === "IPv4" && !internal) {
return address;
}
}
}
return "0.0.0.0";
}
// Change the entry
function rewriteXml(entry: string) {
let xml = fs.readFileSync("./apicloud/config.xml", "utf8");
const js = convert.xml2js(xml, { compact: true });
// @ts-ignore
js.widget.content._attributes.src = entry;
xml = convert.js2xml(js, {
compact: true,
spaces: 4,
ignoreComment: true,
});
fs.writeFileSync("./apicloud/config.xml", xml, "utf8");
}
const url = isDev
? `http://${getLocalIP()}:3000/${process.env.APP_ENTRY}.html`
: `${process.env.APP_ENTRY}.html`;
rewriteXml(url);
const copy:
| (
| string
| {
from: string;
to?: string;
}
)[]
| undefined = ["./apicloud/config.xml"];
isDev && copy.push({ from: "./apicloud/vue.global.js", to: "js" });
!isDev &&
copy.push({ from: "./apicloud/vue.runtime.global.prod.js", to: "js" });
export default defineConfig({
plugins: [pluginVue()],
dev: {
lazyCompilation: false,
writeToDisk: (file) => {
return /config.xml$/.test(file);
},
assetPrefix: "./",
},
source: {
entry: pages,
alias: {
"@": "./src",
},
},
output: {
assetPrefix: "./",
filenameHash: false,
distPath: {
js: "js",
css: "css",
},
externals: {
vue: "Vue",
},
copy,
},
tools: {
rspack: {
plugins: [
AutoImport({
resolvers: [VantResolver()],
}),
Components({
resolvers: [VantResolver()],
}),
],
},
htmlPlugin(config, { entryName, entryValue }) {
config.template = "./apicloud/page.ejs";
config.title = entryName;
config.templateParameters = {
isDev,
};
config.scriptLoading = "blocking";
config.minify = !isDev;
},
},
});