Skip to content

Commit 7338ee3

Browse files
authored
docs: add multiple entries lib mode example (#18364)
1 parent f50d358 commit 7338ee3

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

docs/guide/build.md

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,15 @@ When you are developing a browser-oriented library, you are likely spending most
131131

132132
When it is time to bundle your library for distribution, use the [`build.lib` config option](/config/build-options.md#build-lib). Make sure to also externalize any dependencies that you do not want to bundle into your library, e.g. `vue` or `react`:
133133

134-
```js twoslash [vite.config.js]
134+
::: code-group
135+
136+
```js twoslash [vite.config.js (single entry)]
135137
import { resolve } from 'path'
136138
import { defineConfig } from 'vite'
137139

138140
export default defineConfig({
139141
build: {
140142
lib: {
141-
// Could also be a dictionary or array of multiple entry points
142143
entry: resolve(__dirname, 'lib/main.js'),
143144
name: 'MyLib',
144145
// the proper extensions will be added
@@ -160,6 +161,37 @@ export default defineConfig({
160161
})
161162
```
162163

164+
```js twoslash [vite.config.js (multiple entries)]
165+
import { resolve } from 'path'
166+
import { defineConfig } from 'vite'
167+
168+
export default defineConfig({
169+
build: {
170+
lib: {
171+
entry: {
172+
'my-lib': resolve(__dirname, 'lib/main.js'),
173+
secondary: resolve(__dirname, 'lib/secondary.js'),
174+
},
175+
name: 'MyLib',
176+
},
177+
rollupOptions: {
178+
// make sure to externalize deps that shouldn't be bundled
179+
// into your library
180+
external: ['vue'],
181+
output: {
182+
// Provide global variables to use in the UMD build
183+
// for externalized deps
184+
globals: {
185+
vue: 'Vue',
186+
},
187+
},
188+
},
189+
},
190+
})
191+
```
192+
193+
:::
194+
163195
The entry file would contain exports that can be imported by users of your package:
164196

165197
```js [lib/main.js]
@@ -179,7 +211,9 @@ dist/my-lib.umd.cjs 0.30 kB / gzip: 0.16 kB
179211

180212
Recommended `package.json` for your lib:
181213

182-
```json [package.json]
214+
::: code-group
215+
216+
```json [package.json (single entry)]
183217
{
184218
"name": "my-lib",
185219
"type": "module",
@@ -195,9 +229,7 @@ Recommended `package.json` for your lib:
195229
}
196230
```
197231

198-
Or, if exposing multiple entry points:
199-
200-
```json [package.json]
232+
```json [package.json (multiple entries)]
201233
{
202234
"name": "my-lib",
203235
"type": "module",
@@ -217,6 +249,8 @@ Or, if exposing multiple entry points:
217249
}
218250
```
219251

252+
:::
253+
220254
::: tip File Extensions
221255
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`.
222256
:::

0 commit comments

Comments
 (0)