vite.config.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import path from 'node:path';
  2. import { YJS_WEBSOCKET_BASE_PATH } from '@growi/core/dist/consts';
  3. import react from '@vitejs/plugin-react';
  4. import glob from 'glob';
  5. import { nodeExternals } from 'rollup-plugin-node-externals';
  6. import type { Plugin } from 'vite';
  7. import { defineConfig } from 'vite';
  8. import dts from 'vite-plugin-dts';
  9. const YJS_PATH_PREFIX = `${YJS_WEBSOCKET_BASE_PATH}/`;
  10. const excludeFiles = [
  11. '**/components/playground/*',
  12. '**/main.tsx',
  13. '**/vite-env.d.ts',
  14. ];
  15. const devWebSocketPlugin = (): Plugin => ({
  16. name: 'dev-y-websocket',
  17. apply: 'serve',
  18. configureServer(server) {
  19. if (!server.httpServer) return;
  20. // eslint-disable-next-line @typescript-eslint/no-require-imports
  21. const { setupWSConnection } = require('y-websocket/bin/utils');
  22. // eslint-disable-next-line @typescript-eslint/no-require-imports
  23. const { WebSocketServer } = require('ws');
  24. const wss = new WebSocketServer({ noServer: true });
  25. server.httpServer.on('upgrade', (request, socket, head) => {
  26. const url = request.url ?? '';
  27. if (!url.startsWith(YJS_PATH_PREFIX)) return;
  28. const pageId = url.slice(YJS_PATH_PREFIX.length).split('?')[0];
  29. wss.handleUpgrade(request, socket, head, (ws) => {
  30. wss.emit('connection', ws, request);
  31. setupWSConnection(ws, request, { docName: pageId });
  32. });
  33. });
  34. },
  35. });
  36. // https://vitejs.dev/config/
  37. export default defineConfig({
  38. plugins: [
  39. react(),
  40. devWebSocketPlugin(),
  41. dts({
  42. entryRoot: 'src',
  43. exclude: [...excludeFiles],
  44. copyDtsFiles: true,
  45. }),
  46. {
  47. ...nodeExternals({
  48. devDeps: true,
  49. builtinsPrefix: 'ignore',
  50. }),
  51. enforce: 'pre',
  52. },
  53. ],
  54. build: {
  55. outDir: 'dist',
  56. sourcemap: true,
  57. lib: {
  58. entry: glob.sync(path.resolve(__dirname, 'src/**/*.{ts,tsx}'), {
  59. ignore: [...excludeFiles, '**/*.spec.ts'],
  60. }),
  61. name: 'editor-libs',
  62. formats: ['es'],
  63. },
  64. rollupOptions: {
  65. output: {
  66. preserveModules: true,
  67. preserveModulesRoot: 'src',
  68. },
  69. },
  70. },
  71. });