vite icon indicating copy to clipboard operation
vite copied to clipboard

feat(vite): emitAssetsWithModule in library mode

Open yoyo930021 opened this issue 2 years ago • 1 comments

Description

Fixed https://github.com/vitejs/vite/issues/4454 Fixed https://github.com/vitejs/vite/issues/3295

We replace asset to base64 for lib mode by default, It can handle most of the contexts.
But it will add a lot of size in JS/CSS and disallow for CSP.

In this PR, we add a emitAssetsWithModule option.
When enable this option, vite will try to emit assets and generate original import code in JS or CSS.

For example:

// Code
import image from '@/assets/image/banner.jpg'
// Generate
import img from './assets/banner.87342es.jpg'
// Use bundler to resolve it. Ex: Vite, Webpack (url-loader)
/* Code */
body {
  background-image: url('../assets/image/banner.jpg');
}
/* Generate */
body {
  background-image: url('./assets/banner.87342es.jpg');
}
/* Use bundler to resolve it. Ex: Vite, Webpack (css-loader) */

The emitAssetsWithModule will try to generate module with assets. When enable this option, please use bundler in user project.

Additional context

No.


What is the purpose of this pull request?

  • [ ] Bug fix
  • [x] New Feature
  • [x] Documentation update
  • [ ] Other

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

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

yoyo930021 avatar Aug 18 '22 14:08 yoyo930021

@yoyo930021 Wouldn't it make more sense for lib mode to respect the assetsDir and assetsInlineLimit options instead of introducing a new lib specific emitAssetsWithModule?

jschill avatar Sep 13 '22 11:09 jschill

@yoyo930021 Wouldn't it make more sense for lib mode to respect the assetsDir and assetsInlineLimit options instead of introducing a new lib specific emitAssetsWithModule?

I don't think it's a good idea. This option is not applicable in all situations. We need option for disable or enable it.

When enable this option, it also respect assetsDir and assetsInlineLimit options.

yoyo930021 avatar Sep 23 '22 01:09 yoyo930021

I need this PR~

zjjjjjjjjjjd avatar Oct 18 '22 01:10 zjjjjjjjjjjd

This PR is a must have for a library, please merge?

ivan-palatov avatar Oct 19 '22 08:10 ivan-palatov

hows the progress on this?

mr-moto avatar Nov 22 '22 23:11 mr-moto

Unless I am missing a workaround, I can't use Vite for a new Vue3 component library because of this. I would adventure to go back to native Rollup, however rollup-plugin-vue has been archived for almost a year with this message:

This is no longer maintained. Use Vite and @vitejs/plugin-vue instead.

Would be nice if this is finally reviewed and merged.

cgodo avatar Nov 23 '22 21:11 cgodo

For those looking for a quick and dirty solution here is what you could do if you use pnpm (there are similar methods for other package managers):

  1. run pnpm patch vite@version_of_vite_you_are_using_currently
  2. open the path shown in your terminal
  3. open "files changed" section of this pr and using the search feature apply the relevant changes to the files in the project you opened. (Obviously, you do not need the changes in playground files or tests).
  4. once you done with your changes run pnpm patch-commit path_to_the_folder_with_changes
  5. now you could set emitAssetsWithModule: true in the lib section of your vite config. If you are using typescript, you might also need to add // @ts-expect-error declaration before that.

Although, I've found one issue with this: my assets are in src/assets/img and src/assets/svg folders and after building the import/export paths for those become /assets/*, which is wrong and isn't automatically resolvable, so I have to add a path alias in the project, that consumes that library for that to work. I do not know if it is something that I messed up or if it is an issue with this pr though and I do not have time to find out.

ivan-palatov avatar Dec 01 '22 03:12 ivan-palatov

For those looking for a quick and dirty solution here is what you could do if you use pnpm (there are similar methods for other package managers):

1. run `pnpm patch vite@version_of_vite_you_are_using_currently`

2. open the path shown in your terminal

3. open "files changed" section of this pr and using the search feature apply the relevant changes to the files in the project you opened. (Obviously, you do not need the changes in playground files or tests).

4. once you done with your changes run `pnpm patch-commit path_to_the_folder_with_changes`

5. now you could set `emitAssetsWithModule: true` in the lib section of your vite config. If you are using typescript, you might also need to add `// @ts-expect-error` declaration before that.

Although, I've found one issue with this: my assets are in src/assets/img and src/assets/svg folders and after building the import/export paths for those become /assets/*, which is wrong and isn't automatically resolvable, so I have to add a path alias in the project, that consumes that library for that to work. I do not know if it is something that I messed up or if it is an issue with this pr though and I do not have time to find out.

The PR is based on old vite main branch. It may not apply to the latest version.

yoyo930021 avatar Dec 01 '22 04:12 yoyo930021

@yoyo930021 I was using vite 3.1.8, so that could be the case, yeah.

ivan-palatov avatar Dec 02 '22 02:12 ivan-palatov

We need this, without this, we may choose to introduce rollup to replace vite lib mode

tjx666 avatar Dec 07 '22 19:12 tjx666

Is this going to be merged any time soon?

wststone avatar Dec 20 '22 12:12 wststone

In esbuild, the name is copy loader. Maybe we can rename option. https://esbuild.github.io/content-types/#copy

yoyo930021 avatar Feb 10 '23 08:02 yoyo930021

I rebase this PR from main branch.

yoyo930021 avatar Feb 10 '23 10:02 yoyo930021

We need this feature too, Vite is a great tool but this feature was a hole in the racket 🙏 Great work !

NathanVss avatar Mar 17 '23 14:03 NathanVss

Any updates? We would love this feature!

fatton139 avatar Oct 18 '23 04:10 fatton139