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: preload paths with different production base path breaks #3061

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 14 additions & 0 deletions packages/playground/base-path/__tests__/base-path.spec.ts
@@ -0,0 +1,14 @@
import { readFile, untilUpdated, isBuild } from '../../testUtils'

test('should work', async () => {
await page.click('.clickme')
await untilUpdated(() => page.textContent('.content'), '3', true)
})

if (isBuild) {
test('should have relative path', async () => {
expect(readFile('dist/foo/assets/index.js')).toMatch(
/.*\.\/assets\/preload\.js.*/
)
})
}
5 changes: 5 additions & 0 deletions packages/playground/base-path/dep.js
@@ -0,0 +1,5 @@
import preload from './preload'

export function test() {
return 1 + preload()
}
4 changes: 4 additions & 0 deletions packages/playground/base-path/index.html
@@ -0,0 +1,4 @@
<script type="module" src="./main.js"></script>

<button class="clickme">Click me</button>
<div class="content"></div>
4 changes: 4 additions & 0 deletions packages/playground/base-path/main.js
@@ -0,0 +1,4 @@
document.querySelector('.clickme').addEventListener('click', async () => {
const { test } = await import('./dep.js')
document.querySelector('.content').textContent = test()
})
11 changes: 11 additions & 0 deletions packages/playground/base-path/package.json
@@ -0,0 +1,11 @@
{
"name": "base-path",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"serve": "vite preview"
}
}
3 changes: 3 additions & 0 deletions packages/playground/base-path/preload.js
@@ -0,0 +1,3 @@
export default function () {
return 2
}
24 changes: 24 additions & 0 deletions packages/playground/base-path/vite.config.js
@@ -0,0 +1,24 @@
const { resolve } = require('path')

/**
* @type {import('vite').UserConfig}
*/
module.exports = {
base: '/foo/',
build: {
// simulate production environment
outDir: 'dist/foo',
// prevent bundling of dep and preload to ensure that vite-preloading occurs
rollupOptions: {
input: [
resolve(__dirname, 'index.html'),
resolve(__dirname, 'preload.js'),
resolve(__dirname, 'dep.js')
],
// use simple entry names for easier reference inside the tests
output: {
entryFileNames: 'assets/[name].js'
}
}
}
}
6 changes: 4 additions & 2 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -242,11 +242,13 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
analyzed.add(filename)
const chunk = bundle[filename] as OutputChunk | undefined
if (chunk) {
deps.add(config.base + chunk.fileName)
// use a relative path https://github.com/vitejs/vite/pull/3061
deps.add('./' + chunk.fileName)
const cssFiles = chunkToEmittedCssFileMap.get(chunk)
if (cssFiles) {
cssFiles.forEach((file) => {
deps.add(config.base + file)
// use a relative path https://github.com/vitejs/vite/pull/3061
deps.add('./' + file)
})
}
chunk.imports.forEach(addDeps)
Expand Down