halo-fe-plugin 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/forceAsset404.js +48 -0
- package/dist/htmlTransform.js +35 -0
- package/dist/index.js +10 -0
- package/package.json +25 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { existsSync } from 'fs';
|
|
2
|
+
import { resolve } from 'path';
|
|
3
|
+
/**
|
|
4
|
+
* 默认的静态资源后缀列表
|
|
5
|
+
*/
|
|
6
|
+
const DEFAULT_EXTENSIONS = [
|
|
7
|
+
'js', 'css', 'png', 'jpg', 'jpeg', 'gif', 'svg', 'ico',
|
|
8
|
+
'json', 'woff', 'woff2', 'ttf', 'eot', 'map', 'webp',
|
|
9
|
+
'mp3', 'mp4', 'webm', 'pdf', 'zip', 'rar'
|
|
10
|
+
];
|
|
11
|
+
/**
|
|
12
|
+
* 强制静态资源返回 404 的 Vite 插件
|
|
13
|
+
*
|
|
14
|
+
* 解决问题:Vite SPA 模式会将不存在的静态资源也回退到 index.html
|
|
15
|
+
*
|
|
16
|
+
* @param extensions - 需要检查的文件后缀列表
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import {forceAsset404} from 'halo-fe-plugin'
|
|
21
|
+
*
|
|
22
|
+
* export default defineConfig({
|
|
23
|
+
* plugins: [vue(), forceAsset404()]
|
|
24
|
+
* })
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function forceAsset404(extensions = DEFAULT_EXTENSIONS) {
|
|
28
|
+
const extensionPattern = new RegExp(`\\.(${extensions.join('|')})$`, 'i');
|
|
29
|
+
let root;
|
|
30
|
+
return {
|
|
31
|
+
name: 'halo:force-asset-404',
|
|
32
|
+
configResolved(config) {
|
|
33
|
+
root = config.root;
|
|
34
|
+
},
|
|
35
|
+
configureServer(server) {
|
|
36
|
+
server.middlewares.use((req, res, next) => {
|
|
37
|
+
const url = (req.url || '').split('?')[0];
|
|
38
|
+
if (extensionPattern.test(url) && !existsSync(resolve(root, 'public', url.slice(1)))) {
|
|
39
|
+
res.statusCode = 404;
|
|
40
|
+
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
|
41
|
+
res.end(`404 Not Found: ${url}`);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
next();
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 首页 VITE 变量替换插件
|
|
3
|
+
*
|
|
4
|
+
* 将 index.html 中的 {{VITE_XXX}} 变量替换为实际环境变量值
|
|
5
|
+
* 同时自动注入 {{VITE_RUNTIME_VERSION}} 为当前构建时间
|
|
6
|
+
*
|
|
7
|
+
* @param env - 环境变量对象
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {htmlTransform} from 'halo-fe-plugin'
|
|
12
|
+
*
|
|
13
|
+
* export default defineConfig(({mode}) => {
|
|
14
|
+
* const env = loadEnv(mode, process.cwd())
|
|
15
|
+
* return {
|
|
16
|
+
* plugins: [vue(), htmlTransform(env)]
|
|
17
|
+
* }
|
|
18
|
+
* })
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function htmlTransform(env) {
|
|
22
|
+
// 把 UTC 时间转换成东八区时间
|
|
23
|
+
const time = new Date().toLocaleString('zh-cn', { timeZone: 'Asia/Shanghai' });
|
|
24
|
+
const version = time.replace(/[/:]/g, '').replace(/ /g, '_');
|
|
25
|
+
return {
|
|
26
|
+
name: 'halo:html-transform',
|
|
27
|
+
transformIndexHtml(html) {
|
|
28
|
+
const keys = Object.keys(env);
|
|
29
|
+
for (const key of keys) {
|
|
30
|
+
html = html.replaceAll(`{{${key}}}`, env[key]);
|
|
31
|
+
}
|
|
32
|
+
return html.replace(/{{\s*VITE_RUNTIME_VERSION\s*}}/, version);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "halo-fe-plugin",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Halo 前端框架 开发服务器 插件集合",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"module": "./src/index.ts",
|
|
7
|
+
"types": "./esm/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"halo-fe-plugin:install": "pnpm install",
|
|
13
|
+
"halo-fe-plugin:tsc": "vue-tsc",
|
|
14
|
+
"halo-fe-plugin:build": "tsc",
|
|
15
|
+
"halo-fe-plugin:publish": "tsc && npm publish"
|
|
16
|
+
},
|
|
17
|
+
"author": "jay.zhou <869758965@qq.com>",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "25.2.2",
|
|
21
|
+
"typescript": "5.9.3",
|
|
22
|
+
"vite": "7.3.1",
|
|
23
|
+
"vue-tsc": "3.2.4"
|
|
24
|
+
}
|
|
25
|
+
}
|