|
1 | | -import { defineConfig } from 'vite'; |
| 1 | +import { defineConfig, loadEnv } from 'vite'; |
2 | 2 | import react from '@vitejs/plugin-react'; |
3 | 3 |
|
4 | | -export default defineConfig({ |
5 | | - plugins: [react()], |
6 | | - resolve: { |
7 | | - extensions: ['.ts', '.js', '.mjs', '.json', '.tsx'], |
8 | | - }, |
9 | | - esbuild: { |
10 | | - loader: 'tsx', |
11 | | - include: /\.(ts|tsx|js|mjs)$/, |
12 | | - }, |
| 4 | +export default defineConfig(({ command, mode }) => { |
| 5 | + const env = loadEnv(mode, process.cwd(), ''); |
| 6 | + const cdnURL = env.VITE_CDN_URL; |
| 7 | + |
| 8 | + return { |
| 9 | + plugins: [react()], |
| 10 | + resolve: { |
| 11 | + extensions: ['.ts', '.js', '.mjs', '.json', '.tsx'], |
| 12 | + }, |
| 13 | + esbuild: { |
| 14 | + loader: 'tsx', |
| 15 | + include: /\.(ts|tsx|js|mjs)$/, |
| 16 | + }, |
| 17 | + /** |
| 18 | + * https://vite.dev/guide/build.html#advanced-base-options |
| 19 | + */ |
| 20 | + experimental: { |
| 21 | + renderBuiltUrl(filename: string | URL) { |
| 22 | + // We only want to prepend the CDN URL during the 'build' command |
| 23 | + // and only if the VITE_CDN_URL is set. |
| 24 | + if (command === 'build' && cdnURL) { |
| 25 | + // hostType can be 'js', 'css', or 'asset' |
| 26 | + return new URL(filename, cdnURL).href; |
| 27 | + } else { |
| 28 | + // Otherwise, (e.g., in 'serve' mode), use relative paths. |
| 29 | + return { relative: true }; |
| 30 | + } |
| 31 | + }, |
| 32 | + }, |
| 33 | + }; |
13 | 34 | }); |
0 commit comments