|
| 1 | +import { cloudflareDevProxyVitePlugin as remixCloudflareDevProxy, vitePlugin as remixVitePlugin } from '@remix-run/dev'; |
| 2 | +import UnoCSS from 'unocss/vite'; |
| 3 | +import { defineConfig, type ViteDevServer } from 'vite'; |
| 4 | +import { nodePolyfills } from 'vite-plugin-node-polyfills'; |
| 5 | +import { optimizeCssModules } from 'vite-plugin-optimize-css-modules'; |
| 6 | +import tsconfigPaths from 'vite-tsconfig-paths'; |
| 7 | +import * as dotenv from 'dotenv'; |
| 8 | + |
| 9 | +// Load environment variables from multiple files |
| 10 | +dotenv.config({ path: '.env.local' }); |
| 11 | +dotenv.config({ path: '.env' }); |
| 12 | +dotenv.config(); |
| 13 | + |
| 14 | +export default defineConfig((config) => { |
| 15 | + return { |
| 16 | + define: { |
| 17 | + 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV), |
| 18 | + }, |
| 19 | + build: { |
| 20 | + target: 'esnext', |
| 21 | + rollupOptions: { |
| 22 | + onwarn(warning, warn) { |
| 23 | + // Suppress warnings about mixed static/dynamic imports |
| 24 | + if (warning.code === 'MODULE_LEVEL_DIRECTIVE' || |
| 25 | + (warning.message && warning.message.includes('is dynamically imported by') && warning.message.includes('but also statically imported by'))) { |
| 26 | + return; |
| 27 | + } |
| 28 | + warn(warning); |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + plugins: [ |
| 33 | + nodePolyfills({ |
| 34 | + include: ['buffer', 'process', 'util', 'stream', 'path'], |
| 35 | + globals: { |
| 36 | + Buffer: true, |
| 37 | + process: true, |
| 38 | + global: true, |
| 39 | + }, |
| 40 | + protocolImports: true, |
| 41 | + exclude: ['child_process', 'fs'], |
| 42 | + }), |
| 43 | + { |
| 44 | + name: 'buffer-polyfill', |
| 45 | + transform(code, id) { |
| 46 | + if (id.includes('env.mjs')) { |
| 47 | + return { |
| 48 | + code: `import { Buffer } from 'buffer';\n${code}`, |
| 49 | + map: null, |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + return null; |
| 54 | + }, |
| 55 | + }, |
| 56 | + config.mode !== 'test' && remixCloudflareDevProxy(), |
| 57 | + remixVitePlugin({ |
| 58 | + future: { |
| 59 | + v3_fetcherPersist: true, |
| 60 | + v3_relativeSplatPath: true, |
| 61 | + v3_throwAbortReason: true, |
| 62 | + v3_lazyRouteDiscovery: true, |
| 63 | + }, |
| 64 | + }), |
| 65 | + UnoCSS(), |
| 66 | + tsconfigPaths(), |
| 67 | + chrome129IssuePlugin(), |
| 68 | + config.mode === 'production' && optimizeCssModules({ apply: 'build' }), |
| 69 | + ], |
| 70 | + envPrefix: [ |
| 71 | + 'VITE_', |
| 72 | + 'OPENAI_LIKE_API_BASE_URL', |
| 73 | + 'OPENAI_LIKE_API_MODELS', |
| 74 | + 'OLLAMA_API_BASE_URL', |
| 75 | + 'LMSTUDIO_API_BASE_URL', |
| 76 | + 'TOGETHER_API_BASE_URL', |
| 77 | + ], |
| 78 | + css: { |
| 79 | + preprocessorOptions: { |
| 80 | + scss: { |
| 81 | + api: 'modern-compiler', |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + test: { |
| 86 | + exclude: [ |
| 87 | + '**/node_modules/**', |
| 88 | + '**/dist/**', |
| 89 | + '**/cypress/**', |
| 90 | + '**/.{idea,git,cache,output,temp}/**', |
| 91 | + '**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*', |
| 92 | + '**/tests/preview/**', // Exclude preview tests that require Playwright |
| 93 | + ], |
| 94 | + }, |
| 95 | + }; |
| 96 | +}); |
| 97 | + |
| 98 | +function chrome129IssuePlugin() { |
| 99 | + return { |
| 100 | + name: 'chrome129IssuePlugin', |
| 101 | + configureServer(server: ViteDevServer) { |
| 102 | + server.middlewares.use((req, res, next) => { |
| 103 | + const raw = req.headers['user-agent']?.match(/Chrom(e|ium)\/([0-9]+)\./); |
| 104 | + |
| 105 | + if (raw) { |
| 106 | + const version = parseInt(raw[2], 10); |
| 107 | + |
| 108 | + if (version === 129) { |
| 109 | + res.setHeader('content-type', 'text/html'); |
| 110 | + res.end( |
| 111 | + '<body><h1>Please use Chrome Canary for testing.</h1><p>Chrome 129 has an issue with JavaScript modules & Vite local development, see <a href="https://github.com/stackblitz/bolt.new/issues/86#issuecomment-2395519258">for more information.</a></p><p><b>Note:</b> This only impacts <u>local development</u>. `pnpm run build` and `pnpm run start` will work fine in this browser.</p></body>', |
| 112 | + ); |
| 113 | + |
| 114 | + return; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + next(); |
| 119 | + }); |
| 120 | + }, |
| 121 | + }; |
| 122 | +} |
0 commit comments