-
-
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 importing client-side components with alias (#5845)
- Loading branch information
Showing
4 changed files
with
74 additions
and
0 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,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Fix importing client-side components with alias |
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
50 changes: 50 additions & 0 deletions
50
packages/astro/src/core/build/vite-plugin-alias-resolve.ts
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,50 @@ | ||
import type { Alias, Plugin as VitePlugin } from 'vite'; | ||
import type { BuildInternals } from '../../core/build/internal.js'; | ||
|
||
/** | ||
* `@rollup/plugin-alias` doesn't resolve aliases in Rollup input by default. This plugin fixes it | ||
* with a partial fork of it's resolve function. https://github.com/rollup/plugins/blob/master/packages/alias/src/index.ts | ||
* When https://github.com/rollup/plugins/pull/1402 is merged, we can remove this plugin. | ||
*/ | ||
export function vitePluginAliasResolve(internals: BuildInternals): VitePlugin { | ||
let aliases: Alias[]; | ||
|
||
return { | ||
name: '@astro/plugin-alias-resolve', | ||
enforce: 'pre', | ||
configResolved(config) { | ||
aliases = config.resolve.alias; | ||
}, | ||
async resolveId(id, importer, opts) { | ||
if ( | ||
!importer && | ||
(internals.discoveredHydratedComponents.has(id) || | ||
internals.discoveredClientOnlyComponents.has(id)) | ||
) { | ||
const matchedEntry = aliases.find((entry) => matches(entry.find, id)); | ||
if (!matchedEntry) { | ||
return null; | ||
} | ||
|
||
const updatedId = id.replace(matchedEntry.find, matchedEntry.replacement); | ||
|
||
return this.resolve(updatedId, importer, Object.assign({ skipSelf: true }, opts)).then( | ||
(resolved) => resolved || { id: updatedId } | ||
); | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
function matches(pattern: string | RegExp, importee: string) { | ||
if (pattern instanceof RegExp) { | ||
return pattern.test(importee); | ||
} | ||
if (importee.length < pattern.length) { | ||
return false; | ||
} | ||
if (importee === pattern) { | ||
return true; | ||
} | ||
return importee.startsWith(pattern + '/'); | ||
} |
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