From 03208017fb1b058f8f8710af3980e62d23186de9 Mon Sep 17 00:00:00 2001 From: Chris Wheeldon Date: Wed, 4 Sep 2024 10:08:29 +0100 Subject: [PATCH] fix: allow inlining vite's cached dependencies (#6284) --- packages/vite-node/src/externalize.ts | 11 +++--- test/vite-node/test/server.test.ts | 52 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/packages/vite-node/src/externalize.ts b/packages/vite-node/src/externalize.ts index a447d6c94032..47abaefa27d9 100644 --- a/packages/vite-node/src/externalize.ts +++ b/packages/vite-node/src/externalize.ts @@ -111,11 +111,6 @@ async function _shouldExternalize( id = patchWindowsImportPath(id) - // always externalize Vite deps, they are too big to inline - if (options?.cacheDir && id.includes(options.cacheDir)) { - return id - } - const moduleDirectories = options?.moduleDirectories || ['/node_modules/'] if (matchExternalizePattern(id, moduleDirectories, options?.inline)) { @@ -125,6 +120,12 @@ async function _shouldExternalize( return id } + // Unless the user explicitly opted to inline them, externalize Vite deps. + // They are too big to inline by default. + if (options?.cacheDir && id.includes(options.cacheDir)) { + return id + } + const isLibraryModule = moduleDirectories.some(dir => id.includes(dir)) const guessCJS = isLibraryModule && options?.fallbackCJS id = guessCJS ? guessCJSversion(id) || id : id diff --git a/test/vite-node/test/server.test.ts b/test/vite-node/test/server.test.ts index f6267f7bdc31..1b8a480677ba 100644 --- a/test/vite-node/test/server.test.ts +++ b/test/vite-node/test/server.test.ts @@ -229,3 +229,55 @@ describe('server correctly caches data', () => { expect(webFiles).toHaveLength(1) }) }) + +describe('externalize', () => { + describe('by default', () => { + test('should externalize vite\'s cached dependencies', async () => { + const vnServer = new ViteNodeServer({ + config: { + root: '/', + cacheDir: '/node_modules/.vite', + }, + } as any, {}) + + const externalize = await vnServer.shouldExternalize('/node_modules/.vite/cached.js') + expect(externalize).toBeTruthy() + }) + }) + + describe('with server.deps.inline: true', () => { + test('should not externalize vite\'s cached dependencies', async () => { + const vnServer = new ViteNodeServer({ + config: { + root: '/', + cacheDir: '/node_modules/.vite', + }, + } as any, { + deps: { + inline: true, + }, + }) + + const externalize = await vnServer.shouldExternalize('/node_modules/.vite/cached.js') + expect(externalize).toBeFalsy() + }) + }) + + describe('with server.deps.inline including the cache dir', () => { + test('should not externalize vite\'s cached dependencies', async () => { + const vnServer = new ViteNodeServer({ + config: { + root: '/', + cacheDir: '/node_modules/.vite', + }, + } as any, { + deps: { + inline: [/node_modules\/\.vite/], + }, + }) + + const externalize = await vnServer.shouldExternalize('/node_modules/.vite/cached.js') + expect(externalize).toBeFalsy() + }) + }) +})