import type { Plugin } from 'vite' import path from 'node:path' import tailwindcss from '@tailwindcss/vite' import react from '@vitejs/plugin-react' import { defineConfig } from 'vite' /** * 打包出的块里有 import 入口的就报错,免得到了 VS Code 里页面整个跑两遍。 * * 入口由加载方加上 `?v=` 加载(见 [webview 页面](vscode/src/webview/html.ts#webviewHtml)),别的块按不带 `?v=` 的地址 * import 入口,浏览器当它是另一个模块,再加载、再跑一遍。 */ function noEntryImports(): Plugin { return { name: 'no-entry-imports', enforce: 'post', generateBundle(_options, bundle) { const chunks = Object.values(bundle).filter(file => file.type === 'chunk') const entries = new Set(chunks.filter(chunk => chunk.isEntry).map(chunk => chunk.fileName)) for (const chunk of chunks) { const imported = [...chunk.imports, ...chunk.dynamicImports].filter(file => entries.has(file)) if (imported.length > 0) this.error(`${chunk.fileName} import 了入口 ${imported.join('、')}:入口带 ?v= 加载,这样会再跑一遍。把它们共用的模块用 manualChunks 拆进带哈希的块`) } }, } } export default defineConfig({ plugins: [react(), tailwindcss(), noEntryImports()], base: './', build: { outDir: '../dist/webview-ui', emptyOutDir: true, // webview 的 CSP 只放行带 nonce 的