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

Dep Optimization cann't work with import.meta.url asset #11095

Closed
7 tasks done
dabuside opened this issue Nov 28, 2022 · 3 comments
Closed
7 tasks done

Dep Optimization cann't work with import.meta.url asset #11095

dabuside opened this issue Nov 28, 2022 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@dabuside
Copy link

dabuside commented Nov 28, 2022

Describe the bug

I have a npm package with static asset using new URL('img', import.meta.url) to reference.
npm package structure

@dabuside/test-import-meta-asset/
├─ logo.svg
├─ index.js  

index.js

const img = new URL('./logo.svg', import.meta.url);
export default img;

Vite prebuild the dependency into .vite/ folder, without moving asset into it.
Running dev mode, javascript file was loaded from .vite/ folder, static asset also loaded from .vite/, which is incorrect.
I should always configure optimizeDeps.exclude to prevent prebuild those assets, which is annoying.

Reproduction

https://stackblitz.com/edit/vitejs-vite-sef4ob?file=main.js&terminal=dev

Steps to reproduce

  1. Open the link, you should see a broken image.
  2. Run vite build && vite preview, you shoule see 2 vite logo works as expected.

dev mode

image

preview mode

image

System Info

System:
    OS: Windows 10 10.0.17763
    CPU: (4) x64 Intel(R) Core(TM)2 Duo CPU     T7700  @ 2.40GHz
    Memory: 1.58 GB / 8.00 GB
  Binaries:
    Node: 16.13.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.17 - C:\Program Files\nodejs\yarn.CMD
    npm: 8.1.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 107.0.5304.121
    Internet Explorer: 11.0.17763.1

Used Package Manager

npm

Logs

No response

Validations

@luo3house
Copy link
Contributor

It seems Vite may not be clear whether the optimized module needs some assets to import.

But explicitly import keyword can be recognized. Then the asset path is redirected back to node_modules.

import img from './logo.svg';

be optimized as follows:

import img from "/home/projects/vitejs-vite-tvn831/node_modules/@dabuside/test-import-meta-asset/logo.svg";

vite build works is because Deps optimization is enabled by default in dev only.

@dabuside
Copy link
Author

It seems Vite may not be clear whether the optimized module needs some assets to import.

But explicitly import keyword can be recognized. Then the asset path is redirected back to node_modules.

import img from './logo.svg';

be optimized as follows:

import img from "/home/projects/vitejs-vite-tvn831/node_modules/@dabuside/test-import-meta-asset/logo.svg";

vite build works is because Deps optimization is enabled by default in dev only.

I can't explicitly import assets. Because I'm shipping an npm package with a lot of static asset. (eg, vue component with background image, map with on demand load geojson)

@sapphi-red
Copy link
Member

Duplicate of #8427

@sapphi-red sapphi-red marked this as a duplicate of #8427 Nov 29, 2022
@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Nov 29, 2022
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Nov 29, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Dec 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants