vite.config.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import fs from 'node:fs'
  2. import path from 'node:path'
  3. import { type Plugin, defineConfig } from 'vite'
  4. import vue from '@vitejs/plugin-vue'
  5. import { spawnSync } from 'node:child_process'
  6. const commit = spawnSync('git', ['rev-parse', '--short=7', 'HEAD'])
  7. .stdout.toString()
  8. .trim()
  9. export default defineConfig({
  10. plugins: [
  11. vue({
  12. script: {
  13. fs: {
  14. fileExists: fs.existsSync,
  15. readFile: file => fs.readFileSync(file, 'utf-8'),
  16. },
  17. },
  18. }),
  19. copyVuePlugin(),
  20. ],
  21. define: {
  22. __COMMIT__: JSON.stringify(commit),
  23. __VUE_PROD_DEVTOOLS__: JSON.stringify(true),
  24. },
  25. optimizeDeps: {
  26. exclude: ['@vue/repl'],
  27. },
  28. })
  29. function copyVuePlugin(): Plugin {
  30. return {
  31. name: 'copy-vue',
  32. generateBundle() {
  33. const copyFile = (file: string) => {
  34. const filePath = path.resolve(__dirname, '../../packages', file)
  35. const basename = path.basename(file)
  36. if (!fs.existsSync(filePath)) {
  37. throw new Error(
  38. `${basename} not built. ` +
  39. `Run "nr build vue -f esm-browser" first.`,
  40. )
  41. }
  42. this.emitFile({
  43. type: 'asset',
  44. fileName: basename,
  45. source: fs.readFileSync(filePath, 'utf-8'),
  46. })
  47. }
  48. copyFile(`vue/dist/vue.esm-browser.js`)
  49. copyFile(`vue/dist/vue.esm-browser.prod.js`)
  50. copyFile(`vue/dist/vue.runtime.esm-browser.js`)
  51. copyFile(`vue/dist/vue.runtime.esm-browser.prod.js`)
  52. copyFile(`server-renderer/dist/server-renderer.esm-browser.js`)
  53. },
  54. }
  55. }