Skip to content

Commit

Permalink
fix: allow inlining vite's cached dependencies (#6284)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswheeldon-peakon authored Sep 4, 2024
1 parent 2673c3b commit 0320801
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/vite-node/src/externalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
Expand Down
52 changes: 52 additions & 0 deletions test/vite-node/test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})

0 comments on commit 0320801

Please sign in to comment.