Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dev): resolve correct relative path for alias dir internal import… #407

Merged
merged 1 commit into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion playground/TestAlias.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
<p class="alias">{{ msg }}</p>
<p class="dir-alias">{{ dirMsg }}</p>
<p class="dir-alias-index">{{ dirIndexMsg }}</p>
<p class="dir-alias-import-outside">{{ importOutsideMsg }}</p>
</template>

<script>
import { msg } from 'alias'
import { msg as dirMsg } from '/@alias/named'
import { msg as dirIndexMsg } from '/@alias/'
import { msg as importOutsideMsg } from '/@alias/importOutside'

export default {
data: () => ({
msg,
dirMsg,
dirIndexMsg
dirIndexMsg,
importOutsideMsg
})
}
</script>
1 change: 1 addition & 0 deletions playground/aliased-dir-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msg = 'directory aliased internal import outside works'
1 change: 1 addition & 0 deletions playground/aliased-dir/importOutside.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { msg } from '../aliased-dir-import'
25 changes: 25 additions & 0 deletions src/node/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export interface InternalResolver {
fileToRequest(filePath: string): string
normalizePublicPath(publicPath: string): string
alias(id: string): string | undefined
resolveRelativeRequest(
publicPath: string,
relativePublicPath: string
): { pathname: string; query: string }
}

export const supportedExts = ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json']
Expand Down Expand Up @@ -118,6 +122,7 @@ export function createResolver(
): InternalResolver {
resolvers = [...resolvers]
const literalAlias: Record<string, string> = {}
const literalDirAlias: Record<string, string> = {}

const resolveAlias = (alias: Record<string, string>) => {
for (const key in alias) {
Expand All @@ -143,6 +148,7 @@ export function createResolver(
}
}
})
literalDirAlias[key] = target
} else {
literalAlias[key] = target
}
Expand Down Expand Up @@ -281,6 +287,25 @@ export function createResolver(
return aliased
}
}
},

resolveRelativeRequest(publicPath: string, relativePublicPath: string) {
let dirname = path.dirname(publicPath)
for (const alias in literalDirAlias) {
if (publicPath.startsWith(alias)) {
dirname = path.join('/', path.relative(root, literalDirAlias[alias]))
break
}
}

const resolved = slash(path.posix.resolve(dirname, relativePublicPath))
const queryMatch = relativePublicPath.match(queryRE)
return {
// path resovle strips ending / which should be preserved
pathname:
cleanUrl(resolved) + (relativePublicPath.endsWith('/') ? '/' : ''),
query: queryMatch ? queryMatch[0] : ''
}
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/node/server/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ import {
hmrDirtyFilesMap,
latestVersionsMap
} from './serverPluginHmr'
import {
readBody,
cleanUrl,
isExternalUrl,
resolveRelativeRequest
} from '../utils'
import { readBody, cleanUrl, isExternalUrl } from '../utils'
import chalk from 'chalk'
import { isCSSRequest } from '../utils/cssUtils'

Expand Down Expand Up @@ -252,7 +247,7 @@ export const resolveImport = (
} else {
// 1. relative to absolute
// ./foo -> /some/path/foo
let { pathname, query } = resolveRelativeRequest(importer, id)
let { pathname, query } = resolver.resolveRelativeRequest(importer, id)

// 2. resolve dir index and extensions.
pathname = resolver.normalizePublicPath(pathname)
Expand Down
11 changes: 0 additions & 11 deletions src/node/utils/pathUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import path from 'path'
import slash from 'slash'
import qs, { ParsedUrlQuery } from 'querystring'
import resolve from 'resolve'
Expand All @@ -19,16 +18,6 @@ export const hashRE = /#.*$/
export const cleanUrl = (url: string) =>
url.replace(hashRE, '').replace(queryRE, '')

export const resolveRelativeRequest = (importer: string, id: string) => {
const resolved = slash(path.posix.resolve(path.dirname(importer), id))
const queryMatch = id.match(queryRE)
return {
// path resovle strips ending / which should be preserved
pathname: cleanUrl(resolved) + (id.endsWith('/') ? '/' : ''),
query: queryMatch ? queryMatch[0] : ''
}
}

export const parseWithQuery = (
id: string
): {
Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,25 +470,38 @@ describe('vite', () => {
expect(await getText('.dir-alias-index')).toMatch(
'directory alias index works'
)
expect(await getText('.dir-alias-import-outside')).toMatch(
'directory aliased internal import outside works'
)
if (!isBuild) {
await updateFile('aliased/index.js', (c) =>
c.replace('works', 'hmr works')
)
await expectByPolling(() => getText('.alias'), 'alias hmr works')

await updateFile('aliased-dir/named.js', (c) =>
c.replace('works', 'hmr works')
)
await expectByPolling(
() => getText('.dir-alias'),
'directory alias hmr works'
)

await updateFile('aliased-dir/index.js', (c) =>
c.replace('works', 'hmr works')
)
await expectByPolling(
() => getText('.dir-alias-index'),
'directory alias index hmr works'
)

await updateFile('aliased-dir-import.js', (c) =>
c.replace('works', 'hmr works')
)
await expectByPolling(
() => getText('.dir-alias-import-outside'),
'directory aliased internal import outside hmr works'
)
}
})

Expand Down