diff --git a/packages/src/index.ts b/packages/src/index.ts index 325ae1f43c257257d91660571acf71845f2b85de..a154d46c0007bb0db8b25b2d77cd659d5b86ea00 100644 --- a/packages/src/index.ts +++ b/packages/src/index.ts @@ -1,11 +1,13 @@ import seoPrerender from './render' import childProcess from 'child_process' +import path from 'path' interface Config { puppeteer?: any // puppeteer一些配置 - routes: string[] // 需要生成的路由地址 + routes?: string[] // 需要生成的路由地址 removeStyle?: boolean // 启用vite preview会自带有些样式,默认下移除 callback?: Function + htmlRoutes?: string[] // 处理public目录下的html文件 } const prerender = (config: Config) => { @@ -26,6 +28,23 @@ const prerender = (config: Config) => { buildEnd() { //console.log('buildEnd') }, + configureServer(server) { + const {watcher} = server + if (config.htmlRoutes?.length) { + // 监听 public 目录下的 HTML 文件更改 + watcher.on('change', (filePath) => { + const publicRoot = path.join(server.config.root, '/public') + const relativePath = path.relative(publicRoot, filePath) + console.log('relativePath', relativePath) + /*if (filePath.startsWith(server.config.root + '/public/') && filePath.endsWith('.html')) { + console.log(`Detected change in HTML file: ${filePath}`); + + // 在此处进行你的处理逻辑 + // 可以读取文件内容、替换内容、编译等操作 + }*/ + }) + } + }, closeBundle() { if (!config?.routes?.length) { console.log('路由地址为空,请配置需预渲染的routes') diff --git a/public/contact/index.html b/public/contact/index.html new file mode 100644 index 0000000000000000000000000000000000000000..7301da17dc73edb5333cda5e5e19ac7aa6b94548 --- /dev/null +++ b/public/contact/index.html @@ -0,0 +1,11 @@ + + + + + Title + + +this is contact page ,transform 123? +

1234567891011

+ + diff --git a/src/assets/style.css b/src/assets/style.css new file mode 100644 index 0000000000000000000000000000000000000000..09f86774ffe02b28f785d57f1b746e223f92f668 --- /dev/null +++ b/src/assets/style.css @@ -0,0 +1 @@ +body{margin: 0;padding: 0;font-size: 14px;} diff --git a/src/main.ts b/src/main.ts index e45590078a5d492e1c59bfd9b686720353e5206b..f52c3472d69eb759881337770bbfcb013efd518d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,6 @@ import {createApp} from 'vue' import App from './App.vue' import router from './router' +import './assets/style.css' createApp(App).use(router).mount('#app') diff --git a/src/views/about/index.vue b/src/views/about/index.vue index 446cb112ad89a502023ae533e7a8b65157f507fb..f3a188d1c6919ef29cb8e4b7a37911f1dea64bc3 100644 --- a/src/views/about/index.vue +++ b/src/views/about/index.vue @@ -1,7 +1,7 @@ - diff --git a/src/views/test.vue b/src/views/test.vue index 3d9a0672e0484c83a23e860c26cd60495278a703..b56fd83162869153e9a3a4a1c5ee249050079b07 100644 --- a/src/views/test.vue +++ b/src/views/test.vue @@ -1,8 +1,11 @@ + diff --git a/vite.config.ts b/vite.config.ts index 2b2172a942677cb8c1c207ae56fbc4325d2ea3a9..95cf58cf0b423af34cd6c5a839ee9bdfb329c517 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -7,6 +7,9 @@ import seoPrerender from './packages/src' export default defineConfig({ plugins: [ vue(), - seoPrerender({routes: ['/about']}) + seoPrerender({ + routes: ['/about'], + htmlRoutes: ['/contact/index.html'] + }) ] })