Skip to content

Commit

Permalink
Fix mixed usage of aliases and relative for client hydration (#6168)
Browse files Browse the repository at this point in the history
* Fix mixed usgae of aliases and relative for client hydration

* Add a changeset
  • Loading branch information
matthewp authored Feb 7, 2023
1 parent 92dcf81 commit c0e4b1d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-olives-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix mixed usage of aliases and relative for client hydration
14 changes: 10 additions & 4 deletions packages/astro/src/core/build/plugins/plugin-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,27 @@ export function vitePluginInternals(input: Set<string>, internals: BuildInternal

async generateBundle(_options, bundle) {
const promises = [];
const mapping = new Map<string, string>();
const mapping = new Map<string, Set<string>>();
for (const specifier of input) {
promises.push(
this.resolve(specifier).then((result) => {
if (result) {
mapping.set(result.id, specifier);
if(mapping.has(result.id)) {
mapping.get(result.id)!.add(specifier);
} else {
mapping.set(result.id, new Set<string>([specifier]));
}
}
})
);
}
await Promise.all(promises);
for (const [, chunk] of Object.entries(bundle)) {
if (chunk.type === 'chunk' && chunk.facadeModuleId) {
const specifier = mapping.get(chunk.facadeModuleId) || chunk.facadeModuleId;
internals.entrySpecifierToBundleMap.set(specifier, chunk.fileName);
const specifiers = mapping.get(chunk.facadeModuleId) || new Set([chunk.facadeModuleId]);
for(const specifier of specifiers) {
internals.entrySpecifierToBundleMap.set(specifier, chunk.fileName);
}
} else if (chunk.type === 'chunk') {
for (const id of Object.keys(chunk.modules)) {
const pageData = internals.pagesByViteID.get(id);
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/test/alias.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,16 @@ describe('Aliases', () => {
const scripts = $('script').toArray();
expect(scripts.length).to.be.greaterThan(0);
});

it('can use aliases and relative in same project', async () => {
const html = await fixture.readFile('/two/index.html');
const $ = cheerio.load(html);

// Should render aliased element
expect($('#client').text()).to.equal('test');

const scripts = $('script').toArray();
expect(scripts.length).to.be.greaterThan(0);
});
});
});
25 changes: 25 additions & 0 deletions packages/astro/test/fixtures/alias/src/pages/two.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
import Client from '../components/Client.svelte'
---
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Svelte Client</title>
<style>
html,
body {
font-family: system-ui;
margin: 0;
}
body {
padding: 2rem;
}
</style>
</head>
<body>
<main>
<Client client:load />
</main>
</body>
</html>

0 comments on commit c0e4b1d

Please sign in to comment.