vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.08k stars 6.13k forks source link

building urls with `new URL(...)` doesn't work anymore in 5.1 when `publicDir` is set to false #16034

Closed schelmo closed 7 months ago

schelmo commented 7 months ago

Describe the bug

When you have set publicDir to false and building urls with

const url = new URL('./somefile', import.meta.url).href

In vite 5.0.x this worked. but since vite 5.1, this doesn't work anymore.

my guess the relevant part is here: https://github.com/vitejs/vite/blob/b20d54257e6105333c19676a403c574667878e0f/packages/vite/src/node/plugins/assetImportMetaUrl.ts#L124 i tried to rewrite it to

if (publicDir !== false && isParentDirectory(publicDir, file)) {

but it seems tha publicDir is set to an empty string.

Reproduction

https://stackblitz.com/edit/vitejs-vite-ggxiek?file=main.js

Steps to reproduce

Run npm install then run npm run build to the see log entry

System Info

System:
  OS: Linux 6.7
  CPU: (32) x64 AMD Ryzen 9 5950X 16-Core Processor
  Memory: 52.67 GB / 62.72 GB
  Container: Yes
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 21.6.2 - /usr/bin/node
  Yarn: 1.22.21 - /usr/bin/yarn
  npm: 10.4.0 - /usr/bin/npm
  pnpm: 8.15.2 - /usr/bin/pnpm
  bun: 1.0.29 - /usr/bin/bun
Browsers:
  Chromium: 122.0.6261.69
npmPackages:
  @vitejs/plugin-legacy: ^5.3.0 => 5.3.0 
  @vitejs/plugin-vue2: ^2.3.1 => 2.3.1 
  vite: ^5.1.4 => 5.1.4

Used Package Manager

npm

Logs

Click to expand! ```shell new URL('./javascript.svg', import.meta.url) doesn't exist at build time, it will remain unchanged to be resolved at runtime ```

Validations

stackblitz[bot] commented 7 months ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

miken32 commented 7 months ago

Actually I am having this problem with publicDir left at its default setting. This code works fine in 5.0.12 but breaks in 5.1:

import pdfMake from "pdfmake/build/pdfmake";
import "@fontsource/source-sans-pro/latin-400.css"
import "@fontsource/source-sans-pro/latin-400-italic.css"
import "@fontsource/source-sans-pro/latin-700.css"
import "@fontsource/source-sans-pro/latin-700-italic.css"

pdfMake.fonts = {
    SourceSans: {
        normal: new URL("../../node_modules/@fontsource/source-sans-pro/files/source-sans-pro-latin-400-normal.woff", import.meta.url).href,
        bold: new URL("../../node_modules/@fontsource/source-sans-pro/files/source-sans-pro-latin-700-normal.woff", import.meta.url).href,
        italics: new URL("../../node_modules/@fontsource/source-sans-pro/files/source-sans-pro-latin-400-italic.woff", import.meta.url).href,
        bolditalics: new URL("../../node_modules/@fontsource/source-sans-pro/files/source-sans-pro-latin-700-italic.woff", import.meta.url).href,
    },
};

Output:

vite v5.1.4 building for production...
transforming (1) resources/js/user.js
new URL("../../node_modules/@fontsource/source-sans-pro/files/source-sans-pro-latin-400-normal.woff", import.meta.url) doesn't exist at build time, it will remain unchanged to be resolved at runtime

new URL("../../node_modules/@fontsource/source-sans-pro/files/source-sans-pro-latin-700-normal.woff", import.meta.url) doesn't exist at build time, it will remain unchanged to be resolved at runtime

new URL("../../node_modules/@fontsource/source-sans-pro/files/source-sans-pro-latin-400-italic.woff", import.meta.url) doesn't exist at build time, it will remain unchanged to be resolved at runtime

new URL("../../node_modules/@fontsource/source-sans-pro/files/source-sans-pro-latin-700-italic.woff", import.meta.url) doesn't exist at build time, it will remain unchanged to be resolved at runtime
✓ 659 modules transformed.
schelmo commented 7 months ago

@miken32 may it be that you don't have a public directory in your project?

miken32 commented 7 months ago

@schelmo I do indeed, it's a Laravel project.

schelmo commented 7 months ago

ok then this is the same issue because laravel sets the publicDir default to false: https://github.com/laravel/vite-plugin/blob/dace190f626b5d67dc32958d435701efa9615b42/src/index.ts#L142

andreypopov commented 7 months ago

the same issue in laravel project