72 lines
2.3 KiB
JavaScript
72 lines
2.3 KiB
JavaScript
import { mkdtemp, readFile } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { build, createServer, loadConfigFromFile } from 'vite'
|
|
import Components from 'unplugin-vue-components/vite'
|
|
import AutoImport from 'unplugin-auto-import/vite'
|
|
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
|
|
|
|
// 与生产配置相同的构建,关闭声明文件生成,避免覆盖工作区已有的用户修改。
|
|
const protectedPaths = [
|
|
'src/types/components.d.ts',
|
|
'src/types/auto-imports.d.ts',
|
|
'.auto-import.json'
|
|
]
|
|
const snapshots = await Promise.all(protectedPaths.map((path) => readFile(path)))
|
|
const serve = process.argv.includes('--serve')
|
|
const loaded = await loadConfigFromFile({
|
|
command: serve ? 'serve' : 'build',
|
|
mode: serve ? 'development' : 'production'
|
|
})
|
|
const outputDir = await mkdtemp(join(tmpdir(), 'one-pipe-october-build-'))
|
|
const plugins = loaded.config.plugins
|
|
.flat(Infinity)
|
|
.filter(
|
|
(plugin) => plugin && !['unplugin-vue-components', 'unplugin-auto-import'].includes(plugin.name)
|
|
)
|
|
plugins.push(
|
|
Components({
|
|
deep: true,
|
|
extensions: ['vue'],
|
|
dirs: ['src/components'],
|
|
resolvers: [ElementPlusResolver()],
|
|
dts: false
|
|
}),
|
|
AutoImport({
|
|
imports: ['vue', 'vue-router', '@vueuse/core', 'pinia'],
|
|
resolvers: [ElementPlusResolver()],
|
|
dts: false,
|
|
eslintrc: { enabled: false }
|
|
})
|
|
)
|
|
if (serve) {
|
|
const { mockApiPlugin } = await import('./octoberBrowserFixtures.mjs')
|
|
const server = await createServer({
|
|
...loaded.config,
|
|
configFile: false,
|
|
mode: 'development',
|
|
plugins: [mockApiPlugin, ...plugins],
|
|
server: { host: '127.0.0.1', port: 5190, strictPort: true, proxy: {} }
|
|
})
|
|
await server.listen()
|
|
console.log(
|
|
'Local mock preview: http://127.0.0.1:5190 (all /api requests intercepted; no real backend writes)'
|
|
)
|
|
} else {
|
|
await build({
|
|
...loaded.config,
|
|
configFile: false,
|
|
plugins,
|
|
logLevel: 'warn',
|
|
build: { ...loaded.config.build, outDir: outputDir, emptyOutDir: false }
|
|
})
|
|
}
|
|
for (let i = 0; i < protectedPaths.length; i++) {
|
|
if (!snapshots[i].equals(await readFile(protectedPaths[i])))
|
|
throw new Error(`Build modified protected file: ${protectedPaths[i]}`)
|
|
}
|
|
if (!serve)
|
|
console.log(
|
|
`Production build passed. Generated declaration files preserved. Output: ${outputDir}`
|
|
)
|