Skip to content

Commit

Permalink
feature: pass tsconfig to vite alias
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathantneal committed Feb 28, 2023
1 parent 045262e commit a20f01f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-colts-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Pass config aliases directly to Vite alias
30 changes: 5 additions & 25 deletions packages/astro/src/vite-plugin-config-alias/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export default function configAliasVitePlugin({
}: {
settings: AstroSettings;
}): vite.PluginOption {
const { config } = settings;
/** Aliases from the tsconfig.json or jsconfig.json configuration. */
const configAlias = getConfigAlias(settings);

Expand All @@ -91,29 +90,10 @@ export default function configAliasVitePlugin({
return {
name: 'astro:tsconfig-alias',
enforce: 'pre',
async resolveId(sourceId: string, importer, options) {
/** Resolved ID conditionally handled by any other resolver. (this gives priority to all other resolvers) */
const resolvedId = await this.resolve(sourceId, importer, { skipSelf: true, ...options });

// if any other resolver handles the file, return that resolution
if (resolvedId) return resolvedId;

// conditionally resolve the source ID from any matching alias or baseUrl
for (const alias of configAlias) {
if (alias.find.test(sourceId)) {
/** Processed Source ID with our alias applied. */
const aliasedSourceId = sourceId.replace(alias.find, alias.replacement);

/** Resolved ID conditionally handled by any other resolver. (this also gives priority to all other resolvers) */
const resolvedAliasedId = await this.resolve(aliasedSourceId, importer, {
skipSelf: true,
...options,
});

// if the existing resolvers find the file, return that resolution
if (resolvedAliasedId) return resolvedAliasedId;
}
}
},
config: () => ({
resolve: {
alias: configAlias,
},
}),
};
}

0 comments on commit a20f01f

Please sign in to comment.