|
| 1 | +import { afterAll, describe, expect, test, vi } from 'vitest' |
| 2 | +import { type InlineConfig, type Plugin, build, createServer } from '../..' |
| 3 | + |
| 4 | +const getConfigWithPlugin = ( |
| 5 | + plugins: Plugin[], |
| 6 | + input?: string[], |
| 7 | +): InlineConfig => { |
| 8 | + return { |
| 9 | + configFile: false, |
| 10 | + server: { middlewareMode: true, ws: false }, |
| 11 | + optimizeDeps: { noDiscovery: true, include: [] }, |
| 12 | + build: { rollupOptions: { input } }, |
| 13 | + plugins, |
| 14 | + logLevel: 'silent', |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +describe('hook filter with plugin container', async () => { |
| 19 | + const resolveId = vi.fn() |
| 20 | + const load = vi.fn() |
| 21 | + const transformWithId = vi.fn() |
| 22 | + const transformWithCode = vi.fn() |
| 23 | + const any = expect.toSatisfy(() => true) // anything including undefined and null |
| 24 | + const config = getConfigWithPlugin([ |
| 25 | + { |
| 26 | + name: 'test', |
| 27 | + resolveId: { |
| 28 | + filter: { id: '*.js' }, |
| 29 | + handler: resolveId, |
| 30 | + }, |
| 31 | + load: { |
| 32 | + filter: { id: '*.js' }, |
| 33 | + handler: load, |
| 34 | + }, |
| 35 | + transform: { |
| 36 | + filter: { id: '*.js' }, |
| 37 | + handler: transformWithId, |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: 'test2', |
| 42 | + transform: { |
| 43 | + filter: { code: 'import.meta' }, |
| 44 | + handler: transformWithCode, |
| 45 | + }, |
| 46 | + }, |
| 47 | + ]) |
| 48 | + const server = await createServer(config) |
| 49 | + afterAll(async () => { |
| 50 | + await server.close() |
| 51 | + }) |
| 52 | + const pluginContainer = server.environments.ssr.pluginContainer |
| 53 | + |
| 54 | + test('resolveId', async () => { |
| 55 | + await pluginContainer.resolveId('foo.js') |
| 56 | + await pluginContainer.resolveId('foo.ts') |
| 57 | + expect(resolveId).toHaveBeenCalledTimes(1) |
| 58 | + expect(resolveId).toHaveBeenCalledWith('foo.js', any, any) |
| 59 | + }) |
| 60 | + |
| 61 | + test('load', async () => { |
| 62 | + await pluginContainer.load('foo.js') |
| 63 | + await pluginContainer.load('foo.ts') |
| 64 | + expect(load).toHaveBeenCalledTimes(1) |
| 65 | + expect(load).toHaveBeenCalledWith('foo.js', any) |
| 66 | + }) |
| 67 | + |
| 68 | + test('transform', async () => { |
| 69 | + await server.environments.ssr.moduleGraph.ensureEntryFromUrl('foo.js') |
| 70 | + await server.environments.ssr.moduleGraph.ensureEntryFromUrl('foo.ts') |
| 71 | + |
| 72 | + await pluginContainer.transform('import_meta', 'foo.js') |
| 73 | + await pluginContainer.transform('import.meta', 'foo.ts') |
| 74 | + expect(transformWithId).toHaveBeenCalledTimes(1) |
| 75 | + expect(transformWithId).toHaveBeenCalledWith( |
| 76 | + expect.stringContaining('import_meta'), |
| 77 | + 'foo.js', |
| 78 | + any, |
| 79 | + ) |
| 80 | + expect(transformWithCode).toHaveBeenCalledTimes(1) |
| 81 | + expect(transformWithCode).toHaveBeenCalledWith( |
| 82 | + expect.stringContaining('import.meta'), |
| 83 | + 'foo.ts', |
| 84 | + any, |
| 85 | + ) |
| 86 | + }) |
| 87 | +}) |
| 88 | + |
| 89 | +describe('hook filter with build', async () => { |
| 90 | + const resolveId = vi.fn() |
| 91 | + const load = vi.fn() |
| 92 | + const transformWithId = vi.fn() |
| 93 | + const transformWithCode = vi.fn() |
| 94 | + const any = expect.anything() |
| 95 | + const config = getConfigWithPlugin( |
| 96 | + [ |
| 97 | + { |
| 98 | + name: 'test', |
| 99 | + resolveId: { |
| 100 | + filter: { id: '*.js' }, |
| 101 | + handler: resolveId, |
| 102 | + }, |
| 103 | + load: { |
| 104 | + filter: { id: '*.js' }, |
| 105 | + handler: load, |
| 106 | + }, |
| 107 | + transform: { |
| 108 | + filter: { id: '*.js' }, |
| 109 | + handler: transformWithId, |
| 110 | + }, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: 'test2', |
| 114 | + transform: { |
| 115 | + filter: { code: 'import.meta' }, |
| 116 | + handler: transformWithCode, |
| 117 | + }, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: 'resolver', |
| 121 | + resolveId(id) { |
| 122 | + return id |
| 123 | + }, |
| 124 | + load(id) { |
| 125 | + if (id === 'foo.js') { |
| 126 | + return 'import "foo.ts"\n' + 'import_meta' |
| 127 | + } |
| 128 | + if (id === 'foo.ts') { |
| 129 | + return 'import.meta' |
| 130 | + } |
| 131 | + }, |
| 132 | + }, |
| 133 | + ], |
| 134 | + ['foo.js', 'foo.ts'], |
| 135 | + ) |
| 136 | + await build(config) |
| 137 | + |
| 138 | + test('resolveId', async () => { |
| 139 | + expect(resolveId).toHaveBeenCalledTimes(1) |
| 140 | + expect(resolveId).toHaveBeenCalledWith('foo.js', undefined, any) |
| 141 | + }) |
| 142 | + |
| 143 | + test('load', async () => { |
| 144 | + expect(load).toHaveBeenCalledTimes(1) |
| 145 | + expect(load).toHaveBeenCalledWith('foo.js', any) |
| 146 | + }) |
| 147 | + |
| 148 | + test('transform', async () => { |
| 149 | + expect(transformWithId).toHaveBeenCalledTimes(1) |
| 150 | + expect(transformWithId).toHaveBeenCalledWith( |
| 151 | + expect.stringContaining('import_meta'), |
| 152 | + 'foo.js', |
| 153 | + any, |
| 154 | + ) |
| 155 | + expect(transformWithCode).toHaveBeenCalledTimes(1) |
| 156 | + expect(transformWithCode).toHaveBeenCalledWith( |
| 157 | + expect.stringContaining('import.meta'), |
| 158 | + 'foo.ts', |
| 159 | + any, |
| 160 | + ) |
| 161 | + }) |
| 162 | +}) |
0 commit comments