vite.config.js 2.9 KB

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