-
-
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.
- Loading branch information
Showing
1 changed file
with
29 additions
and
11 deletions.
There are no files selected for viewing
40 changes: 29 additions & 11 deletions
40
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 |
---|---|---|
@@ -1,32 +1,50 @@ | ||
import type { Plugin as VitePlugin } from 'vite'; | ||
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. | ||
* `@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 inputResolve: (id: string) => Promise<string>; | ||
let aliases: Alias[]; | ||
|
||
return { | ||
name: '@astro/plugin-alias-resolve', | ||
enforce: 'pre', | ||
configResolved(config) { | ||
const viteResolve = config.createResolver(); | ||
inputResolve = async (id) => { | ||
const resolved = await viteResolve(id, ' ', true); | ||
return resolved || id; | ||
}; | ||
aliases = config.resolve.alias; | ||
}, | ||
async resolveId(id, importer) { | ||
async resolveId(id, importer, opts) { | ||
if ( | ||
!importer && | ||
(internals.discoveredHydratedComponents.has(id) || | ||
internals.discoveredClientOnlyComponents.has(id)) | ||
) { | ||
const resolved = await inputResolve(id); | ||
return await this.resolve(resolved, importer, { skipSelf: true }); | ||
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 + '/'); | ||
} |