Skip to content

Commit

Permalink
fix: respect "sideEffects: true" in package.json
Browse files Browse the repository at this point in the history
We can assume a package has side effects in this case. This is basically a way
to force Rollup to always include the package when imported.
  • Loading branch information
aleclarson committed Oct 12, 2021
1 parent 386ca79 commit a76919c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -583,7 +583,7 @@ export function tryOptimizedResolve(

export interface PackageData {
dir: string
hasSideEffects: (id: string) => boolean
hasSideEffects: (id: string) => boolean | 'no-treeshake'
webResolvedImports: Record<string, string | undefined>
nodeResolvedImports: Record<string, string | undefined>
setResolvedCache: (key: string, entry: string, targetWeb: boolean) => void
Expand Down Expand Up @@ -621,13 +621,19 @@ export function resolvePackageData(
function loadPackageData(pkgPath: string, cacheKey = pkgPath) {
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)) {
hasSideEffects = createFilter(sideEffects, null, { resolve: pkgDir })
const filter = createFilter(sideEffects, null, { resolve: pkgDir })
hasSideEffects = (id) => filter(id) && 'no-treeshake'
} else {
// Statically analyze each module for side effects.
hasSideEffects = () => true
}

Expand Down

0 comments on commit a76919c

Please sign in to comment.