vite.config.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { fileURLToPath } from 'node:url'
  2. import { transformAsync } from '@babel/core'
  3. import t from '@babel/types'
  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. /**
  10. * @type {import('vite').UserConfig}
  11. */
  12. const config = {
  13. envDir: fileURLToPath(ROOT),
  14. css: {
  15. postcss: {
  16. plugins: [
  17. autoprefixer,
  18. postcssLogical(),
  19. postcssDirPseudoClass(),
  20. ],
  21. },
  22. },
  23. esbuild: {
  24. jsxFactory: 'h',
  25. jsxFragment: 'Fragment',
  26. },
  27. resolve: {
  28. alias: [
  29. {
  30. find: /^uppy\/(.+)$/,
  31. replacement: `${PACKAGES_ROOT}uppy/$1`,
  32. },
  33. {
  34. find: /^@uppy\/([^/]+)$/,
  35. replacement: `${PACKAGES_ROOT}@uppy/$1/src/index.js`,
  36. },
  37. {
  38. find: /^@uppy\/([^/]+)\/lib\/(.+)$/,
  39. replacement: `${PACKAGES_ROOT}@uppy/$1/src/$2`,
  40. },
  41. // {
  42. // find: /^@uppy\/([^/]+)\/(.+)$/,
  43. // replacement: PACKAGES_ROOT + "@uppy/$1/src/$2",
  44. // },
  45. ],
  46. },
  47. plugins: [
  48. // TODO: remove plugin when we remove the socket.io require call in @uppy/transloadit/src/Assembly.
  49. {
  50. name: 'vite-plugin-rewrite-dynamic-socketIo-require',
  51. // eslint-disable-next-line consistent-return
  52. resolveId (id) {
  53. if (id.startsWith(PACKAGES_ROOT) && id.endsWith('transloadit/src/Assembly.js')) {
  54. return id
  55. }
  56. },
  57. transform (code, id) {
  58. if (id.startsWith(PACKAGES_ROOT) && id.endsWith('transloadit/src/Assembly.js')) {
  59. return transformAsync(code, {
  60. plugins: [
  61. {
  62. visitor: {
  63. FunctionDeclaration (path) {
  64. if (path.node.id.name === 'requireSocketIo') {
  65. const prevSibling = path.getPrevSibling()
  66. if (t.isImportDeclaration(prevSibling) && prevSibling.node.specifiers?.length === 1
  67. && t.isImportDefaultSpecifier(prevSibling.node.specifiers[0])
  68. && prevSibling.node.specifiers[0].local.name === 'socketIo') {
  69. // The require call has already been rewritten to an import statement.
  70. return
  71. }
  72. if (!t.isVariableDeclaration(prevSibling)) {
  73. const { type, loc } = prevSibling.node
  74. throw new Error(`Unexpected ${type} at line ${loc.start.line}, cannot apply requireSocketIo hack`)
  75. }
  76. const { id:socketIoIdentifier } = prevSibling.node.declarations[0]
  77. prevSibling.replaceWith(t.importDeclaration(
  78. [t.importDefaultSpecifier(socketIoIdentifier)],
  79. t.stringLiteral('socket.io-client'),
  80. ))
  81. path.replaceWith(t.functionDeclaration(path.node.id, path.node.params, t.blockStatement([
  82. t.returnStatement(socketIoIdentifier),
  83. ])))
  84. }
  85. },
  86. },
  87. },
  88. ],
  89. })
  90. }
  91. return code
  92. },
  93. },
  94. ],
  95. }
  96. export default config