diff --git a/packages/playground/base-path/__tests__/base-path.spec.ts b/packages/playground/base-path/__tests__/base-path.spec.ts new file mode 100644 index 00000000000000..3003213c53cb30 --- /dev/null +++ b/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.*/ + ) + }) +} diff --git a/packages/playground/base-path/dep.js b/packages/playground/base-path/dep.js new file mode 100644 index 00000000000000..ceba2075064d12 --- /dev/null +++ b/packages/playground/base-path/dep.js @@ -0,0 +1,5 @@ +import preload from './preload' + +export function test() { + return 1 + preload() +} diff --git a/packages/playground/base-path/index.html b/packages/playground/base-path/index.html new file mode 100644 index 00000000000000..0c022aeddfc4c5 --- /dev/null +++ b/packages/playground/base-path/index.html @@ -0,0 +1,4 @@ + + + +
diff --git a/packages/playground/base-path/main.js b/packages/playground/base-path/main.js new file mode 100644 index 00000000000000..273c9bb7ffa466 --- /dev/null +++ b/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() +}) diff --git a/packages/playground/base-path/package.json b/packages/playground/base-path/package.json new file mode 100644 index 00000000000000..28fe91d2c98fe3 --- /dev/null +++ b/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" + } +} diff --git a/packages/playground/base-path/preload.js b/packages/playground/base-path/preload.js new file mode 100644 index 00000000000000..ef8bd729e9f926 --- /dev/null +++ b/packages/playground/base-path/preload.js @@ -0,0 +1,3 @@ +export default function () { + return 2 +} diff --git a/packages/playground/base-path/vite.config.js b/packages/playground/base-path/vite.config.js new file mode 100644 index 00000000000000..6a52ce6ee5be28 --- /dev/null +++ b/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' + } + } + } +} diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 8ba83f3ea89dd1..39096e75fffd3e 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -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)