Skip to content

Commit 5a6a20f

Browse files
fix: tdz and host path normalization.
1 parent 5e9021b commit 5a6a20f

3 files changed

Lines changed: 51 additions & 3 deletions

File tree

playwright/app.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,30 @@ test('react mode typecheck loads types without malformed URL fetches', async ({
376376
}
377377
})
378378

379+
test('react mode executes default React import without TDZ runtime failure', async ({
380+
page,
381+
}) => {
382+
await waitForInitialRender(page)
383+
384+
await ensurePanelToolsVisible(page, 'component')
385+
386+
await page.getByLabel('ShadowRoot (open)').uncheck()
387+
await page.locator('#render-mode').selectOption('react')
388+
await setComponentEditorSource(
389+
page,
390+
[
391+
"import React from 'react'",
392+
'const App = () => <button>react default import works</button>',
393+
].join('\n'),
394+
)
395+
396+
await expect(page.locator('#status')).toHaveText('Rendered')
397+
await expect(page.locator('#preview-host button')).toContainText(
398+
'react default import works',
399+
)
400+
await expect(page.locator('#preview-host pre')).toHaveCount(0)
401+
})
402+
379403
test('clearing component source reports clear action without error status', async ({
380404
page,
381405
}) => {

src/modules/render-runtime.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,17 @@ export const createRenderRuntimeController = ({
305305
const preamble = []
306306
const unsupportedSources = new Set()
307307
let requiresReactRuntime = false
308+
let hasReactRuntimeAlias = false
309+
310+
const ensureReactRuntimeAlias = () => {
311+
if (hasReactRuntimeAlias) {
312+
return '__knightedReactRuntime'
313+
}
314+
315+
hasReactRuntimeAlias = true
316+
preamble.push('const __knightedReactRuntime = React')
317+
return '__knightedReactRuntime'
318+
}
308319

309320
for (const entry of imports) {
310321
if (!entry || entry.importKind !== 'value') {
@@ -324,15 +335,28 @@ export const createRenderRuntimeController = ({
324335
}
325336

326337
if (binding.kind === 'default' || binding.kind === 'namespace') {
338+
if (binding.local === 'React') {
339+
continue
340+
}
341+
327342
preamble.push(`const ${binding.local} = React`)
328343
continue
329344
}
330345

331346
if (binding.kind === 'named') {
332347
if (binding.imported === 'default') {
348+
if (binding.local === 'React') {
349+
continue
350+
}
351+
333352
preamble.push(`const ${binding.local} = React`)
334353
} else {
335-
preamble.push(`const ${binding.local} = React.${binding.imported}`)
354+
if (binding.local === 'React') {
355+
const reactRuntimeAlias = ensureReactRuntimeAlias()
356+
preamble.push(`const React = ${reactRuntimeAlias}.${binding.imported}`)
357+
} else {
358+
preamble.push(`const ${binding.local} = React.${binding.imported}`)
359+
}
336360
}
337361
}
338362
}

src/modules/type-diagnostics.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,11 +681,11 @@ export const createTypeDiagnosticsController = ({
681681
fileExists: fileName => files.has(normalizeVirtualFileName(fileName)),
682682
readFile: fileName => files.get(normalizeVirtualFileName(fileName)),
683683
directoryExists: directoryName => {
684-
const normalized = normalizeRelativePath(directoryName)
684+
const normalized = normalizeRelativePath(normalizeVirtualFileName(directoryName))
685685
return listVirtualDirectories(normalized).length > 0
686686
},
687687
getDirectories: directoryName => {
688-
const normalized = normalizeRelativePath(directoryName)
688+
const normalized = normalizeRelativePath(normalizeVirtualFileName(directoryName))
689689
return listVirtualDirectories(normalized)
690690
},
691691
realpath: fileName => normalizeVirtualFileName(fileName),

0 commit comments

Comments
 (0)