vite.config.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /// <reference types="vitest" />
  2. import { babel } from '@rollup/plugin-babel'
  3. import inject from '@rollup/plugin-inject'
  4. import react from '@vitejs/plugin-react'
  5. import * as path from 'path'
  6. import { visualizer } from 'rollup-plugin-visualizer'
  7. import { defineConfig } from 'vite'
  8. import checker from 'vite-plugin-checker'
  9. // https://vitejs.dev/config/
  10. export default defineConfig({
  11. root: './src',
  12. build: {
  13. target: ['chrome87', 'edge88', 'es2020', 'firefox78', 'safari14'],
  14. emptyOutDir: true,
  15. outDir: path.resolve(__dirname, 'dist'),
  16. rollupOptions: {
  17. input: {
  18. main: path.resolve(__dirname, 'src/index.html'),
  19. embedded: path.resolve(__dirname, 'src/embedded/index.html'),
  20. },
  21. },
  22. },
  23. test: {
  24. environment: 'happy-dom',
  25. setupFiles: ['vitest-setup.ts'],
  26. globals: true,
  27. },
  28. worker: {
  29. plugins: [
  30. // This plugin fixes:
  31. // https://github.com/Joystream/atlas/issues/3005
  32. // By default vite was transpiling `import.meta.url` (that you can find in `node_modules/@polkadot/api/packageInfo.js`)
  33. // to the code which uses `document.baseURI`. `Document` is not available in workers and in the result we got reference errors.
  34. // This plugin replace `document.baseURI` with `self.location.href` which should be available in the worker
  35. {
  36. name: 'resolve-import-meta-polkadot',
  37. resolveImportMeta(_, { chunkId }) {
  38. if (chunkId === 'polkadot-worker.js') {
  39. return 'self.location.href'
  40. }
  41. },
  42. },
  43. ],
  44. },
  45. plugins: [
  46. {
  47. name: 'embedded-fallback',
  48. configureServer(server) {
  49. server.middlewares.use('/embedded', (req, res, next) => {
  50. if (req.url?.includes('.')) {
  51. next()
  52. return
  53. }
  54. req.url = '/index.html'
  55. req.originalUrl = '/embedded/index.html'
  56. next()
  57. })
  58. },
  59. },
  60. react({
  61. exclude: /\.stories\.[tj]sx?$/,
  62. }),
  63. checker({
  64. typescript: true,
  65. eslint: {
  66. lintCommand: 'eslint "./**/*.{js,jsx,ts,tsx}"',
  67. dev: { overrideConfig: { ignorePath: '../.eslintignore' } },
  68. },
  69. overlay: false,
  70. }),
  71. babel({
  72. extensions: ['.tsx', '.ts'],
  73. include: ['**/*.style.*', '**/*.styles.*'],
  74. plugins: ['@emotion'],
  75. compact: false,
  76. babelHelpers: 'bundled',
  77. }),
  78. {
  79. ...inject({
  80. include: ['node_modules/**/*.js*'],
  81. modules: {
  82. Buffer: ['buffer', 'Buffer'],
  83. },
  84. }),
  85. enforce: 'post',
  86. },
  87. {
  88. ...visualizer({
  89. filename: 'dist/stats.html',
  90. }),
  91. enforce: 'post',
  92. },
  93. ],
  94. resolve: {
  95. alias: {
  96. '@': path.resolve(__dirname, './src'),
  97. },
  98. },
  99. optimizeDeps: {
  100. include: ['buffer', 'blake3/browser-async', 'multihashes', '@emotion/styled/base'],
  101. },
  102. })