You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Build as a library. `entry` is required since the library cannot use HTML as entry. `name` is the exposed global variable and is required when `formats` includes `'umd'` or `'iife'`. Default `formats` are `['es', 'umd']`, or `['es', 'cjs']`, if multiple entries are used. `fileName` is the name of the package file output, default `fileName` is the name option of package.json, it can also be defined as function taking the `format` and `entryName` as arguments.
168
+
Build as a library. `entry` is required since the library cannot use HTML as entry. `name` is the exposed global variable and is required when `formats` includes `'umd'` or `'iife'`. Default `formats` are `['es', 'umd']`, or `['es', 'cjs']`, if multiple entries are used.
169
+
170
+
`fileName` is the name of the package file output, which defaults to the `"name"` in `package.json`. It can also be defined as a function taking the `format` and `entryName` as arguments, and returning the file name.
171
+
172
+
If your package imports CSS, `cssFileName` can be used to specify the name of the CSS file output. It defaults to the same value as `fileName` if it's set a string, otherwise it also falls back to the `"name"` in `package.json`.
Copy file name to clipboardexpand all lines: docs/guide/build.md
+29-1
Original file line number
Diff line number
Diff line change
@@ -200,7 +200,12 @@ import Bar from './Bar.vue'
200
200
export { Foo, Bar }
201
201
```
202
202
203
-
Running `vite build` with this config uses a Rollup preset that is oriented towards shipping libraries and produces two bundle formats: `es` and `umd` (configurable via `build.lib`):
203
+
Running `vite build` with this config uses a Rollup preset that is oriented towards shipping libraries and produces two bundle formats:
204
+
205
+
-`es` and `umd` (for single entry)
206
+
-`es` and `cjs` (for multiple entries)
207
+
208
+
The formats can be configured with the [`build.lib.formats`](/config/build-options.md#build-lib) option.
204
209
205
210
```
206
211
$ vite build
@@ -251,6 +256,29 @@ Recommended `package.json` for your lib:
251
256
252
257
:::
253
258
259
+
### CSS support
260
+
261
+
If your library imports any CSS, it will be bundled as a single CSS file besides the built JS files, e.g. `dist/my-lib.css`. The name defaults to `build.lib.fileName`, but can also be changed with [`build.lib.cssFileName`](/config/build-options.md#build-lib).
262
+
263
+
You can export the CSS file in your `package.json` to be imported by users:
264
+
265
+
```json {12}
266
+
{
267
+
"name": "my-lib",
268
+
"type": "module",
269
+
"files": ["dist"],
270
+
"main": "./dist/my-lib.umd.cjs",
271
+
"module": "./dist/my-lib.js",
272
+
"exports": {
273
+
".": {
274
+
"import": "./dist/my-lib.js",
275
+
"require": "./dist/my-lib.umd.cjs"
276
+
},
277
+
"./style.css": "./dist/my-lib.css"
278
+
}
279
+
}
280
+
```
281
+
254
282
::: tip File Extensions
255
283
If the `package.json` does not contain `"type": "module"`, Vite will generate different file extensions for Node.js compatibility. `.js` will become `.mjs` and `.cjs` will become `.js`.
Copy file name to clipboardexpand all lines: docs/guide/migration.md
+20
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,26 @@ From Vite 6, the modern API is used by default for Sass. If you wish to still us
32
32
33
33
To migrate to the modern API, see [the Sass documentation](https://sass-lang.com/documentation/breaking-changes/legacy-js-api/).
34
34
35
+
### Customize CSS output file name in library mode
36
+
37
+
In Vite 5, the CSS output file name in library mode was always `style.css` and cannot be easily changed through the Vite config.
38
+
39
+
From Vite 6, the default file name now uses `"name"` in `package.json` similar to the JS output files. If [`build.lib.fileName`](/config/build-options.md#build-lib) is set with a string, the value will also be used for the CSS output file name. To explicitly set a different CSS file name, you can use the new [`build.lib.cssFileName`](/config/build-options.md#build-lib) to configure it.
40
+
41
+
To migrate, if you had relied on the `style.css` file name, you should update references to it to the new name based on your package name. For example:
42
+
43
+
```json [package.json]
44
+
{
45
+
"name": "my-lib",
46
+
"exports": {
47
+
"./style.css": "./dist/style.css"// [!code --]
48
+
"./style.css": "./dist/my-lib.css"// [!code ++]
49
+
}
50
+
}
51
+
```
52
+
53
+
If you prefer to stick with `style.css` like in Vite 5, you can set `build.lib.cssFileName: 'style'` instead.
54
+
35
55
## Advanced
36
56
37
57
There are other breaking changes which only affect few users.
0 commit comments