Skip to content

Commit 5ac0fdf

Browse files
committed
Actually externalize optional deps
1 parent bf26c45 commit 5ac0fdf

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { existsSync, readFileSync } from 'fs'
2-
import { join } from 'path'
1+
import { existsSync, readFileSync } from 'node:fs'
2+
import { join } from 'node:path'
33
import type { Plugin } from 'vite'
44

55
interface UserOptions {
@@ -9,6 +9,10 @@ interface UserOptions {
99
useFile: string,
1010
}
1111

12+
const parseFile = (file: string) => {
13+
return JSON.parse(readFileSync(file).toString())
14+
}
15+
1216
export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
1317
const optionsResolved: UserOptions = {
1418
deps: true,
@@ -24,7 +28,7 @@ export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
2428
config: (_config, _env) => {
2529
if (existsSync(optionsResolved.useFile)) {
2630
const externalDeps = new Set<RegExp>()
27-
const { dependencies = {}, peerDependencies = {} } = JSON.parse(readFileSync(optionsResolved.useFile).toString())
31+
const { dependencies = {}, optionalDependencies = {}, peerDependencies = {} } = parseFile(optionsResolved.useFile)
2832

2933
if (optionsResolved.deps) {
3034
Object.keys(dependencies).forEach((dep) => {
@@ -34,6 +38,14 @@ export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
3438
})
3539
}
3640

41+
if (optionsResolved.optionalDeps) {
42+
Object.keys(optionalDependencies).forEach((dep) => {
43+
const depMatcher = new RegExp(`^${dep}(?:/.+)?$`)
44+
45+
externalDeps.add(depMatcher)
46+
})
47+
}
48+
3749
if (optionsResolved.peerDeps) {
3850
Object.keys(peerDependencies).forEach((dep) => {
3951
const depMatcher = new RegExp(`^${dep}(?:/.+)?$`)

0 commit comments

Comments
 (0)