Skip to content

Commit 6d6aca2

Browse files
authored
Merge pull request #12 from thecodacus/fix/windows-render-entry-path
fix: handle Windows paths in render entry imports
2 parents ce54ace + 39433db commit 6d6aca2

3 files changed

Lines changed: 89 additions & 3 deletions

File tree

‎packages/bundler/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"scripts": {
1818
"build": "tsc",
1919
"dev": "tsc --watch",
20+
"test": "node --test test/*.test.mjs",
2021
"typecheck": "tsc --noEmit",
2122
"clean": "rm -rf dist"
2223
},

‎packages/bundler/src/render-entry-code.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'node:path';
2+
13
/**
24
* Generates the JavaScript code for the render entry point.
35
* This code is injected into the bundle and bridges the headless browser
@@ -7,16 +9,21 @@ export function generateRenderEntryCode(
79
userEntryPoint: string,
810
overrides?: Record<string, unknown>,
911
): string {
10-
// Use the absolute path directly for Vite to resolve
11-
const importPath = userEntryPoint;
12+
// Normalize Windows separators while preserving literal backslashes in POSIX
13+
// filenames. Serialize the specifier to keep special characters valid in JS.
14+
const isWindowsPath = path.win32.isAbsolute(userEntryPoint)
15+
&& (process.platform === 'win32' || !path.posix.isAbsolute(userEntryPoint));
16+
const importPath = JSON.stringify(isWindowsPath
17+
? userEntryPoint.replace(/\\/g, '/')
18+
: userEntryPoint);
1219

1320
// Serialize overrides so Sequence components can read them at runtime
1421
const overridesSnippet = overrides && Object.keys(overrides).length > 0
1522
? `window.__RENDIV_TIMELINE_OVERRIDES__ = new Map(Object.entries(${JSON.stringify(overrides)}));`
1623
: '';
1724

1825
return `
19-
import '${importPath}';
26+
import ${importPath};
2027
import { getRootComponent, CompositionManagerContext, TimelineContext, CompositionContext, RendivEnvironmentContext, getPendingHoldCount } from '@rendiv/core';
2128
import React, { useState, useCallback, useMemo, useEffect } from 'react';
2229
import { createRoot } from 'react-dom/client';
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)