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: relative path html #8122

Merged
merged 9 commits into from May 12, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -174,7 +174,11 @@ export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {
}

// relative
if (id.startsWith('.') || (preferRelative && /^\w/.test(id))) {
if (
id.startsWith('.') ||
(preferRelative && /^\w/.test(id)) ||
importer?.endsWith('.html')
) {
const basedir = importer ? path.dirname(importer) : process.cwd()
const fsPath = path.resolve(basedir, id)
// handle browser field mapping for relative imports
Expand Down
5 changes: 5 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Expand Up @@ -347,3 +347,8 @@ test('html import word boundary', async () => {
)
expect(await page.textContent('.string-import-express')).toMatch('no load')
})

test('relative path in html asset', async () => {
expect(await page.textContent('.relative-js')).toMatch('hello')
expect(await getColor('.relative-css')).toMatch('red')
})
4 changes: 4 additions & 0 deletions playground/assets/asset/main.js
@@ -0,0 +1,4 @@
function text(el, text) {
document.querySelector(el).textContent = text
}
text('.relative-js', 'hello')
3 changes: 3 additions & 0 deletions playground/assets/asset/style.css
@@ -0,0 +1,3 @@
.relative-css {
color: red;
}
5 changes: 4 additions & 1 deletion playground/assets/index.html
Expand Up @@ -290,7 +290,10 @@ <h3>style in svg</h3>
/>
</g>
</svg>

<link rel="stylesheet" href="asset/style.css" />
<div class="relative-css">link style</div>
<div class="relative-js"></div>
<script src="asset/main.js" type="module"></script>
<style>
@import '/foo.css';
</style>
Expand Down
7 changes: 7 additions & 0 deletions playground/html/__tests__/html.spec.ts
Expand Up @@ -66,6 +66,13 @@ function testPage(isNested: boolean) {
expect(await getColor('h1')).toBe(isNested ? 'red' : 'blue')
expect(await getColor('p')).toBe('grey')
})

if (isNested) {
test('relative path in html asset', async () => {
expect(await page.textContent('.relative-js')).toMatch('hello')
expect(await getColor('.relative-css')).toMatch('red')
})
}
}

describe('main', () => {
Expand Down
4 changes: 4 additions & 0 deletions playground/html/nested/asset/main.js
@@ -0,0 +1,4 @@
function text(el, text) {
document.querySelector(el).textContent = text
}
text('.relative-js', 'hello')
3 changes: 3 additions & 0 deletions playground/html/nested/asset/style.css
@@ -0,0 +1,3 @@
.relative-css {
color: red;
}
6 changes: 6 additions & 0 deletions playground/html/nested/index.html
@@ -1,3 +1,9 @@
<link rel="stylesheet" href="./nested.css" />
<h1>Nested</h1>
<script type="module" src="./nested.js"></script>

<h1>no base path nested</h1>
<link rel="stylesheet" href="asset/style.css" />
<div class="relative-css">link style</div>
<div class="relative-js"></div>
<script src="asset/main.js" type="module"></script>