|
| 1 | +import path from 'node:path' |
| 2 | +import type { ExportSpecifier, ImportSpecifier } from 'es-module-lexer' |
| 3 | +import { build } from 'vite' |
| 4 | +import { describe, expect, it, vi } from 'vitest' |
| 5 | +import type { RscPluginManager } from '../plugin' |
| 6 | +import { scanBuildStripPlugin } from './scan' |
| 7 | + |
| 8 | +const root = path.join(import.meta.dirname, 'fixtures/scan-observer') |
| 9 | + |
| 10 | +describe(scanBuildStripPlugin, () => { |
| 11 | + it('does not notify observers outside scan builds', async () => { |
| 12 | + const observer = vi.fn() |
| 13 | + const manager = { |
| 14 | + isScanBuild: false, |
| 15 | + scanBuildObservers: new Set([observer]), |
| 16 | + } as unknown as RscPluginManager |
| 17 | + |
| 18 | + await build({ |
| 19 | + root, |
| 20 | + logLevel: 'silent', |
| 21 | + build: { |
| 22 | + write: false, |
| 23 | + rollupOptions: { input: path.join(root, 'entry.js') }, |
| 24 | + }, |
| 25 | + plugins: [scanBuildStripPlugin({ manager })], |
| 26 | + }) |
| 27 | + |
| 28 | + expect(observer).not.toHaveBeenCalled() |
| 29 | + }) |
| 30 | + |
| 31 | + it('emits ordered reset and module events with raw scan metadata', async () => { |
| 32 | + const observer = vi.fn() |
| 33 | + const secondObserver = vi.fn() |
| 34 | + const manager = { |
| 35 | + isScanBuild: true, |
| 36 | + scanBuildObservers: new Set([observer, secondObserver]), |
| 37 | + } as unknown as RscPluginManager |
| 38 | + |
| 39 | + await build({ |
| 40 | + root, |
| 41 | + logLevel: 'silent', |
| 42 | + build: { |
| 43 | + write: false, |
| 44 | + rollupOptions: { input: path.join(root, 'entry.js') }, |
| 45 | + }, |
| 46 | + plugins: [scanBuildStripPlugin({ manager })], |
| 47 | + }) |
| 48 | + |
| 49 | + expect(secondObserver.mock.calls).toEqual(observer.mock.calls) |
| 50 | + expect(observer.mock.calls[0]![0]).toEqual({ |
| 51 | + type: 'reset', |
| 52 | + environmentName: 'client', |
| 53 | + }) |
| 54 | + |
| 55 | + const moduleEvents = observer.mock.calls |
| 56 | + .map(([event]) => event) |
| 57 | + .filter((event) => event.type === 'module') |
| 58 | + expect(moduleEvents).toHaveLength(2) |
| 59 | + |
| 60 | + const entryEvent = moduleEvents.find((event) => |
| 61 | + event.info.id.endsWith('/entry.js'), |
| 62 | + ) |
| 63 | + expect(entryEvent).toMatchObject({ |
| 64 | + type: 'module', |
| 65 | + environmentName: 'client', |
| 66 | + code: expect.stringContaining("import { action } from './actions.js'"), |
| 67 | + info: { |
| 68 | + id: expect.stringMatching(/\/entry\.js$/), |
| 69 | + importedIds: [expect.stringMatching(/\/actions\.js$/)], |
| 70 | + }, |
| 71 | + }) |
| 72 | + expect(entryEvent.imports.map((item: ImportSpecifier) => item.n)).toEqual([ |
| 73 | + './actions.js', |
| 74 | + './actions.js', |
| 75 | + ]) |
| 76 | + expect(entryEvent.exports.map((item: ExportSpecifier) => item.n)).toEqual([ |
| 77 | + 'submit', |
| 78 | + ]) |
| 79 | + |
| 80 | + const actionsEvent = moduleEvents.find((event) => |
| 81 | + event.info.id.endsWith('/actions.js'), |
| 82 | + ) |
| 83 | + expect(actionsEvent).toMatchObject({ |
| 84 | + type: 'module', |
| 85 | + environmentName: 'client', |
| 86 | + code: 'export const action = 1\n', |
| 87 | + imports: [], |
| 88 | + exports: [expect.objectContaining({ n: 'action' })], |
| 89 | + info: { |
| 90 | + id: expect.stringMatching(/\/actions\.js$/), |
| 91 | + importedIds: [], |
| 92 | + }, |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + it('deduplicates module events across shared scan plugins', async () => { |
| 97 | + const observer = vi.fn() |
| 98 | + const manager = { |
| 99 | + isScanBuild: true, |
| 100 | + scanBuildObservers: new Set([observer]), |
| 101 | + } as unknown as RscPluginManager |
| 102 | + |
| 103 | + await build({ |
| 104 | + root, |
| 105 | + logLevel: 'silent', |
| 106 | + build: { |
| 107 | + write: false, |
| 108 | + rollupOptions: { input: path.join(root, 'entry.js') }, |
| 109 | + }, |
| 110 | + plugins: [ |
| 111 | + scanBuildStripPlugin({ manager }), |
| 112 | + scanBuildStripPlugin({ manager }), |
| 113 | + ], |
| 114 | + }) |
| 115 | + |
| 116 | + const moduleIds = observer.mock.calls |
| 117 | + .map(([event]) => event) |
| 118 | + .filter((event) => event.type === 'module') |
| 119 | + .map((event) => path.basename(event.info.id)) |
| 120 | + .sort() |
| 121 | + expect(moduleIds).toEqual(['actions.js', 'entry.js']) |
| 122 | + }) |
| 123 | +}) |
0 commit comments