Skip to content

Commit 0320801

Browse files
fix: allow inlining vite's cached dependencies (#6284)
1 parent 2673c3b commit 0320801

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

packages/vite-node/src/externalize.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ async function _shouldExternalize(
111111

112112
id = patchWindowsImportPath(id)
113113

114-
// always externalize Vite deps, they are too big to inline
115-
if (options?.cacheDir && id.includes(options.cacheDir)) {
116-
return id
117-
}
118-
119114
const moduleDirectories = options?.moduleDirectories || ['/node_modules/']
120115

121116
if (matchExternalizePattern(id, moduleDirectories, options?.inline)) {
@@ -125,6 +120,12 @@ async function _shouldExternalize(
125120
return id
126121
}
127122

123+
// Unless the user explicitly opted to inline them, externalize Vite deps.
124+
// They are too big to inline by default.
125+
if (options?.cacheDir && id.includes(options.cacheDir)) {
126+
return id
127+
}
128+
128129
const isLibraryModule = moduleDirectories.some(dir => id.includes(dir))
129130
const guessCJS = isLibraryModule && options?.fallbackCJS
130131
id = guessCJS ? guessCJSversion(id) || id : id

test/vite-node/test/server.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,55 @@ describe('server correctly caches data', () => {
229229
expect(webFiles).toHaveLength(1)
230230
})
231231
})
232+
233+
describe('externalize', () => {
234+
describe('by default', () => {
235+
test('should externalize vite\'s cached dependencies', async () => {
236+
const vnServer = new ViteNodeServer({
237+
config: {
238+
root: '/',
239+
cacheDir: '/node_modules/.vite',
240+
},
241+
} as any, {})
242+
243+
const externalize = await vnServer.shouldExternalize('/node_modules/.vite/cached.js')
244+
expect(externalize).toBeTruthy()
245+
})
246+
})
247+
248+
describe('with server.deps.inline: true', () => {
249+
test('should not externalize vite\'s cached dependencies', async () => {
250+
const vnServer = new ViteNodeServer({
251+
config: {
252+
root: '/',
253+
cacheDir: '/node_modules/.vite',
254+
},
255+
} as any, {
256+
deps: {
257+
inline: true,
258+
},
259+
})
260+
261+
const externalize = await vnServer.shouldExternalize('/node_modules/.vite/cached.js')
262+
expect(externalize).toBeFalsy()
263+
})
264+
})
265+
266+
describe('with server.deps.inline including the cache dir', () => {
267+
test('should not externalize vite\'s cached dependencies', async () => {
268+
const vnServer = new ViteNodeServer({
269+
config: {
270+
root: '/',
271+
cacheDir: '/node_modules/.vite',
272+
},
273+
} as any, {
274+
deps: {
275+
inline: [/node_modules\/\.vite/],
276+
},
277+
})
278+
279+
const externalize = await vnServer.shouldExternalize('/node_modules/.vite/cached.js')
280+
expect(externalize).toBeFalsy()
281+
})
282+
})
283+
})

0 commit comments

Comments
 (0)