feat: 构建时把 extra/ 目录内容复制进 dist
This commit is contained in:
1 parent
84c60ab758
commit
f488c2fdd4
3 files changed
+44
-8
No files matched your search
@@ -27,7 +27,8 @@ npm run lint # ESLint 检查
|
|||||||
```
|
```
|
||||||
|
|
||||||
> 构建脚本为 `tsc -b && vite build && node scripts/postbuild.mjs`,最后一步会把
|
> 构建脚本为 `tsc -b && vite build && node scripts/postbuild.mjs`,最后一步会把
|
||||||
> `dist/index.html` 复制成 `dist/more/index.html`,用于静态托管。
|
> `dist/index.html` 复制成 `dist/more/index.html`,用于静态托管,并把项目根目录
|
||||||
|
> `extra/` 里的所有文件复制进 `dist/`(见下方「额外静态文件」)。
|
||||||
|
|
||||||
## 目录结构
|
## 目录结构
|
||||||
|
|
||||||
@@ -54,7 +55,8 @@ src/
|
|||||||
├── hooks/ useTypewriter / useParallax
|
├── hooks/ useTypewriter / useParallax
|
||||||
├── types/index.ts NavItem 类型
|
├── types/index.ts NavItem 类型
|
||||||
└── assets/banner.png 全站背景图
|
└── assets/banner.png 全站背景图
|
||||||
scripts/postbuild.mjs 构建后处理(生成 dist/more/index.html)
|
extra/ 额外静态文件(构建时原样复制进 dist/)
|
||||||
|
scripts/postbuild.mjs 构建后处理(生成 dist/more/index.html、复制 extra/)
|
||||||
```
|
```
|
||||||
|
|
||||||
## 添加 Item
|
## 添加 Item
|
||||||
@@ -165,6 +167,29 @@ writeFileSync(resolve(blogDir, "index.html"), indexHtml);
|
|||||||
> 如果托管平台已配置 SPA history fallback(如 Cloudflare Pages / Netlify 的 rewrite),
|
> 如果托管平台已配置 SPA history fallback(如 Cloudflare Pages / Netlify 的 rewrite),
|
||||||
> 这一步可以省略。
|
> 这一步可以省略。
|
||||||
|
|
||||||
|
## 额外静态文件(`extra/`)
|
||||||
|
|
||||||
|
如果有一些**不属于源码、但希望每次构建都出现在站点里**的文件(例如独立的小工具页面、
|
||||||
|
验证文件、下载资源等),把它们放进项目根目录的 `extra/` 即可,无需改配置:
|
||||||
|
|
||||||
|
```
|
||||||
|
extra/
|
||||||
|
├── hello.txt -> dist/hello.txt
|
||||||
|
├── tools/ -> dist/tools/
|
||||||
|
│ └── index.html -> dist/tools/index.html
|
||||||
|
└── .well-known/ -> dist/.well-known/
|
||||||
|
└── verification -> dist/.well-known/verification
|
||||||
|
```
|
||||||
|
|
||||||
|
- **构建时**:`scripts/postbuild.mjs` 会递归复制 `extra/` 里的所有内容到 `dist/` 根目录
|
||||||
|
(是合并,不会生成 `dist/extra/`)。所以 `extra/a.txt` 最终可访问路径是 `/a.txt`。
|
||||||
|
- **目录不存在时**:自动跳过,构建不会报错。空目录不会被 git 跟踪,仓库里用
|
||||||
|
`extra/.gitkeep` 占位。
|
||||||
|
- **只影响构建产物**:`extra/` 不参与 `npm run dev`,文件不会出现在开发服务器上;
|
||||||
|
若开发时也要能访问,请改用 Vite 原生的 `public/` 目录。
|
||||||
|
- **注意**:`extra/` 中的文件会覆盖 `dist/` 里的同名文件。比如放一个 `index.html` 会
|
||||||
|
覆盖站点首页,请避免。
|
||||||
|
|
||||||
## 路由
|
## 路由
|
||||||
|
|
||||||
路由是自研的零依赖实现,核心思路:
|
路由是自研的零依赖实现,核心思路:
|
||||||
@@ -194,11 +219,11 @@ npm run build
|
|||||||
```
|
```
|
||||||
|
|
||||||
产物在 `dist/`,可直接托管到任意静态平台。注意构建会自动把
|
产物在 `dist/`,可直接托管到任意静态平台。注意构建会自动把
|
||||||
`dist/index.html` 复制成 `dist/more/index.html`,保证 `/more` 可直达。
|
`dist/index.html` 复制成 `dist/more/index.html`,保证 `/more` 可直达,并把 `extra/`
|
||||||
|
的内容合并进 `dist/`。
|
||||||
|
|
||||||
## 备注
|
## 备注
|
||||||
|
|
||||||
- 全站背景图为 `src/assets/banner.png`,由 Vite 打包(带 hash)。
|
- 全站背景图为 `src/assets/banner.png`,由 Vite 打包(带 hash)。
|
||||||
- `index.html` 中接入了 umami 访问统计(`umami.xiaozhou233.cn`),如不需要可移除该 `<script>`。
|
- `index.html` 中接入了 umami 访问统计(`umami.xiaozhou233.cn`),如不需要可移除该 `<script>`。
|
||||||
- 首页导航目前包含一个「Maintenance」占位项,原来的 Blog / Git / Jenkins / Workstation
|
- 首页导航当前包含 Git / Jenkins / Workstation / More 四项。
|
||||||
入口已临时下线。
|
|
||||||
Whitespace-only changes.
+14
-3
@@ -1,12 +1,23 @@
|
|||||||
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
import { cpSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||||
import { dirname, resolve } from "node:path";
|
import { dirname, resolve } from "node:path";
|
||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
|
|
||||||
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||||
const indexHtml = readFileSync(resolve(root, "dist", "index.html"), "utf8");
|
const distDir = resolve(root, "dist");
|
||||||
|
const indexHtml = readFileSync(resolve(distDir, "index.html"), "utf8");
|
||||||
|
|
||||||
const moreDir = resolve(root, "dist", "more");
|
const moreDir = resolve(distDir, "more");
|
||||||
mkdirSync(moreDir, { recursive: true });
|
mkdirSync(moreDir, { recursive: true });
|
||||||
writeFileSync(resolve(moreDir, "index.html"), indexHtml);
|
writeFileSync(resolve(moreDir, "index.html"), indexHtml);
|
||||||
|
|
||||||
console.log("Copied dist/index.html -> dist/more/index.html");
|
console.log("Copied dist/index.html -> dist/more/index.html");
|
||||||
|
|
||||||
|
// 把项目根目录 extra/ 里的所有文件递归复制到 dist/(每次构建)
|
||||||
|
// 目录不存在时跳过,方便用户按需启用
|
||||||
|
const extraDir = resolve(root, "extra");
|
||||||
|
if (existsSync(extraDir)) {
|
||||||
|
cpSync(extraDir, distDir, { recursive: true });
|
||||||
|
console.log("Copied extra/ -> dist/");
|
||||||
|
} else {
|
||||||
|
console.log("Skipped extra/ (not found)");
|
||||||
|
}
|
||||||
Reference in new issue
Block a user