Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect "sideEffects: true" in package.json #5257

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/vite/src/node/packages.ts
Expand Up @@ -96,10 +96,14 @@ export function loadPackageData(

const data = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
const pkgDir = path.dirname(pkgPath)

// When the "sideEffects" field is defined, we can assume modules
// in the package are either side effect-free or not, which means
// Rollup doesn't have to statically analyze the AST.
const { sideEffects } = data
let hasSideEffects: (id: string) => boolean
let hasSideEffects: (id: string) => boolean | 'no-treeshake'
if (typeof sideEffects === 'boolean') {
hasSideEffects = () => sideEffects
hasSideEffects = () => sideEffects && 'no-treeshake'
} else if (Array.isArray(sideEffects)) {
const finalPackageSideEffects = sideEffects.map((sideEffect) => {
/*
Expand All @@ -113,10 +117,12 @@ export function loadPackageData(
return `**/${sideEffect}`
})

hasSideEffects = createFilter(finalPackageSideEffects, null, {
const filter = createFilter(finalPackageSideEffects, null, {
resolve: pkgDir,
})
hasSideEffects = (id) => filter(id) && 'no-treeshake'
} else {
// Statically analyze each module for side effects.
hasSideEffects = () => true
}

Expand Down