Skip to content

Commit

Permalink
feat: add customResolver option to resolve.alias (#5876)
Browse files Browse the repository at this point in the history
  • Loading branch information
y1d7ng authored Jan 11, 2022
1 parent 7541a8d commit 6408a3a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ export default defineConfig(async ({ command, mode }) => {
### resolve.alias
- **Type:**
`Record<string, string> | Array<{ find: string | RegExp, replacement: string }>`
`Record<string, string> | Array<{ find: string | RegExp, replacement: string, customResolver?: ResolverFunction | ResolverObject }>`
Will be passed to `@rollup/plugin-alias` as its [entries option](https://github.com/rollup/plugins/tree/master/packages/alias#entries). Can either be an object, or an array of `{ find, replacement }` pairs.
Will be passed to `@rollup/plugin-alias` as its [entries option](https://github.com/rollup/plugins/tree/master/packages/alias#entries). Can either be an object, or an array of `{ find, replacement, customResolver }` pairs.
When aliasing to file system paths, always use absolute paths. Relative alias values will be used as-is and will not be resolved into file system paths.
Expand Down
6 changes: 6 additions & 0 deletions packages/playground/alias/__tests__/alias.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ test('aliased module', async () => {
'[success] aliased module'
)
})

test('custom resolver', async () => {
expect(await page.textContent('.custom-resolver')).toMatch(
'[success] alias to custom-resolver path'
)
})
1 change: 1 addition & 0 deletions packages/playground/alias/customResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msg = `[success] alias to custom-resolver path`
3 changes: 3 additions & 0 deletions packages/playground/alias/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ <h1>Alias</h1>
<p class="dep"></p>
<p class="from-script-src"></p>
<p class="aliased-module"></p>
<p class="custom-resolver"></p>

<div class="optimized"></div>

Expand All @@ -15,6 +16,7 @@ <h1>Alias</h1>
import { msg as regexMsg } from 'regex/test'
import { msg as depMsg } from 'dep'
import { msg as moduleMsg } from 'aliased-module/index.js'
import { msg as customResolverMsg } from 'custom-resolver'

function text(el, text) {
document.querySelector(el).textContent = text
Expand All @@ -25,6 +27,7 @@ <h1>Alias</h1>
text('.regex', regexMsg + ' via regex')
text('.dep', depMsg)
text('.aliased-module', moduleMsg)
text('.custom-resolver', customResolverMsg)

import { createApp } from 'vue'
import { ref } from 'foo'
Expand Down
9 changes: 8 additions & 1 deletion packages/playground/alias/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ module.exports = {
// aliasing an optimized dep
{ find: 'vue', replacement: 'vue/dist/vue.esm-bundler.js' },
// aliasing one unoptimized dep to an optimized dep
{ find: 'foo', replacement: 'vue' }
{ find: 'foo', replacement: 'vue' },
{
find: 'custom-resolver',
replacement: path.resolve(__dirname, 'test.js'),
customResolver(id) {
return id.replace('test.js', 'customResolver.js')
}
}
]
},
build: {
Expand Down
16 changes: 14 additions & 2 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,11 @@ function normalizeAlias(o: AliasOptions): Alias[] {

// https://github.com/vitejs/vite/issues/1363
// work around https://github.com/rollup/plugins/issues/759
function normalizeSingleAlias({ find, replacement }: Alias): Alias {
function normalizeSingleAlias({
find,
replacement,
customResolver
}: Alias): Alias {
if (
typeof find === 'string' &&
find.endsWith('/') &&
Expand All @@ -794,7 +798,15 @@ function normalizeSingleAlias({ find, replacement }: Alias): Alias {
find = find.slice(0, find.length - 1)
replacement = replacement.slice(0, replacement.length - 1)
}
return { find, replacement }

const alias: Alias = {
find,
replacement
}
if (customResolver) {
alias.customResolver = customResolver
}
return alias
}

export function sortUserPlugins(
Expand Down

0 comments on commit 6408a3a

Please sign in to comment.