Skip to content

Commit

Permalink
fix: relative path html (#8122)
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed May 12, 2022
1 parent 1cc2e2d commit d0deac0
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 2 deletions.
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>

0 comments on commit d0deac0

Please sign in to comment.