Unverified Commit ce234fb9 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Add error for styled-jsx in Server Component (#46482)

Add specific error for the `styled-jsx` import that's implicitly added
to the file when using `<style jsx>`.

Before

![image](https://user-images.githubusercontent.com/25056922/221547281-7b25a979-ee03-4d29-97c8-a038b9ebb214.png)

After

![image](https://user-images.githubusercontent.com/25056922/221547152-ed122163-93e5-48ae-bf95-484388f462ea.png)

Fixes NEXT-669

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/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`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
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`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 86c81d29
...@@ -35,14 +35,21 @@ export function getNextInvalidImportError( ...@@ -35,14 +35,21 @@ export function getNextInvalidImportError(
let invalidImportMessage = '' let invalidImportMessage = ''
if (firstExternalModule) { if (firstExternalModule) {
let formattedExternalFile = const firstExternalPackageName =
firstExternalModule.resource.split('node_modules') firstExternalModule.resourceResolveData?.descriptionFileData?.name
formattedExternalFile =
formattedExternalFile[formattedExternalFile.length - 1] if (firstExternalPackageName === 'styled-jsx') {
invalidImportMessage += `\n\nThe error was caused by using 'styled-jsx' in '${importTrace[0]}'. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.`
invalidImportMessage += `\n\nThe error was caused by importing '${formattedExternalFile.slice( } else {
1 let formattedExternalFile =
)}' in '${importTrace[0]}'.` firstExternalModule.resource.split('node_modules')
formattedExternalFile =
formattedExternalFile[formattedExternalFile.length - 1]
invalidImportMessage += `\n\nThe error was caused by importing '${formattedExternalFile.slice(
1
)}' in '${importTrace[0]}'.`
}
} }
return new SimpleWebpackError( return new SimpleWebpackError(
......
...@@ -70,7 +70,7 @@ createNextDescribe( ...@@ -70,7 +70,7 @@ createNextDescribe(
"app/comp2.js "app/comp2.js
'client-only' cannot be imported from a Server Component module. It should only be used from a Client Component. 'client-only' cannot be imported from a Server Component module. It should only be used from a Client Component.
The error was caused by importing 'styled-jsx/style.js' in 'app/comp2.js'. The error was caused by using 'styled-jsx' in 'app/comp2.js'. It only works in a Client Component but none of its parents are marked with \\"use client\\", so they're Server Components by default.
Import trace for requested module: Import trace for requested module:
app/comp2.js app/comp2.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