vite: library mode can't extract static assets
Describe the bug
while in library mode, exec yarn build, the static assets is inline into css file, but what i want is to remain assets alone.
Reproduction
- yarn create @vitejs/app vite-demo --template react
- replace
imgwithdivinApp.tsx, and removesrcattribute
<div className="App-logo" />
- add
background-image: url(facivon.svg)for.App-logoclassname inApp.css
.App-logo {
height: 40vmin;
pointer-events: none;
background-image: url(t.jpeg);
}
- edit
vite.config.json
import { defineConfig } from 'vite';
import path from 'path';
import reactRefresh from '@vitejs/plugin-react-refresh';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [reactRefresh()],
build: {
assetsInlineLimit: 0,
lib: {
entry: path.resolve(__dirname, './src/main.tsx'),
name: 'demo',
},
},
});
-
exec
yarn build -
output
dist
├── style.css
├── vite-demo.es.js
└── vite-demo.umd.js
what i expect is that there is a favicon.svg file in dist directory
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 76
- Comments: 32 (3 by maintainers)
Commits related to this issue
- feat: build library new option - emitAssets #4454 #3295 — committed to torsteinringnes/vite by torsteinringnes 5 years ago
- Merge branch 'vitejs:main' into fix-#3295 — committed to torsteinringnes/vite by torsteinringnes 5 years ago
- Merge pull request #1 from torsteinringnes/fix-#3295 Fix #3295 — committed to torsteinringnes/vite by torsteinringnes 5 years ago
- Merge branch 'vitejs:main' into fix-#3295 — committed to torsteinringnes/vite by torsteinringnes 5 years ago
- Merge branch 'vitejs:main' into fix-#3295 — committed to torsteinringnes/vite by torsteinringnes 5 years ago
- Try to use vite-plugin-lib-assets to avoid wasm inlining. Vite lib mode forces assets inlining, regardless of size. This is directly stated in the doc (https://vitejs.dev/guide/build#library-mode) an... — committed to Senryoku/smol-string by Senryoku 2 years ago
- fix(fonts): extract fonts to separate stylesheet Vite automatically base64-encodes and inlines fonts when building in library mode. This leads to pretty big CSS files, while at the same time not all ... — committed to digitalservicebund/ris-ui by andreasphil 2 years ago
- Output static assets in navigator Vite’s Library mode can’t export static assets and defaults to Base64. So we currently need a plugin to handle that: https://github.com/vitejs/vite/issues/3295 Note... — committed to readium/ts-toolkit by JayPanoz 2 years ago
- :hammer: [#724] Set up npm package build with Vite Slowly preparing to swap over the ESM build for the NPM package from CRA/webpack to Vite. This configuration was hand-crafted. Initially the librar... — committed to open-formulieren/open-forms-sdk by sergei-maertens 2 years ago
- :hammer: [#724] Set up npm package build with Vite Slowly preparing to swap over the ESM build for the NPM package from CRA/webpack to Vite. This configuration was hand-crafted. Initially the librar... — committed to open-formulieren/open-forms-sdk by sergei-maertens 2 years ago
- :hammer: [#724] Set up npm package build with Vite Slowly preparing to swap over the ESM build for the NPM package from CRA/webpack to Vite. This configuration was hand-crafted. Initially the librar... — committed to open-formulieren/open-forms-sdk by sergei-maertens 2 years ago
Any updates?
Can we have an option that enables emitting assets separately in lib mode?
I wrote a plugin to extract static assets in library mode: vite-plugin-lib-assets
usage:
Any updates on this?
I think the suggestion from the Vite team would be to use a
publicdirectory.Say you have the file
/public/cool-font.woffthat was included in your Vue component library:Configured like this, the file will not be inline, but there’s one other catch. The
publicdirectory needs to be published with the package. So thepackage.jsonwould be updated:Now whatever environment the
cool-packageis included in can handle packing/vite/rollup of the static assets however it needs too.I have a solution: move assets to another package
I would really like this as well. Curious, why is this not supported in “lib” mode? Must be a reason.
This fixes the symptom but not the problem. As library author, you want to be able to provide e.g. components alongside with static assets. This workaround requires you to include the static assets in the app where you implement the components.
Please, allow this for ‘es’ libs. Thanks 😃 . Bundle with svg files inlined , why?
Anyone? Why is there a difference from standard, when using lib mode?
This problem has been bothering me for a long time. Is there any update?
Initially i “downvoted” this answer since i could not get it to work ATT. Since there seem to be no interest to look into the “main topic”, i gave this a try again. Could have been some old version i was using that caused it to not work then. Seem to be working now and i will test it some more 😄
Big thanks for the suggestion!!!
This might not be related to this, but what about web workers? I’m unable to use web workers in library mode as well. It builds fine, but doesn’t bundle the worker with it.
I was using library mode so I could get
iifeandumdbuilds, but I still wanted the static assets to be separate from the bundle.I ended up digging into the
rollupOptionsinstead, myvite.config.jsis similar to this:Nice! Static resources will not be converted to BASE64, but converted to require relative path reference, the problem is perfectly solved.
It should be noted that when using the package by
@laynezh/vite-plugin-lib-assets, your app needs to support the related loaders or plugins about the used static resources.+1
This would also be cool for json files.
We also have a use case for this.
This comment still works to hack it in: https://github.com/vitejs/vite/issues/3295#issuecomment-924063684
However, we also had to tweak the
assetFileNamesoption in this location: https://github.com/vitejs/vite/blob/8f109a67bd54b0eb7679fe1fafc56e03a247b768/packages/vite/src/node/build.ts#L619To make it respect the
assetsDiroption properly:Would love to see this officially added. Massively helpful.
I suggest you to put your
t.jpeginto the/publicfolder (or any subfolder inside public directory, such as/public/assets/images/t.jpeg). This solution helped me with missing background image.Thanks for the answer by roydukkey,