|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { removeDynamicImportsPlugin } from './bundle-vat.ts'; |
| 4 | + |
| 5 | +type TransformFn = ( |
| 6 | + code: string, |
| 7 | + id: string, |
| 8 | +) => { code: string; map: null } | null; |
| 9 | + |
| 10 | +describe('removeDynamicImportsPlugin', () => { |
| 11 | + const plugin = removeDynamicImportsPlugin(); |
| 12 | + const transform = (plugin.transform as TransformFn).bind({ |
| 13 | + warn: vi.fn(), |
| 14 | + }); |
| 15 | + |
| 16 | + it.each([ |
| 17 | + ['single-quoted import', `import('module')`, `Promise.resolve({})`], |
| 18 | + ['double-quoted import', `import("module")`, `Promise.resolve({})`], |
| 19 | + ['backtick-quoted import', 'import(`module`)', `Promise.resolve({})`], |
| 20 | + [ |
| 21 | + 'import with whitespace before paren', |
| 22 | + `import ('module')`, |
| 23 | + `Promise.resolve({})`, |
| 24 | + ], |
| 25 | + [ |
| 26 | + 'import with whitespace around specifier', |
| 27 | + `import( 'module' )`, |
| 28 | + `Promise.resolve({})`, |
| 29 | + ], |
| 30 | + [ |
| 31 | + 'multiple dynamic imports in one file', |
| 32 | + `const a = import('foo');\nconst b = import("bar");`, |
| 33 | + `const a = Promise.resolve({});\nconst b = Promise.resolve({});`, |
| 34 | + ], |
| 35 | + [ |
| 36 | + 'dynamic import surrounded by other code', |
| 37 | + `const x = 1;\nawait import('lazy-util');\nconst y = 2;`, |
| 38 | + `const x = 1;\nawait Promise.resolve({});\nconst y = 2;`, |
| 39 | + ], |
| 40 | + ])('replaces %s', (_name, code, expected) => { |
| 41 | + const result = transform(code, 'test.ts'); |
| 42 | + expect(result).toStrictEqual({ code: expected, map: null }); |
| 43 | + }); |
| 44 | + |
| 45 | + it.each([ |
| 46 | + ['no import at all', 'const x = 1;'], |
| 47 | + ['ESM import statement', `import foo from 'bar';`], |
| 48 | + ['ESM named import', `import { foo } from 'bar';`], |
| 49 | + ['empty code', ''], |
| 50 | + ])('returns null for %s (fast-path miss)', (_name, code) => { |
| 51 | + expect(transform(code, 'test.ts')).toBeNull(); |
| 52 | + }); |
| 53 | + |
| 54 | + it.each([ |
| 55 | + ['computed specifier (variable)', `import(variable)`], |
| 56 | + ['computed specifier (expression)', `import(getPath())`], |
| 57 | + ['computed specifier (concatenation)', `import('./locales/' + lang)`], |
| 58 | + ])('returns null for %s (no literal match)', (_name, code) => { |
| 59 | + expect(transform(code, 'test.ts')).toBeNull(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('does not match reimport or similar substrings', () => { |
| 63 | + const code = `reimport('foo')`; |
| 64 | + expect(transform(code, 'test.ts')).toBeNull(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('warns when fast-path matches but replacement regex does not', () => { |
| 68 | + const warn = vi.fn(); |
| 69 | + const boundTransform = (plugin.transform as TransformFn).bind({ |
| 70 | + warn, |
| 71 | + }); |
| 72 | + |
| 73 | + boundTransform(`import(variable)`, 'test.ts'); |
| 74 | + |
| 75 | + expect(warn).toHaveBeenCalledWith( |
| 76 | + expect.stringContaining('could not be replaced'), |
| 77 | + ); |
| 78 | + }); |
| 79 | +}); |
0 commit comments