vite.config.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import path from 'node:path';
  2. import react from '@vitejs/plugin-react';
  3. import glob from 'glob';
  4. import { nodeExternals } from 'rollup-plugin-node-externals';
  5. import { Server } from 'socket.io';
  6. import type { Plugin } from 'vite';
  7. import { defineConfig } from 'vite';
  8. import dts from 'vite-plugin-dts';
  9. import { YSocketIO } from 'y-socket.io/dist/server';
  10. const excludeFiles = [
  11. '**/components/playground/*',
  12. '**/main.tsx',
  13. '**/vite-env.d.ts',
  14. ];
  15. const devSocketIOPlugin = (): Plugin => ({
  16. name: 'dev-socket-io',
  17. apply: 'serve',
  18. configureServer(server) {
  19. if (!server.httpServer) return;
  20. // setup socket.io
  21. const io = new Server(server.httpServer);
  22. io.on('connection', (socket) => {
  23. // biome-ignore lint/suspicious/noConsole: Allow to use
  24. console.log('Client connected');
  25. socket.on('disconnect', () => {
  26. // biome-ignore lint/suspicious/noConsole: Allow to use
  27. console.log('Client disconnected');
  28. });
  29. });
  30. // setup y-socket.io
  31. const ysocketio = new YSocketIO(io);
  32. ysocketio.initialize();
  33. },
  34. });
  35. // https://vitejs.dev/config/
  36. export default defineConfig({
  37. plugins: [
  38. react(),
  39. devSocketIOPlugin(),
  40. dts({
  41. entryRoot: 'src',
  42. exclude: [...excludeFiles],
  43. copyDtsFiles: true,
  44. // Fix TS2345/TS2719 "Two different types with this name exist" errors
  45. // during declaration file generation.
  46. //
  47. // vite-plugin-dts internally creates its own TypeScript program, which
  48. // resolves @codemirror/state and @codemirror/view through different pnpm
  49. // symlink chains depending on whether the import originates from
  50. // @growi/editor source or from @uiw/react-codemirror's re-exports.
  51. // Although both chains point to the same physical package, TypeScript
  52. // treats them as distinct types because private fields (e.g.
  53. // SelectionRange#flags) create nominal type identity per declaration.
  54. //
  55. // Pinning paths here forces vite-plugin-dts to resolve these packages
  56. // to a single location regardless of the import origin.
  57. compilerOptions: {
  58. paths: {
  59. '@codemirror/state': [
  60. path.resolve(__dirname, 'node_modules/@codemirror/state'),
  61. ],
  62. '@codemirror/view': [
  63. path.resolve(__dirname, 'node_modules/@codemirror/view'),
  64. ],
  65. },
  66. },
  67. }),
  68. {
  69. ...nodeExternals({
  70. devDeps: true,
  71. builtinsPrefix: 'ignore',
  72. }),
  73. enforce: 'pre',
  74. },
  75. ],
  76. build: {
  77. outDir: 'dist',
  78. sourcemap: true,
  79. lib: {
  80. entry: glob.sync(path.resolve(__dirname, 'src/**/*.{ts,tsx}'), {
  81. ignore: [...excludeFiles, '**/*.spec.ts'],
  82. }),
  83. name: 'editor-libs',
  84. formats: ['es'],
  85. },
  86. rollupOptions: {
  87. output: {
  88. preserveModules: true,
  89. preserveModulesRoot: 'src',
  90. },
  91. },
  92. },
  93. });