vite.config.js 2.0 KB

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