|
1 |
| -import { defineConfig } from 'vite'; |
| 1 | +import { defineConfig, loadEnv } from 'vite'; |
2 | 2 | import react from '@vitejs/plugin-react';
|
3 | 3 | import path from 'path';
|
4 | 4 | import type { Plugin } from "vite";
|
5 | 5 |
|
6 | 6 | // https://vitejs.dev/config/
|
7 | 7 | export default defineConfig({
|
8 |
| - server: { |
9 |
| - host: 'localhost', |
10 |
| - port: 3090, |
11 |
| - strictPort: false, |
12 |
| - proxy: { |
13 |
| - '/api': { |
14 |
| - target: 'http://localhost:3080', |
15 |
| - changeOrigin: true |
16 |
| - }, |
17 |
| - '/auth': { |
18 |
| - target: 'http://localhost:3080', |
19 |
| - changeOrigin: true |
20 |
| - }, |
21 |
| - '/oauth': { |
22 |
| - target: 'http://localhost:3080', |
23 |
| - changeOrigin: true |
24 |
| - } |
25 |
| - } |
26 |
| - }, |
27 |
| - plugins: [react(), sourcemapExclude({excludeNodeModules: true})], |
28 |
| - publicDir: './public', |
29 |
| - build: { |
30 |
| - sourcemap: true, |
31 |
| - outDir: './dist', |
32 |
| - rollupOptions: { |
33 |
| - output: { |
34 |
| - manualChunks: id => { |
35 |
| - if (id.includes('node_modules')) { |
36 |
| - return 'vendor'; |
37 |
| - } |
| 8 | + server: { |
| 9 | + host: 'localhost', |
| 10 | + port: 3090, |
| 11 | + strictPort: false, |
| 12 | + proxy: { |
| 13 | + '/api': { |
| 14 | + target: 'http://localhost:3080', |
| 15 | + changeOrigin: true |
| 16 | + }, |
| 17 | + '/auth': { |
| 18 | + target: 'http://localhost:3080', |
| 19 | + changeOrigin: true |
| 20 | + }, |
| 21 | + '/oauth': { |
| 22 | + target: 'http://localhost:3080', |
| 23 | + changeOrigin: true |
| 24 | + } |
| 25 | + } |
| 26 | + }, |
| 27 | + plugins: [react(), sourcemapExclude({ excludeNodeModules: true })], |
| 28 | + publicDir: './public', |
| 29 | + build: { |
| 30 | + sourcemap: true, |
| 31 | + outDir: './dist', |
| 32 | + rollupOptions: { |
| 33 | + output: { |
| 34 | + manualChunks: id => { |
| 35 | + if (id.includes('node_modules')) { |
| 36 | + return 'vendor'; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + }, |
| 42 | + resolve: { |
| 43 | + alias: { |
| 44 | + '~': path.join(__dirname, 'src/') |
38 | 45 | }
|
39 |
| - } |
40 |
| - } |
41 |
| - }, |
42 |
| - resolve: { |
43 |
| - alias: { |
44 |
| - '~': path.join(__dirname, 'src/') |
45 | 46 | }
|
46 |
| - } |
47 | 47 | });
|
48 | 48 |
|
49 | 49 | interface SourcemapExclude {
|
50 |
| - excludeNodeModules?: boolean; |
| 50 | + excludeNodeModules?: boolean; |
51 | 51 | }
|
52 | 52 | export function sourcemapExclude(opts?: SourcemapExclude): Plugin {
|
53 |
| - return { |
54 |
| - name: "sourcemap-exclude", |
55 |
| - transform(code: string, id: string) { |
56 |
| - if (opts?.excludeNodeModules && id.includes("node_modules")) { |
57 |
| - return { |
58 |
| - code, |
59 |
| - // https://github.com/rollup/rollup/blob/master/docs/plugin-development/index.md#source-code-transformations |
60 |
| - map: { mappings: "" }, |
61 |
| - }; |
62 |
| - } |
63 |
| - }, |
64 |
| - }; |
| 53 | + return { |
| 54 | + name: "sourcemap-exclude", |
| 55 | + transform(code: string, id: string) { |
| 56 | + if (opts?.excludeNodeModules && id.includes("node_modules")) { |
| 57 | + return { |
| 58 | + code, |
| 59 | + // https://github.com/rollup/rollup/blob/master/docs/plugin-development/index.md#source-code-transformations |
| 60 | + map: { mappings: "" }, |
| 61 | + }; |
| 62 | + } |
| 63 | + }, |
| 64 | + }; |
65 | 65 | }
|
66 | 66 |
|
| 67 | +function htmlPlugin(env: ReturnType<typeof loadEnv>) { |
| 68 | + return { |
| 69 | + name: "html-transform", |
| 70 | + transformIndexHtml: { |
| 71 | + enforce: "pre" as const, |
| 72 | + transform: (html: string): string => |
| 73 | + html.replace(/%(.*?)%/g, (match, p1) => env[p1] ?? match), |
| 74 | + }, |
| 75 | + }; |
| 76 | +} |
0 commit comments