-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix namespaced component usage in MDX (#4272)
* fix(#4209): handle namespaced JSX and MDX * chore: add changeset * chore: update lockfile * fix: throw error when componentExport is unresolved * chore: bump compiler * chore: bump compiler * chore: revert example changes Co-authored-by: Nate Moore <nate@astro.build>
- Loading branch information
1 parent
3ca9051
commit 24d2f7a
Showing
17 changed files
with
315 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'astro': patch | ||
'@astrojs/mdx': patch | ||
--- | ||
|
||
Properly handle hydration for namespaced components |
7 changes: 7 additions & 0 deletions
7
packages/astro/e2e/fixtures/namespaced-component/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import preact from '@astrojs/preact'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [preact()], | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/astro/e2e/fixtures/namespaced-component/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@e2e/namespaced-component", | ||
"version": "0.0.0", | ||
"private": true, | ||
"devDependencies": { | ||
"@astrojs/preact": "workspace:*", | ||
"astro": "workspace:*" | ||
}, | ||
"dependencies": { | ||
"preact": "^10.7.3" | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/astro/e2e/fixtures/namespaced-component/src/components/PreactCounter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useState } from 'preact/hooks'; | ||
|
||
/** a counter written in Preact */ | ||
function PreactCounter({ children, id }) { | ||
const [count, setCount] = useState(0); | ||
const add = () => setCount((i) => i + 1); | ||
const subtract = () => setCount((i) => i - 1); | ||
|
||
return ( | ||
<div id={id} class="counter"> | ||
<button class="decrement" onClick={subtract}>-</button> | ||
<pre>{count}</pre> | ||
<button class="increment" onClick={add}>+</button> | ||
<div class="children">{children}</div> | ||
</div> | ||
); | ||
} | ||
|
||
export const components = { PreactCounter } |
18 changes: 18 additions & 0 deletions
18
packages/astro/e2e/fixtures/namespaced-component/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
import * as ns from '../components/PreactCounter.tsx'; | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<link rel="icon" type="image/x-icon" href="/favicon.ico" /> | ||
</head> | ||
<body> | ||
<main> | ||
<ns.components.PreactCounter id="preact-counter" client:load> | ||
<h1>preact</h1> | ||
</ns.components.PreactCounter> | ||
</main> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { expect } from '@playwright/test'; | ||
import { testFactory } from './test-utils.js'; | ||
|
||
const test = testFactory({ | ||
root: './fixtures/namespaced-component/', | ||
}); | ||
|
||
let devServer; | ||
|
||
test.beforeEach(async ({ astro }) => { | ||
devServer = await astro.startDevServer(); | ||
}); | ||
|
||
test.afterEach(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
test.describe('Hydrating namespaced components', () => { | ||
test('Preact Component', async ({ page }) => { | ||
await page.goto('/'); | ||
|
||
const counter = await page.locator('#preact-counter'); | ||
await expect(counter, 'component is visible').toBeVisible(); | ||
|
||
const count = await counter.locator('pre'); | ||
await expect(count, 'initial count is 0').toHaveText('0'); | ||
|
||
const children = await counter.locator('.children'); | ||
await expect(children, 'children exist').toHaveText('preact'); | ||
|
||
const increment = await counter.locator('.increment'); | ||
await increment.click(); | ||
|
||
await expect(count, 'count incremented by 1').toHaveText('1'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/integrations/mdx/test/fixtures/mdx-namespace/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import mdx from '@astrojs/mdx'; | ||
import react from '@astrojs/react'; | ||
|
||
export default { | ||
integrations: [react(), mdx()] | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/integrations/mdx/test/fixtures/mdx-namespace/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@test/mdx-namespace", | ||
"dependencies": { | ||
"astro": "workspace:*", | ||
"@astrojs/mdx": "workspace:*", | ||
"@astrojs/react": "workspace:*" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/integrations/mdx/test/fixtures/mdx-namespace/src/components/Component.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const Component = () => { | ||
return <p id="component">Hello world</p>; | ||
}; | ||
export const ns = { | ||
Component | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/integrations/mdx/test/fixtures/mdx-namespace/src/pages/object.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as mod from '../components/Component.jsx'; | ||
|
||
<mod.ns.Component client:load /> |
3 changes: 3 additions & 0 deletions
3
packages/integrations/mdx/test/fixtures/mdx-namespace/src/pages/star.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ns } from '../components/Component.jsx'; | ||
|
||
<ns.Component client:load /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { expect } from 'chai'; | ||
import { parseHTML } from 'linkedom'; | ||
import { loadFixture } from '../../../astro/test/test-utils.js'; | ||
|
||
describe('MDX Namespace', () => { | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: new URL('./fixtures/mdx-namespace/', import.meta.url), | ||
}); | ||
}); | ||
|
||
describe('build', () => { | ||
before(async () => { | ||
await fixture.build(); | ||
}); | ||
|
||
it('works for object', async () => { | ||
const html = await fixture.readFile('/object/index.html'); | ||
const { document } = parseHTML(html); | ||
|
||
const island = document.querySelector('astro-island'); | ||
const component = document.querySelector('#component'); | ||
|
||
expect(island).not.undefined; | ||
expect(component.textContent).equal('Hello world') | ||
}); | ||
|
||
it('works for star', async () => { | ||
const html = await fixture.readFile('/star/index.html'); | ||
const { document } = parseHTML(html); | ||
|
||
const island = document.querySelector('astro-island'); | ||
const component = document.querySelector('#component'); | ||
|
||
expect(island).not.undefined; | ||
expect(component.textContent).equal('Hello world') | ||
}); | ||
}); | ||
|
||
describe('dev', () => { | ||
let devServer; | ||
|
||
before(async () => { | ||
devServer = await fixture.startDevServer(); | ||
}); | ||
|
||
after(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
it('works for object', async () => { | ||
const res = await fixture.fetch('/object'); | ||
|
||
expect(res.status).to.equal(200); | ||
|
||
const html = await res.text(); | ||
const { document } = parseHTML(html); | ||
|
||
const island = document.querySelector('astro-island'); | ||
const component = document.querySelector('#component'); | ||
|
||
expect(island).not.undefined; | ||
expect(component.textContent).equal('Hello world') | ||
}); | ||
|
||
it('works for star', async () => { | ||
const res = await fixture.fetch('/star'); | ||
|
||
expect(res.status).to.equal(200); | ||
|
||
const html = await res.text(); | ||
const { document } = parseHTML(html); | ||
|
||
const island = document.querySelector('astro-island'); | ||
const component = document.querySelector('#component'); | ||
|
||
expect(island).not.undefined; | ||
expect(component.textContent).equal('Hello world') | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.