Unverified Commit bc28b2d9 authored by Sam Poder's avatar Sam Poder Committed by GitHub

[turbopack] Add test for sideEffects with optimizePackageImports (#96549)

Follow up to https://github.com/vercel/next.js/pull/96383. This test
passes with Webpack, failed with Turbopack before the change, and passes
now.
parent ff3a2cfa
export default function Root({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { Trap } from 'sidecar-lib'
import { Widget } from 'side-effectful-lib'
export default function Page() {
return (
<>
<p id="sidecar-lib-effects">
<Trap />
</p>
<p id="side-effectful-lib-effects">
<Widget />
</p>
</>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
optimizePackageImports: ['sidecar-lib', 'side-effectful-lib'],
},
}
module.exports = nextConfig
export { Widget } from './widget.js'
export { Unused } from './unused.js'
{
"name": "side-effectful-lib",
"type": "module",
"exports": "./index.js",
"sideEffects": true
}
// The package declares `sideEffects: true`, so this registration must survive tree shaking
// even though the package is assumed to be side effect free.
import { effects } from './medium.js'
effects.push('register.js')
import './register.js'
import { effects } from './medium.js'
export function Widget() {
return effects.length ? effects.join(',') : 'none'
}
export { Trap } from './trap.js'
export { Unused } from './unused.js'
// Not listed in the `sideEffects` glob of package.json, so this registration is expected
// to be dropped.
import { effects } from './medium.js'
effects.push('other.js')
{
"name": "sidecar-lib",
"type": "module",
"exports": "./index.js",
"sideEffects": [
"**/sidecar.js"
]
}
// Listed in the `sideEffects` glob of package.json, so this registration must survive
// tree shaking even though the package is assumed to be side effect free.
import { effects } from './medium.js'
effects.push('sidecar.js')
import './sidecar.js'
import './other.js'
import { effects } from './medium.js'
export function Trap() {
return effects.length ? effects.join(',') : 'none'
}
import { nextTestSetup } from 'e2e-utils'
// `optimizePackageImports` assumes the listed packages are side effect free. A package that
// declares `sideEffects` in its package.json must still win over that assumption, otherwise
// side-effect-only registration modules (the `sidecar` pattern used by react-focus-lock and
// react-remove-scroll) get tree shaken away. See https://github.com/vercel/next.js/issues/96333
describe('optimizePackageImports - sideEffects declared in package.json', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should keep a module matching the sideEffects glob of an optimized package', async () => {
const $ = await next.render$('/')
expect($('#sidecar-lib-effects').text()).toContain('sidecar.js')
})
it('should still drop a module not matching the sideEffects glob of an optimized package', async () => {
const $ = await next.render$('/')
expect($('#sidecar-lib-effects').text()).not.toContain('other.js')
})
it('should keep a module of an optimized package declaring sideEffects: true', async () => {
const $ = await next.render$('/')
expect($('#side-effectful-lib-effects').text()).toContain('register.js')
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment