Skip to content

Commit e818cc0

Browse files
authored
Fix importing client-side components with alias (#5845)
1 parent 3a00ecb commit e818cc0

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

.changeset/ninety-garlics-fly.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fix importing client-side components with alias

packages/astro/src/core/build/static-build.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { generatePages } from './generate.js';
2222
import { trackPageData } from './internal.js';
2323
import type { PageBuildData, StaticBuildOptions } from './types';
2424
import { getTimeStat } from './util.js';
25+
import { vitePluginAliasResolve } from './vite-plugin-alias-resolve.js';
2526
import { vitePluginAnalyzer } from './vite-plugin-analyzer.js';
2627
import { rollupPluginAstroBuildCSS } from './vite-plugin-css.js';
2728
import { vitePluginHoistedScripts } from './vite-plugin-hoisted-scripts.js';
@@ -221,6 +222,7 @@ async function clientBuild(
221222
},
222223
},
223224
plugins: [
225+
vitePluginAliasResolve(internals),
224226
vitePluginInternals(input, internals),
225227
vitePluginHoistedScripts(settings, internals),
226228
rollupPluginAstroBuildCSS({
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { Alias, Plugin as VitePlugin } from 'vite';
2+
import type { BuildInternals } from '../../core/build/internal.js';
3+
4+
/**
5+
* `@rollup/plugin-alias` doesn't resolve aliases in Rollup input by default. This plugin fixes it
6+
* with a partial fork of it's resolve function. https://github.com/rollup/plugins/blob/master/packages/alias/src/index.ts
7+
* When https://github.com/rollup/plugins/pull/1402 is merged, we can remove this plugin.
8+
*/
9+
export function vitePluginAliasResolve(internals: BuildInternals): VitePlugin {
10+
let aliases: Alias[];
11+
12+
return {
13+
name: '@astro/plugin-alias-resolve',
14+
enforce: 'pre',
15+
configResolved(config) {
16+
aliases = config.resolve.alias;
17+
},
18+
async resolveId(id, importer, opts) {
19+
if (
20+
!importer &&
21+
(internals.discoveredHydratedComponents.has(id) ||
22+
internals.discoveredClientOnlyComponents.has(id))
23+
) {
24+
const matchedEntry = aliases.find((entry) => matches(entry.find, id));
25+
if (!matchedEntry) {
26+
return null;
27+
}
28+
29+
const updatedId = id.replace(matchedEntry.find, matchedEntry.replacement);
30+
31+
return this.resolve(updatedId, importer, Object.assign({ skipSelf: true }, opts)).then(
32+
(resolved) => resolved || { id: updatedId }
33+
);
34+
}
35+
},
36+
};
37+
}
38+
39+
function matches(pattern: string | RegExp, importee: string) {
40+
if (pattern instanceof RegExp) {
41+
return pattern.test(importee);
42+
}
43+
if (importee.length < pattern.length) {
44+
return false;
45+
}
46+
if (importee === pattern) {
47+
return true;
48+
}
49+
return importee.startsWith(pattern + '/');
50+
}

packages/astro/test/alias.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,21 @@ describe('Aliases', () => {
3535
expect(scripts.length).to.be.greaterThan(0);
3636
});
3737
});
38+
39+
describe('build', () => {
40+
before(async () => {
41+
await fixture.build();
42+
});
43+
44+
it('can load client components', async () => {
45+
const html = await fixture.readFile('/index.html');
46+
const $ = cheerio.load(html);
47+
48+
// Should render aliased element
49+
expect($('#client').text()).to.equal('test');
50+
51+
const scripts = $('script').toArray();
52+
expect(scripts.length).to.be.greaterThan(0);
53+
});
54+
});
3855
});

0 commit comments

Comments
 (0)