|
1 | 1 | import { existsSync } from 'node:fs' |
2 | 2 | import { isNodeBuiltin, isValidNodeImport } from 'mlly' |
| 3 | +import { join } from 'pathe' |
3 | 4 | import type { DepsHandlingOptions } from './types' |
4 | 5 | import { slash } from './utils' |
5 | 6 |
|
@@ -109,35 +110,37 @@ async function _shouldExternalize( |
109 | 110 | if (options?.cacheDir && id.includes(options.cacheDir)) |
110 | 111 | return id |
111 | 112 |
|
112 | | - if (matchExternalizePattern(id, options?.inline)) |
| 113 | + const moduleDirectories = options?.moduleDirectories || ['/node_modules/'] |
| 114 | + |
| 115 | + if (matchExternalizePattern(id, moduleDirectories, options?.inline)) |
113 | 116 | return false |
114 | | - if (matchExternalizePattern(id, options?.external)) |
| 117 | + if (matchExternalizePattern(id, moduleDirectories, options?.external)) |
115 | 118 | return id |
116 | 119 |
|
117 | | - const isNodeModule = id.includes('/node_modules/') |
118 | | - const guessCJS = isNodeModule && options?.fallbackCJS |
| 120 | + const isLibraryModule = moduleDirectories.some(dir => id.includes(dir)) |
| 121 | + const guessCJS = isLibraryModule && options?.fallbackCJS |
119 | 122 | id = guessCJS ? (guessCJSversion(id) || id) : id |
120 | 123 |
|
121 | | - if (matchExternalizePattern(id, defaultInline)) |
| 124 | + if (matchExternalizePattern(id, moduleDirectories, defaultInline)) |
122 | 125 | return false |
123 | | - if (matchExternalizePattern(id, depsExternal)) |
| 126 | + if (matchExternalizePattern(id, moduleDirectories, depsExternal)) |
124 | 127 | return id |
125 | 128 |
|
126 | 129 | const isDist = id.includes('/dist/') |
127 | | - if ((isNodeModule || isDist) && await isValidNodeImport(id)) |
| 130 | + if ((isLibraryModule || isDist) && await isValidNodeImport(id)) |
128 | 131 | return id |
129 | 132 |
|
130 | 133 | return false |
131 | 134 | } |
132 | 135 |
|
133 | | -function matchExternalizePattern(id: string, patterns?: (string | RegExp)[] | true) { |
| 136 | +function matchExternalizePattern(id: string, moduleDirectories: string[], patterns?: (string | RegExp)[] | true) { |
134 | 137 | if (patterns == null) |
135 | 138 | return false |
136 | 139 | if (patterns === true) |
137 | 140 | return true |
138 | 141 | for (const ex of patterns) { |
139 | 142 | if (typeof ex === 'string') { |
140 | | - if (id.includes(`/node_modules/${ex}/`)) |
| 143 | + if (moduleDirectories.some(dir => id.includes(join(dir, id)))) |
141 | 144 | return true |
142 | 145 | } |
143 | 146 | else { |
|
0 commit comments