cockpitStaticPlugin.js 1.5 KB

123456789101112131415161718192021222324252627282930313233
  1. import fs from 'node:fs'
  2. import path from 'node:path'
  3. // public 是无条件复制区:只发布驾驶舱资源,不发布演示、工具或本机配置。
  4. const PUBLIC_ASSETS = ['cockpit/assets', 'cockpit/models', 'cockpit/aerial.webp', 'logo.png']
  5. export default function cockpitStatic() {
  6. let config
  7. return {
  8. name: 'cockpit-static-allowlist',
  9. config() { return { build: { copyPublicDir: false } } },
  10. configResolved(resolved) { config = resolved },
  11. generateBundle() {
  12. // 旧目录即使被恢复,也不能悄悄回到驾驶舱产物。
  13. for (const id of this.getModuleIds()) {
  14. const relative = path.relative(config.root, id).split(path.sep).join('/')
  15. if (/^(?:src\/plugins|tools|verify|[.]local-archive|dynamic-component-service-master)\//.test(relative)) {
  16. this.error('驾驶舱构建不允许引用隔离内容:' + id)
  17. }
  18. }
  19. },
  20. writeBundle() {
  21. if (config.command !== 'build') return
  22. const outDir = path.resolve(config.root, config.build.outDir)
  23. for (const relative of PUBLIC_ASSETS) {
  24. const source = path.resolve(config.publicDir, relative)
  25. const target = path.resolve(outDir, relative)
  26. fs.mkdirSync(path.dirname(target), { recursive: true })
  27. fs.cpSync(source, target, { recursive: true, filter: (file) => !file.endsWith('.md') })
  28. }
  29. },
  30. }
  31. }