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: consider # as a valid dir symbol (fix #4701) #4703

Merged
merged 9 commits into from Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/playground/resolve/__tests__/resolve.spec.ts
Expand Up @@ -89,3 +89,9 @@ test('resolve.mainFields', async () => {
test('resolve.conditions', async () => {
expect(await page.textContent('.custom-condition')).toMatch('[success]')
})

test('resolve package that contains # in path', async () => {
expect(await page.textContent('.path-contains-sharp-symbol')).toMatch(
'[success]'
)
})
6 changes: 6 additions & 0 deletions packages/playground/resolve/index.html
Expand Up @@ -60,6 +60,9 @@ <h2>resolve.mainFields</h2>
<h2>resolve.conditions</h2>
<p class="custom-condition"></p>

<h2>resolve package that contains # in path</h2>
<p class="path-contains-sharp-symbol"></p>

<script type="module">
function text(selector, text) {
document.querySelector(selector).textContent = text
Expand Down Expand Up @@ -171,6 +174,9 @@ <h2>resolve.conditions</h2>

import { msg as inlineMsg } from './inline-package'
text('.inline-pkg', inlineMsg)

import es5Ext from 'es5-ext'
text('.path-contains-sharp-symbol', '[success]')
</script>

<style>
Expand Down
1 change: 1 addition & 0 deletions packages/playground/resolve/package.json
Expand Up @@ -16,6 +16,7 @@
"resolve-custom-condition": "link:./custom-condition",
"@babel/runtime": "^7.12.5",
"normalize.css": "^8.0.1",
"es5-ext": "0.10.53",
"resolve-linked": "workspace:*"
}
}
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/loadFallback.ts
Expand Up @@ -10,7 +10,8 @@ export function loadFallbackPlugin(): Plugin {
name: 'vite:load-fallback',
async load(id) {
try {
return fs.readFile(cleanUrl(id), 'utf-8')
// if we don't add `await` here, we couldn't catch the error in readFile
Copy link
Contributor Author

@hyf0 hyf0 Oct 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, is this plugin never working 🤣 ?

return await fs.readFile(cleanUrl(id), 'utf-8')
} catch (e) {
return fs.readFile(id, 'utf-8')
}
Expand Down
50 changes: 50 additions & 0 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -314,6 +314,24 @@ function tryFsResolve(
}

let res: string | undefined

// if we fould postfix exist, we should first try resolving file with postfix. details see #4703.
if (
postfix &&
(res = tryResolveFile(
fsPath,
'',
options,
false,
targetWeb,
preserveSymlinks,
options.tryPrefix,
options.skipPackageJson
))
) {
return res
}
patak-dev marked this conversation as resolved.
Show resolved Hide resolved

if (
(res = tryResolveFile(
file,
Expand All @@ -330,6 +348,22 @@ function tryFsResolve(
}

for (const ext of options.extensions || DEFAULT_EXTENSIONS) {
if (
postfix &&
(res = tryResolveFile(
fsPath + ext,
'',
options,
false,
targetWeb,
preserveSymlinks,
options.tryPrefix,
options.skipPackageJson
))
) {
return res
}

if (
(res = tryResolveFile(
file + ext,
Expand All @@ -346,6 +380,22 @@ function tryFsResolve(
}
}

if (
postfix &&
(res = tryResolveFile(
fsPath,
'',
options,
tryIndex,
targetWeb,
preserveSymlinks,
options.tryPrefix,
options.skipPackageJson
))
) {
return res
}

if (
(res = tryResolveFile(
file,
Expand Down
50 changes: 50 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.