vite.config.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { fileURLToPath } from 'node:url'
  2. import { transformAsync } from '@babel/core'
  3. import autoprefixer from 'autoprefixer'
  4. import postcssLogical from 'postcss-logical'
  5. import postcssDirPseudoClass from 'postcss-dir-pseudo-class'
  6. const ROOT = new URL('../../', import.meta.url)
  7. const PACKAGES_ROOT = fileURLToPath(new URL('./packages/', ROOT))
  8. // To enable the plugin, it looks like we need to interact with the resolution
  9. // algorithm, but we need to stop afterwards otherwise it messes up somewhere
  10. // else. This hack can be removed when we get rid of JSX inside of .js files.
  11. let counter = 0
  12. /**
  13. * @type {import('vite').UserConfig}
  14. */
  15. const config = {
  16. build: {
  17. commonjsOptions: {
  18. defaultIsModuleExports: true,
  19. },
  20. },
  21. css: {
  22. postcss: {
  23. plugins: [
  24. autoprefixer,
  25. postcssLogical(),
  26. postcssDirPseudoClass(),
  27. ],
  28. },
  29. },
  30. esbuild: {
  31. jsxFactory: 'h',
  32. jsxFragment: 'Fragment',
  33. },
  34. resolve: {
  35. alias: [
  36. {
  37. find: /^uppy\/(.+)$/,
  38. replacement: `${PACKAGES_ROOT}uppy/$1`,
  39. },
  40. {
  41. find: /^@uppy\/([^/]+)$/,
  42. replacement: `${PACKAGES_ROOT}@uppy/$1/src/index.js`,
  43. },
  44. {
  45. find: /^@uppy\/([^/]+)\/lib\/(.+)$/,
  46. replacement: `${PACKAGES_ROOT}@uppy/$1/src/$2`,
  47. },
  48. // {
  49. // find: /^@uppy\/([^/]+)\/(.+)$/,
  50. // replacement: PACKAGES_ROOT + "@uppy/$1/src/$2",
  51. // },
  52. ],
  53. },
  54. plugins: [
  55. // TODO: remove plugin when we switch to ESM and get rid of JSX inside .js files.
  56. {
  57. name: 'vite-plugin-jsx-commonjs',
  58. // TODO: remove this hack when we get rid of JSX inside .js files.
  59. enforce: 'pre',
  60. // eslint-disable-next-line consistent-return
  61. resolveId (id) {
  62. if (id.startsWith(PACKAGES_ROOT) && id.endsWith('.js')) {
  63. return id
  64. }
  65. // TODO: remove this hack when we get rid of JSX inside .js files.
  66. if (counter++ < 2) {
  67. return id
  68. }
  69. },
  70. transform (code, id) {
  71. if (id.startsWith(PACKAGES_ROOT) && id.endsWith('.js')) {
  72. return transformAsync(code, {
  73. plugins: [
  74. ['@babel/plugin-transform-react-jsx', { pragma: 'h' }],
  75. 'transform-commonjs',
  76. ],
  77. })
  78. }
  79. return code
  80. },
  81. },
  82. ],
  83. }
  84. export default config