|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { mkdtemp, rm, writeFile } from 'node:fs/promises'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { test } from 'node:test'; |
| 6 | +import { build } from 'vite'; |
| 7 | +import { generateRenderEntryCode } from '../dist/render-entry-code.js'; |
| 8 | + |
| 9 | +const cases = [ |
| 10 | + ['Windows path', String.raw`C:\Users\test\rendiv\src\index.tsx`, 'C:/Users/test/rendiv/src/index.tsx'], |
| 11 | + ['numeric Windows directory', String.raw`D:\videos\2025\src\index.tsx`, 'D:/videos/2025/src/index.tsx'], |
| 12 | + ['root-relative Windows path', String.raw`\videos\2025\src\index.tsx`, '/videos/2025/src/index.tsx'], |
| 13 | + ['Windows path with spaces and apostrophe', String.raw`C:\Users\O'Brien\My Videos\src\index.tsx`, "C:/Users/O'Brien/My Videos/src/index.tsx"], |
| 14 | + ['POSIX path', '/home/user/My Videos/src/index.tsx', '/home/user/My Videos/src/index.tsx'], |
| 15 | + ['POSIX path with quotes', `/home/user/O'Brien/"video"/src/index.tsx`, `/home/user/O'Brien/"video"/src/index.tsx`], |
| 16 | +]; |
| 17 | + |
| 18 | +for (const [name, entryPoint, expectedImport] of cases) { |
| 19 | + test(`render entry preserves ${name}`, async () => { |
| 20 | + const imports = []; |
| 21 | + const virtualEntry = '\0rendiv-render-entry-test'; |
| 22 | + |
| 23 | + await build({ |
| 24 | + configFile: false, |
| 25 | + logLevel: 'silent', |
| 26 | + plugins: [{ |
| 27 | + name: 'render-entry-test', |
| 28 | + resolveId(source) { |
| 29 | + if (source === virtualEntry) return source; |
| 30 | + imports.push(source); |
| 31 | + return { id: source, external: true }; |
| 32 | + }, |
| 33 | + load(id) { |
| 34 | + if (id === virtualEntry) return generateRenderEntryCode(entryPoint); |
| 35 | + }, |
| 36 | + }], |
| 37 | + build: { |
| 38 | + write: false, |
| 39 | + minify: false, |
| 40 | + rollupOptions: { input: virtualEntry }, |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + assert.ok(imports.includes(expectedImport), `Expected ${JSON.stringify(expectedImport)} in ${JSON.stringify(imports)}`); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +test('render entry resolves POSIX filenames containing literal backslashes', { skip: process.platform === 'win32' }, async (t) => { |
| 49 | + const directory = await mkdtemp(path.join(tmpdir(), 'rendiv-entry-test-')); |
| 50 | + t.after(() => rm(directory, { recursive: true, force: true })); |
| 51 | + const entryPoint = path.join(directory, String.raw`video\2025.js`); |
| 52 | + await writeFile(entryPoint, 'globalThis.__RENDIV_PATH_TEST__ = true;'); |
| 53 | + const virtualEntry = '\0rendiv-render-entry-test'; |
| 54 | + |
| 55 | + const result = await build({ |
| 56 | + configFile: false, |
| 57 | + logLevel: 'silent', |
| 58 | + plugins: [{ |
| 59 | + name: 'render-entry-test', |
| 60 | + resolveId(source) { |
| 61 | + if (source === virtualEntry) return source; |
| 62 | + }, |
| 63 | + load(id) { |
| 64 | + if (id === virtualEntry) return generateRenderEntryCode(entryPoint); |
| 65 | + }, |
| 66 | + }], |
| 67 | + build: { |
| 68 | + write: false, |
| 69 | + minify: false, |
| 70 | + rollupOptions: { |
| 71 | + input: virtualEntry, |
| 72 | + external: ['@rendiv/core', 'react', 'react-dom/client', 'react-dom'], |
| 73 | + }, |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + assert.ok(result.output.some((output) => output.type === 'chunk' && output.code.includes('__RENDIV_PATH_TEST__'))); |
| 78 | +}); |
0 commit comments