vite.config.ts 1.3 KB

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