Unverified Commit c5c2c9d3 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix global css being marked as side effect free (#41481)

It’s possible that global CSS imports, that come from node_modules, are
marked as side effect free because of possible `sideEffects: false` in
the external package's package.json. In that case, we still need to
handle global CSS imports as side effects on the server because we need
to collect them and avoid tree-shaking for these modules.

You can take a look at the test case to see what exactly happened.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 0c2c48e5
...@@ -427,6 +427,8 @@ export const css = curry(async function css( ...@@ -427,6 +427,8 @@ export const css = curry(async function css(
loader({ loader({
oneOf: [ oneOf: [
markRemovable({ markRemovable({
// CSS imports have side effects, even on the server side.
sideEffects: true,
test: [regexCssGlobal, regexSassGlobal], test: [regexCssGlobal, regexSassGlobal],
use: require.resolve('next/dist/compiled/ignore-loader'), use: require.resolve('next/dist/compiled/ignore-loader'),
}), }),
......
...@@ -110,4 +110,13 @@ describe('app dir - rsc external dependency', () => { ...@@ -110,4 +110,13 @@ describe('app dir - rsc external dependency', () => {
} }
) )
}) })
it('should correctly collect global css imports and mark them as side effects', async () => {
await fetchViaHTTP(next.url, '/css/a').then(async (response) => {
const result = await resolveStreamResponse(response)
// It should include the global CSS import
expect(result).toMatch(/\.css/)
})
})
}) })
import 'global-css/style.css'
export default function Page() {
return <div>hello</div>
}
{
"name": "global-css",
"type": "module",
"sideEffects": false,
"exports": {
".": "./index.js",
"./style.css": "./style.css",
"./package.json": "./package.json"
}
}
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