Skip to content

Commit b055c03

Browse files
committed
Allow additional packages to be externalized
1 parent ccc4f59 commit b055c03

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules/typescript/lib"
3+
}

src/index.ts

+38
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ interface UserOptions {
77
deps: boolean,
88
devDeps: boolean,
99
except: Array<string | RegExp>,
10+
/**
11+
* Additional dependencies to externalize.
12+
*
13+
* @example
14+
*
15+
* ```ts
16+
* externalizeDeps({
17+
* include: [
18+
* /^unlisted-dep(?:\/.*)?$/,
19+
* ],
20+
* })
21+
* ```
22+
*
23+
* @default []
24+
*/
25+
include: Array<string | RegExp>,
1026
nodeBuiltins: boolean,
1127
optionalDeps: boolean,
1228
peerDeps: boolean,
@@ -38,6 +54,12 @@ const parseFile = (file: string) => {
3854
* // Or match patterns with regular expressions.
3955
* /^@some\/obscure(?:\/.+)?$/,
4056
* ],
57+
* include: [
58+
* // Match exact values with strings.
59+
* '@some/obscure/dependency',
60+
* // Or match patterns with regular expressions.
61+
* /^@some\/obscure(?:\/.+)?$/,
62+
* ],
4163
* nodeBuiltins: true,
4264
* optionalDeps: true,
4365
* peerDeps: true,
@@ -52,6 +74,7 @@ export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
5274
deps: true,
5375
devDeps: false,
5476
except: [],
77+
include: [],
5578
nodeBuiltins: true,
5679
optionalDeps: true,
5780
peerDeps: true,
@@ -116,6 +139,7 @@ export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
116139
}
117140

118141
const depMatchers = Array.from(externalDeps)
142+
119143
const isException = (id: string) => {
120144
return optionsResolved.except.some((exception) => {
121145
if (typeof exception === 'string') {
@@ -126,6 +150,16 @@ export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
126150
})
127151
}
128152

153+
const isIncluded = (id: string) => {
154+
return optionsResolved.include.some((included) => {
155+
if (typeof included === 'string') {
156+
return included === id
157+
}
158+
159+
return included.test(id)
160+
})
161+
}
162+
129163
return {
130164
build: {
131165
rollupOptions: {
@@ -134,6 +168,10 @@ export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
134168
return false
135169
}
136170

171+
if (isIncluded(id)) {
172+
return true
173+
}
174+
137175
return depMatchers.some((depMatcher) => depMatcher.test(id))
138176
},
139177
},

test/entry.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ import { defineConfig } from 'vite'
77
import esbuild from 'esbuild'
88
// @ts-ignore
99
import rollup from 'rollup'
10+
// @ts-ignore
11+
import hello from 'unlisted-dep'
1012

11-
console.log(path, path2, resolve, chalk, esbuild, defineConfig, rollup)
13+
console.log(path, path2, resolve, chalk, esbuild, defineConfig, rollup, hello)

test/vite.config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export default defineConfig({
1616
nodeResolve(),
1717
externalizeDeps({
1818
devDeps: true,
19+
include: [
20+
/^unlisted-dep(?:\/.*)?$/,
21+
],
1922
useFile: './test/test.json',
2023
}),
2124
],

0 commit comments

Comments
 (0)