Skip to content

Commit 4662927

Browse files
committed
Allow exceptions
1 parent 4c5e480 commit 4662927

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,23 @@ Pass an object to `externalizeDeps` to override the default configuration.
4040
externalizeDeps({
4141
deps: true,
4242
devDeps: false,
43+
except: [],
4344
nodeBuiltins: true,
4445
optionalDeps: true,
4546
peerDeps: true,
4647
useFile: join(process.cwd(), 'package.json'),
4748
})
4849
```
50+
51+
To _include_ specific dependencies in your bundle, you can add exceptions with the `except` option.
52+
53+
```ts
54+
externalizeDeps({
55+
except: [
56+
// Match exact values with strings.
57+
'@some/obscure/dependency',
58+
// Or match patterns with regular expressions.
59+
/^@some\/obscure(?:\/.+)?$/,
60+
],
61+
})
62+
```

src/index.ts

+50-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Plugin } from 'vite'
66
interface UserOptions {
77
deps: boolean,
88
devDeps: boolean,
9+
except: Array<string | RegExp>,
910
nodeBuiltins: boolean,
1011
optionalDeps: boolean,
1112
peerDeps: boolean,
@@ -16,10 +17,41 @@ const parseFile = (file: string) => {
1617
return JSON.parse(readFileSync(file).toString())
1718
}
1819

20+
/**
21+
* Returns a Vite plugin to exclude dependencies from the bundle.
22+
*
23+
* @example
24+
*
25+
* ```ts
26+
* // vite.config.ts
27+
* import { defineConfig } from 'vite'
28+
* import { externalizeDeps } from 'vite-plugin-externalize-deps'
29+
*
30+
* export default defineConfig({
31+
* plugins: [
32+
* externalizeDeps({
33+
* deps: true,
34+
* devDeps: false,
35+
* except: [
36+
* // Match exact values with strings.
37+
* '@some/obscure/dependency',
38+
* // Or match patterns with regular expressions.
39+
* /^@some\/obscure(?:\/.+)?$/,
40+
* ],
41+
* nodeBuiltins: true,
42+
* optionalDeps: true,
43+
* peerDeps: true,
44+
* useFile: join(process.cwd(), 'package.json'),
45+
* }),
46+
* ],
47+
* })
48+
* ```
49+
*/
1950
export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
2051
const optionsResolved: UserOptions = {
2152
deps: true,
2253
devDeps: false,
54+
except: [],
2355
nodeBuiltins: true,
2456
optionalDeps: true,
2557
peerDeps: true,
@@ -83,12 +115,27 @@ export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
83115
})
84116
}
85117

118+
const depMatchers = Array.from(externalDeps)
119+
const isException = (id: string) => {
120+
return optionsResolved.except.some((exception) => {
121+
if (typeof exception === 'string') {
122+
return exception === id
123+
}
124+
125+
return exception.test(id)
126+
})
127+
}
128+
86129
return {
87130
build: {
88131
rollupOptions: {
89-
external: [
90-
...externalDeps.values(),
91-
],
132+
external: (id) => {
133+
if (isException(id)) {
134+
return false
135+
}
136+
137+
return depMatchers.some((depMatcher) => depMatcher.test(id))
138+
},
92139
},
93140
},
94141
}

0 commit comments

Comments
 (0)