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

feat: allow assets to be external instead of inline in lib mode. #6267

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

wxsms
Copy link

@wxsms wxsms commented Dec 27, 2021

Description

this PR solved #4454

As a newbie to vite, I have no idea whether this solution or the option design is is good enough, but it works in my case. I can tweak this if any feedback. If this looks too stupid, sry to bother, pls close it at will.

Additional context

This feature generate code like this in library mode:

import { defineComponent, openBlock, createElementBlock, unref, ref, computed, watch, nextTick, onBeforeUnmount, onMounted, normalizeClass, Fragment, renderList, renderSlot, normalizeStyle, createVNode, withCtx, createCommentVNode, pushScopeId, popScopeId, createElementVNode, createTextVNode } from "vue";
import _imports_0$1 from "./assets/some-image.68d77419.png"; // <-
import _imports_1$1 from "./assets/other-image.58733637.png"; // <-

// ...

css (if any):

/* added a dot in front of asset path */
.list-loading[data-v-5e2e31c0]{background:url(./assets/list-loading-dark.e0e27964.gif)}

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@wxsms wxsms marked this pull request as ready for review December 27, 2021 08:50
@Shinigami92 Shinigami92 added YAO Yet another option... p2-nice-to-have Not breaking anything but nice to have (priority) labels Dec 27, 2021
@hronro
Copy link
Contributor

hronro commented Feb 17, 2022

import _imports_0$1 from "./assets/some-image.68d77419.png";

some-image.68d77419.png is not a valid javascript module, so if you run the generated code directly in the browser, you'll get an error.

It is OK if you consume the generated code via some bundlers. However, not all the bundlers handle this (import assets in javascript) in a same way. I think it's better not to assume all the users will use a bundler to consume the generated code.

@Bigfish8
Copy link
Contributor

@hronro
How about const _imports_0$1 = "./assets/some-image.68d77419.png";?
It result in <img src="./assets/some-image.68d77419.png"> and will work.

@hronro
Copy link
Contributor

hronro commented Apr 12, 2022

@Bigfish8

Unfortunately, const _imports_0$1 = "./assets/some-image.68d77419.png"; doesn't work.

"./assets/some-image.68d77419.png" is a relative path, unfortunately, it's not related to the library we build, it's related to the URL of the current web page, which can be a random string for single-page applications.

Using an absolute path does not work either. Absolute path generation depends on the value of Public Base Path (or output.publicPath in webpack), which can be different when different people using the library we build. It is not possible for us to determine the value of public base path at the time of building the library.

@Bigfish8
Copy link
Contributor

@hronro
Thank you very much for the detail explanation!
Because we dont know the library comes from which url,so we should use the information like import.meta.url.

@Matb85
Copy link

Matb85 commented May 2, 2022

really looking forward to it

@vaynevayne
Copy link

How do you use vite to build a component library? can you give me an example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: library mode p2-nice-to-have Not breaking anything but nice to have (priority) YAO Yet another option...
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants