|
1 | 1 | import { resolve } from 'pathe'
|
| 2 | +import swc from 'unplugin-swc' |
2 | 3 | import { defineConfig } from 'vitest/config'
|
3 | 4 | import vue from '@vitejs/plugin-vue'
|
4 | 5 | import MagicString from 'magic-string'
|
5 | 6 | import remapping from '@ampproject/remapping'
|
| 7 | +import type { Plugin } from 'vite' |
6 | 8 |
|
7 | 9 | const provider = process.argv[1 + process.argv.indexOf('--provider')]
|
8 | 10 |
|
9 |
| -export default defineConfig(() => ({ |
| 11 | +export default defineConfig(_ => ({ |
10 | 12 | plugins: [
|
11 | 13 | vue(),
|
12 |
| - /* |
13 |
| - * Transforms `multi-environment.ts` differently based on test environment (JSDOM/Node) |
14 |
| - * so that there are multiple different source maps for a single file. |
15 |
| - * This causes a case where coverage report is incorrect if sourcemaps are not picked based on transform mode. |
16 |
| - */ |
17 |
| - { |
18 |
| - name: 'vitest-custom-multi-transform', |
19 |
| - enforce: 'pre', |
20 |
| - transform(code, id, options) { |
21 |
| - if (id.includes('src/multi-environment')) { |
22 |
| - const ssr = options?.ssr || false |
23 |
| - const transforMode = `transformMode is ${ssr ? 'ssr' : 'csr'}` |
24 |
| - const padding = '\n*****'.repeat(ssr ? 0 : 15) |
25 |
| - |
26 |
| - const transformed = new MagicString(code) |
27 |
| - transformed.replace('\'default-padding\'', `\`${transforMode} ${padding}\``) |
28 |
| - |
29 |
| - const map = remapping( |
30 |
| - [transformed.generateMap({ hires: true }), this.getCombinedSourcemap() as any], |
31 |
| - () => null, |
32 |
| - ) as any |
33 |
| - |
34 |
| - return { code: transformed.toString(), map } |
35 |
| - } |
36 |
| - }, |
37 |
| - }, |
38 |
| - { |
39 |
| - // Simulates Vite's virtual files: https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention |
40 |
| - name: 'vitest-custom-virtual-files', |
41 |
| - resolveId(id) { |
42 |
| - if (id === 'virtual:vitest-custom-virtual-file-1') |
43 |
| - return 'src/virtual:vitest-custom-virtual-file-1.ts' |
44 |
| - |
45 |
| - if (id === '\0vitest-custom-virtual-file-2') |
46 |
| - return 'src/\0vitest-custom-virtual-file-2.ts' |
47 |
| - }, |
48 |
| - load(id) { |
49 |
| - if (id === 'src/virtual:vitest-custom-virtual-file-1.ts') { |
50 |
| - return ` |
51 |
| - const virtualFile = "This file should be excluded from coverage report #1" |
52 |
| - export default virtualFile; |
53 |
| - ` |
54 |
| - } |
55 |
| - |
56 |
| - // Vitest browser resolves this as "\x00", Node as "__x00__" |
57 |
| - if (id === 'src/__x00__vitest-custom-virtual-file-2.ts' || id === 'src/\x00vitest-custom-virtual-file-2.ts') { |
58 |
| - return ` |
59 |
| - const virtualFile = "This file should be excluded from coverage report #2" |
60 |
| - export default virtualFile; |
61 |
| - ` |
62 |
| - } |
63 |
| - }, |
64 |
| - }, |
| 14 | + MultiTransformPlugin(), |
| 15 | + VirtualFilesPlugin(), |
| 16 | + DecoratorsPlugin(), |
65 | 17 | ],
|
66 | 18 | define: {
|
67 | 19 | MY_CONSTANT: '"my constant"',
|
@@ -105,3 +57,88 @@ export default defineConfig(() => ({
|
105 | 57 | ],
|
106 | 58 | },
|
107 | 59 | }))
|
| 60 | + |
| 61 | +/* |
| 62 | + * Transforms `multi-environment.ts` differently based on test environment (JSDOM/Node) |
| 63 | + * so that there are multiple different source maps for a single file. |
| 64 | + * This causes a case where coverage report is incorrect if sourcemaps are not picked based on transform mode. |
| 65 | + */ |
| 66 | +function MultiTransformPlugin(): Plugin { |
| 67 | + return { |
| 68 | + name: 'vitest-custom-multi-transform', |
| 69 | + enforce: 'pre', |
| 70 | + transform(code, id, options) { |
| 71 | + if (id.includes('src/multi-environment')) { |
| 72 | + const ssr = options?.ssr || false |
| 73 | + const transforMode = `transformMode is ${ssr ? 'ssr' : 'csr'}` |
| 74 | + const padding = '\n*****'.repeat(ssr ? 0 : 15) |
| 75 | + |
| 76 | + const transformed = new MagicString(code) |
| 77 | + transformed.replace('\'default-padding\'', `\`${transforMode} ${padding}\``) |
| 78 | + |
| 79 | + const map = remapping( |
| 80 | + [transformed.generateMap({ hires: true }), this.getCombinedSourcemap() as any], |
| 81 | + () => null, |
| 82 | + ) as any |
| 83 | + |
| 84 | + return { code: transformed.toString(), map } |
| 85 | + } |
| 86 | + }, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// Simulates Vite's virtual files: https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention |
| 91 | +function VirtualFilesPlugin(): Plugin { |
| 92 | + return { |
| 93 | + name: 'vitest-custom-virtual-files', |
| 94 | + resolveId(id) { |
| 95 | + if (id === 'virtual:vitest-custom-virtual-file-1') |
| 96 | + return 'src/virtual:vitest-custom-virtual-file-1.ts' |
| 97 | + |
| 98 | + if (id === '\0vitest-custom-virtual-file-2') |
| 99 | + return 'src/\0vitest-custom-virtual-file-2.ts' |
| 100 | + }, |
| 101 | + load(id) { |
| 102 | + if (id === 'src/virtual:vitest-custom-virtual-file-1.ts') { |
| 103 | + return ` |
| 104 | + const virtualFile = "This file should be excluded from coverage report #1" |
| 105 | + export default virtualFile; |
| 106 | + ` |
| 107 | + } |
| 108 | + |
| 109 | + // Vitest browser resolves this as "\x00", Node as "__x00__" |
| 110 | + if (id === 'src/__x00__vitest-custom-virtual-file-2.ts' || id === 'src/\x00vitest-custom-virtual-file-2.ts') { |
| 111 | + return ` |
| 112 | + const virtualFile = "This file should be excluded from coverage report #2" |
| 113 | + export default virtualFile; |
| 114 | + ` |
| 115 | + } |
| 116 | + }, |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +function DecoratorsPlugin(): Plugin { |
| 121 | + const plugin = swc.vite({ |
| 122 | + jsc: { |
| 123 | + target: 'esnext', |
| 124 | + parser: { |
| 125 | + syntax: 'typescript', |
| 126 | + decorators: true, |
| 127 | + }, |
| 128 | + transform: { |
| 129 | + legacyDecorator: true, |
| 130 | + decoratorMetadata: true, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }) |
| 134 | + |
| 135 | + return { |
| 136 | + name: 'custom-swc-decorator', |
| 137 | + enforce: 'pre', |
| 138 | + transform(code, id, options) { |
| 139 | + if (id.endsWith('decorators.ts')) |
| 140 | + // @ts-expect-error -- Ignore complex type |
| 141 | + return plugin.transform(code, id, options) |
| 142 | + }, |
| 143 | + } |
| 144 | +} |
0 commit comments