diff --git a/packages/vite/src/node/packages.ts b/packages/vite/src/node/packages.ts index 67b83eddfda8e9..5af667d2417cc9 100644 --- a/packages/vite/src/node/packages.ts +++ b/packages/vite/src/node/packages.ts @@ -176,21 +176,27 @@ export function loadPackageData(pkgPath: string): PackageData { if (typeof sideEffects === 'boolean') { hasSideEffects = () => sideEffects } else if (Array.isArray(sideEffects)) { - const finalPackageSideEffects = sideEffects.map((sideEffect) => { - /* - * The array accepts simple glob patterns to the relevant files... Patterns like *.css, which do not include a /, will be treated like **\/*.css. - * https://webpack.js.org/guides/tree-shaking/ - * https://github.com/vitejs/vite/pull/11807 - */ - if (sideEffect.includes('/')) { - return sideEffect - } - return `**/${sideEffect}` - }) + if (sideEffects.length <= 0) { + // createFilter always returns true if `includes` is an empty array + // but here we want it to always return false + hasSideEffects = () => false + } else { + const finalPackageSideEffects = sideEffects.map((sideEffect) => { + /* + * The array accepts simple glob patterns to the relevant files... Patterns like *.css, which do not include a /, will be treated like **\/*.css. + * https://webpack.js.org/guides/tree-shaking/ + * https://github.com/vitejs/vite/pull/11807 + */ + if (sideEffect.includes('/')) { + return sideEffect + } + return `**/${sideEffect}` + }) - hasSideEffects = createFilter(finalPackageSideEffects, null, { - resolve: pkgDir, - }) + hasSideEffects = createFilter(finalPackageSideEffects, null, { + resolve: pkgDir, + }) + } } else { hasSideEffects = () => null }