Skip to content

Commit

Permalink
fix(alias): normalize alias behavior when there is ending slash
Browse files Browse the repository at this point in the history
ref #1363
  • Loading branch information
yyx990803 committed Jan 6, 2021
1 parent 53fe84f commit c4739a3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/guide/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@

## Alias Behavior Change

[`alias`](/config/#alias) is now being passed to `@rollup/plugin-alias` and no longer require start/ending slashes. The behavior is now a direct replacement, so 1.0-style directory aliases now require an ending slash:
[`alias`](/config/#alias) is now being passed to `@rollup/plugin-alias` and no longer require start/ending slashes. The behavior is now a direct replacement, so 1.0-style directory alias key should remove the ending slash:

```diff
- alias: { '/@foo/': path.resolve(__dirname, 'some-special-dir') }
+ alias: { '/@foo/': path.resolve(__dirname, 'some-special-dir') + '/' }
+ alias: { '/@foo': path.resolve(__dirname, 'some-special-dir') }
```

Alternatively, you can use the `[{ find: RegExp, replacement: string }]` option format for more precise control.
Expand Down
1 change: 1 addition & 0 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"homepage": "https://github.com/vitejs/vite/tree/main/#readme",
"scripts": {
"predev": "rimraf dist",
"dev": "run-p dev-client dev-node",
"dev-client": "tsc -w --incremental --p src/client",
"dev-node": "tsc -w --incremental --p src/node",
Expand Down
28 changes: 22 additions & 6 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,28 @@ function mergeAlias(a: AliasOptions = [], b: AliasOptions = []): Alias[] {
}

function normalizeAlias(o: AliasOptions): Alias[] {
return isObject(o)
? Object.keys(o).map((find) => ({
find,
replacement: (o as any)[find]
}))
: o
return Array.isArray(o)
? o.map(normalizeSingleAlias)
: Object.keys(o).map((find) =>
normalizeSingleAlias({
find,
replacement: (o as any)[find]
})
)
}

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

export function sortUserPlugins(
Expand Down

0 comments on commit c4739a3

Please sign in to comment.