Skip to content

Commit

Permalink
fix: sideEffects: [] should work as sideEffects: false (#16152)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Mar 13, 2024
1 parent 6f77b2b commit f377a84
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions packages/vite/src/node/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit f377a84

Please sign in to comment.