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

The styled-jsx integration relies on hoisting #10149

Closed
arcanis opened this issue Jan 18, 2020 · 9 comments · Fixed by #36585
Closed

The styled-jsx integration relies on hoisting #10149

arcanis opened this issue Jan 18, 2020 · 9 comments · Fixed by #36585

Comments

@arcanis
Copy link
Contributor

arcanis commented Jan 18, 2020

Bug report

Describe the bug

Next generates files that contain the following import statement:

import foo from 'styled-jsx/style';

Since the output files belong to the project that depends on Next rather than Next itself, the styled-jsx dependency needs to be listed in the user project. Based on #9325, I think this isn't what you're looking for.

Suggested fix

  1. Fix styled-jsx by:
    • adding a custom import path setting (it's currently hardcoded).
  2. Fix next by:
    • adding a new file (let's say styled-jsx.js) that contains module.exports = require('styled-jsx/style') (it would basically re-export its own dependency)
    • updating the Babel configuration to use next/styled-jsx inside the custom import path setting that we will have implemented in 1.

This solution isn't obtrusive, doesn't have breaking changes, and makes Next resilient to problems highlighted in #9325 (and, more importantly to me, makes the default create-next-app setup work with Yarn 2 😉).

Note that I likely won't have the resources to make the fix myself right now.

@Timer Timer added this to the 9.2.1 milestone Jan 21, 2020
@Timer
Copy link
Member

Timer commented Jan 21, 2020

Thanks for the ping! Marked to be investigated soon. 🙏

@Timer Timer modified the milestones: 9.2.1, 9.2.2 Jan 23, 2020
@Timer Timer modified the milestones: 9.2.2, 9.2.x Feb 18, 2020
giuseppeg added a commit to giuseppeg/next.js that referenced this issue May 14, 2020
Introduces a new plugin option to configure the module that the transpiled files should import. 
Should unblock vercel#10149 and support for yarn v2 once vercel#10149 is complete.

https://github.com/zeit/styled-jsx/releases/tag/v3.3.0
timneutkens pushed a commit that referenced this issue May 14, 2020
Introduces a new plugin option to configure the module that the transpiled files should import. 
Should unblock #10149 and support for yarn v2 once #10149 is complete.

https://github.com/zeit/styled-jsx/releases/tag/v3.3.0
chibicode pushed a commit to chibicode/next.js that referenced this issue May 21, 2020
Introduces a new plugin option to configure the module that the transpiled files should import. 
Should unblock vercel#10149 and support for yarn v2 once vercel#10149 is complete.

https://github.com/zeit/styled-jsx/releases/tag/v3.3.0
rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
Introduces a new plugin option to configure the module that the transpiled files should import. 
Should unblock vercel#10149 and support for yarn v2 once vercel#10149 is complete.

https://github.com/zeit/styled-jsx/releases/tag/v3.3.0
@therazor
Copy link

therazor commented Sep 29, 2020

judging by the linked issues this should be fixed in v9.5.3 and above, but I'm having the same problem on that version. Is there anything missing still?

steps to repro:

yarn create next-app nextjs-starter--example "https://github.com/vercel/next-learn-starter/tree/master/learn-starter"
cd nextjs-starter
yarn set version berry
yarn dev

@masihjahangiri
Copy link

@arcanis @therazor The issue is still unresolved. I get this error:

- Required package: styled-jsx (via "styled-jsx/style")
- Required by: /app/components/shared-component/FormsInputsWithLabel.tsx
- event - build page: /next/dist/pages/_error
- wait  - compiling...
- error - ./components/shared-component/FormsInputsWithLabel.tsx
- Module not found: Your application tried to access styled-jsx, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.

@masihjahangiri
Copy link

I solved my issue temporary with add nodeLinker: node-modules field to .yarnrc.yml and run yarn install again. I'm waiting for a full solution.

@merceyz
Copy link
Contributor

merceyz commented Oct 25, 2020

As the error says, you're trying to require styled-jsx without declaring it, so if you declare it, it would work again with PnP

@masihjahangiri
Copy link

@merceyz I'm tried to import styled-jsx but I can't find any way to import it to my component. Do you have any idea that how to import it to a next.js component? I think in build time babel handle importing.

@jordinl
Copy link

jordinl commented Jan 18, 2021

Any thoughts on fixing this?

I've created an example application that reproduces this issue:

See: https://github.com/jordinl83/yarn-styled-jsx-bug

To reproduce, just run:

yarn
yarn dev

And navigate to localhost:3000. You should see the error:

Module not found: Your application tried to access styled-jsx, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.

Also, in branch with-styled-jsx-installed I tried adding styled-jsx as a dependency, which seems to have the unintended consequence of not generating the CSS at build time.

To reproduce:

git checkout with-styled-jsx-installed
yarn build
yarn start

Navigate to localhost:3000, open developer console and disable javascript. You'll notice that styles are not applied when reloading the page.

cc @timneutkens

@jordinl
Copy link

jordinl commented Jan 19, 2021

@timneutkens @Timer Added failing test for styled-jsx and PNP: #21320

Let me know if you would like me to attempt to fix it and what would be the best approach. I see @arcanis gave a recommendation in the description of this issue, so I could try that?

@styfle styfle modified the milestones: 11.x.x, 12.0.4 Nov 5, 2021
@timneutkens timneutkens removed this from the 12.0.5 milestone Nov 17, 2021
@kodiakhq kodiakhq bot closed this as completed in #36585 Apr 30, 2022
kodiakhq bot pushed a commit that referenced this issue Apr 30, 2022
When using pnpm / yarnPnP to install next.js, styled-jsx as dependency is not hoisted in the top level node_modules, it will fail when nodejs is trying to resolve `styled-jsx/style` from project directory. Re-export `styled-jsx/style` in next.js and let swc/babel plugin compile the import path it to `next/dist/shared/lib/styled-jsx`

Resolves #10149
Closes #21320
Closes #9325



Co-authored-by: Tim Neutkens <6324199+timneutkens@users.noreply.github.com>
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants