|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { rolldownDepPlugin } from '../../optimizer/rolldownDepPlugin' |
| 3 | +import { normalizePath } from '../../utils' |
| 4 | + |
| 5 | +async function createRolldownDepPluginTransform(cacheDir: string) { |
| 6 | + const baseConfig = { |
| 7 | + cacheDir: normalizePath(cacheDir), |
| 8 | + optimizeDeps: { extensions: [] }, |
| 9 | + server: { fs: { allow: [] } }, |
| 10 | + resolve: { builtins: [] }, |
| 11 | + createResolver: () => ({}), |
| 12 | + } |
| 13 | + |
| 14 | + const mockEnvironment = { |
| 15 | + config: baseConfig, |
| 16 | + getTopLevelConfig: () => baseConfig, |
| 17 | + } as any |
| 18 | + |
| 19 | + const plugins = rolldownDepPlugin(mockEnvironment, {}, []) |
| 20 | + const plugin = plugins.find( |
| 21 | + (p: any) => p.name === 'vite:dep-pre-bundle', |
| 22 | + ) as any |
| 23 | + |
| 24 | + if (!plugin || !plugin.transform) { |
| 25 | + throw new Error('Could not find vite:dep-pre-bundle plugin') |
| 26 | + } |
| 27 | + |
| 28 | + const handler = plugin.transform.handler |
| 29 | + |
| 30 | + return async (code: string, id: string) => { |
| 31 | + const result = await handler.call({}, code, normalizePath(id)) |
| 32 | + return result?.code || result |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +describe('rolldownDepPlugin transform', async () => { |
| 37 | + const transform = await createRolldownDepPluginTransform('/root/.vite') |
| 38 | + |
| 39 | + test('rewrite various relative asset formats', async () => { |
| 40 | + const code = ` |
| 41 | + const img = new URL('./logo.png', import.meta.url).href |
| 42 | + const icon = new URL('./icons/search.svg', import.meta.url) |
| 43 | + const worker = new URL('./worker.js', import.meta.url) |
| 44 | + const wasm = new URL('./module.wasm', import.meta.url) |
| 45 | + ` |
| 46 | + expect(await transform(code, '/root/node_modules/my-lib/dist/index.js')) |
| 47 | + .toMatchInlineSnapshot(` |
| 48 | + " |
| 49 | + const img = new URL('' + "../../node_modules/my-lib/dist/logo.png", import.meta.url).href |
| 50 | + const icon = new URL('' + "../../node_modules/my-lib/dist/icons/search.svg", import.meta.url) |
| 51 | + const worker = new URL('' + "../../node_modules/my-lib/dist/worker.js", import.meta.url) |
| 52 | + const wasm = new URL('' + "../../node_modules/my-lib/dist/module.wasm", import.meta.url) |
| 53 | + " |
| 54 | + `) |
| 55 | + }) |
| 56 | + |
| 57 | + test('respects /* @vite-ignore */', async () => { |
| 58 | + expect( |
| 59 | + await transform( |
| 60 | + "new URL(/* @vite-ignore */ './worker.js', import.meta.url)", |
| 61 | + '/root/node_modules/my-lib/index.js', |
| 62 | + ), |
| 63 | + ).toBeUndefined() |
| 64 | + }) |
| 65 | + |
| 66 | + test('skips non-relative URLs (absolute, data, protocols)', async () => { |
| 67 | + const code = ` |
| 68 | + new URL('/absolute/path.png', import.meta.url) |
| 69 | + new URL('https://example.com/worker.js', import.meta.url) |
| 70 | + new URL('data:text/javascript;base64,Y29uc29sZS5sb2coMSk=', import.meta.url) |
| 71 | + ` |
| 72 | + expect( |
| 73 | + await transform(code, '/root/node_modules/my-lib/index.js'), |
| 74 | + ).toBeUndefined() |
| 75 | + }) |
| 76 | + |
| 77 | + test('skips dynamic template strings', async () => { |
| 78 | + expect( |
| 79 | + await transform( |
| 80 | + 'new URL(`./${name}.js`, import.meta.url)', |
| 81 | + '/root/node_modules/my-lib/index.js', |
| 82 | + ), |
| 83 | + ).toBeUndefined() |
| 84 | + }) |
| 85 | + |
| 86 | + test('handles backticks for static relative strings', async () => { |
| 87 | + expect( |
| 88 | + await transform( |
| 89 | + 'new URL(`./static.js`, import.meta.url)', |
| 90 | + '/root/node_modules/my-lib/index.js', |
| 91 | + ), |
| 92 | + ).toMatchInlineSnapshot( |
| 93 | + `"new URL('' + "../../node_modules/my-lib/static.js", import.meta.url)"`, |
| 94 | + ) |
| 95 | + }) |
| 96 | + |
| 97 | + test('handles assets with query parameters and hashes', async () => { |
| 98 | + const code = ` |
| 99 | + const url1 = new URL('./style.css?raw', import.meta.url) |
| 100 | + const url2 = new URL('./data.json#config', import.meta.url) |
| 101 | + ` |
| 102 | + expect(await transform(code, '/root/node_modules/my-lib/index.js')) |
| 103 | + .toMatchInlineSnapshot(` |
| 104 | + " |
| 105 | + const url1 = new URL('' + "../../node_modules/my-lib/style.css?raw", import.meta.url) |
| 106 | + const url2 = new URL('' + "../../node_modules/my-lib/data.json#config", import.meta.url) |
| 107 | + " |
| 108 | + `) |
| 109 | + }) |
| 110 | + |
| 111 | + test('handles deeply nested relative paths', async () => { |
| 112 | + expect( |
| 113 | + await transform( |
| 114 | + "new URL('../../../assets/file.js', import.meta.url)", |
| 115 | + '/root/node_modules/my-lib/dist/deep/folder/index.js', |
| 116 | + ), |
| 117 | + ).toMatchInlineSnapshot( |
| 118 | + `"new URL('' + "../../node_modules/my-lib/assets/file.js", import.meta.url)"`, |
| 119 | + ) |
| 120 | + }) |
| 121 | + |
| 122 | + test('rewrite relative URLs even if id is not in node_modules', async () => { |
| 123 | + expect( |
| 124 | + await transform( |
| 125 | + "new URL('./asset.js', import.meta.url)", |
| 126 | + '/root/src/local-dep.js', |
| 127 | + ), |
| 128 | + ).toMatchInlineSnapshot( |
| 129 | + `"new URL('' + "../../src/asset.js", import.meta.url)"`, |
| 130 | + ) |
| 131 | + }) |
| 132 | +}) |
0 commit comments