-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Global CSS cannot be imported from files other than your Custom <App>. #3803
Comments
hey @vsavkin do you guys have any news on this? |
Got the similar error when selecting sass for styling while generating a component- this is what i had to do to get it working-
code generator should be updated to updated the generate code. I am happy to create a PR ,but I was not able to find the correct file to make the change inside packages/next folder. @jaysoo ? |
Hi @danielquintero , thanks for filing an issue! Hi @msreekm ! Would you like help creating a PR? |
@mandarini happy to create a PR , do you know which files have generate component template for next.js? |
I see the code ais already in the latest build ,not sure why it is no working? @jaysoo ? <%= style !== 'styled-jsx' ? |
|
Hey folks, after a quick chat with @mandarini we've decided to do the following:
|
@jaysoo @mandarini Thank you for the quick response, that sounds great! |
That would be great! Thanks! |
PR for the first part is ready -- the second change for schematics isn't required but more of a quality of life change. For those that want a workaround before the next release, you can use this in your app's const { join } = require('path');
const { appRootPath } = require('@nrwl/workspace/src/utils/app-root');
const { workspaceLayout } = require('@nrwl/workspace/src/core/file-utils');
function regexEqual(x, y) {
return (
x instanceof RegExp &&
y instanceof RegExp &&
x.source === y.source &&
x.global === y.global &&
x.ignoreCase === y.ignoreCase &&
x.multiline === y.multiline
);
}
module.exports = {
webpack: (config) => {
// Include workspace libs in css/sass loaders
const includes = [join(appRootPath, workspaceLayout().libsDir)];
const nextCssLoaders = config.module.rules.find(
(rule) => typeof rule.oneOf === 'object'
);
// webpack config is not as expected
if (!nextCssLoaders) return config;
/*
* 1. Modify css loader to enable module support for workspace libs
*/
const nextCssLoader = nextCssLoaders.oneOf.find(
(rule) =>
rule.sideEffects === false && regexEqual(rule.test, /\.module\.css$/)
);
// Might not be found if Next.js webpack config changes in the future
if (nextCssLoader) {
nextCssLoader.issuer.or = nextCssLoader.issuer.and
? nextCssLoader.issuer.and.concat(includes)
: includes;
delete nextCssLoader.issuer.and;
}
/*
* 2. Modify sass loader to enable module support for workspace libs
*/
const nextSassLoader = nextCssLoaders.oneOf.find(
(rule) =>
rule.sideEffects === false &&
regexEqual(rule.test, /\.module\.(scss|sass)$/)
);
// Might not be found if Next.js webpack config changes in the future
if (nextSassLoader) {
nextSassLoader.issuer.or = nextSassLoader.issuer.and
? nextSassLoader.issuer.and.concat(includes)
: includes;
delete nextSassLoader.issuer.and;
}
/*
* 3. Modify error loader to ignore css modules used by workspace libs
*/
const nextErrorCssModuleLoader = nextCssLoaders.oneOf.find(
(rule) =>
rule.use &&
rule.use.loader === 'error-loader' &&
rule.use.options &&
(rule.use.options.reason ===
'CSS Modules \u001b[1mcannot\u001b[22m be imported from within \u001b[1mnode_modules\u001b[22m.\n' +
'Read more: https://err.sh/next.js/css-modules-npm' ||
rule.use.options.reason ===
'CSS Modules cannot be imported from within node_modules.\nRead more: https://err.sh/next.js/css-modules-npm')
);
// Might not be found if Next.js webpack config changes in the future
if (nextErrorCssModuleLoader) {
nextErrorCssModuleLoader.exclude = includes;
}
return config;
},
}; |
thank you @jaysoo for working on this! |
This works, but please keep in mind that the schematics for generating the react:lib still need some tweaks:
Thanks! |
Accidentally closed the issue from the PR merge. Keeping this open until we get the schematics change in as well. @areknow We are changing the schematics as well, so by default they will generate with css modules (which are better IMO), and you can pass |
Love that! Thanks @jaysoo What version can we expect the changes in? |
This will come in 10.4, which will land before American Thanksgiving. We'll cut a beta release before stable. |
workspace lib use importing CSS required by a third party component, you can do so in your component. For example: // components/ExampleDialog.js
import { useState } from 'react'
import { Dialog } from '@reach/dialog'
import '@reach/dialog/styles.css'
function ExampleDialog(props) {
const [showDialog, setShowDialog] = useState(false)
const open = () => setShowDialog(true)
const close = () => setShowDialog(false)
return (
<div>
<button onClick={open}>Open Dialog</button>
<Dialog isOpen={showDialog} onDismiss={close}>
<button className="close-button" onClick={close}>
<VisuallyHidden>Close</VisuallyHidden>
<span aria-hidden>×</span>
</button>
<p>Hello there. I am a dialog</p>
</Dialog>
</div>
)
} But there is a bug, error log: I moved |
This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. |
this is still an issue for my team, especially with next-sass breaking in new 10.1.X builds |
seems like there are a few different issues floating around in this thread 👀 firstly, using a react lib with css modules inside a next.js app. From what I understand, that's what the original issue is about. secondly, using sass modules. Same thing here, really. In my attempts locally, sass modules work in the same way as css modules. So once again, if someone have specific steps that reproduce the issue - please let us know! thirdly, importing "global" css from |
I see that this issue is closed, but there is a pending item to review the use of React component libraries which are not using CSS modules. Where can the status of that be tracked? |
This error happens againpackage.json
Node version v14.16.1 |
hey @Armer7 , thanks for reporting this! i pulled down the repo from your message, but running |
It's strange, but after I pulled down my repo and run npm install now it works. I will look when I will create new nx project again and if I will have mistake I will remove folder node_module and I will reinstall packages. |
Hi @kirjai. I checked. Now all good. Maybe It was failure when creating. Although not. When creating other versions were used than in the repository |
@Armer7 great, so if everything's good, then i'll close the issue |
This issue still exists in part, so I don't think it should be closed. I am using a fresh install of the latest nx, and when I generate a page with |
This was re-introduced in NextJS v11.1.1 (seemingly only for Windows devs) and fixed in 11.1.2; see here for details: vercel/next.js#28579 |
Hey guys (@kirjai ?),
I'm getting: |
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context. |
## Current Behavior Given a generated NextJs app that imports a generated shared react lib with a default component that uses either css or cssModule imports fails throwing an error.
Expected Behavior
Given a default NextJs app that imports a shared react lib with a default component that uses either css or cssModule imports should just work, no need to adapt
next.config.js
as both imports are supported by default in NextJs.Is this a regression? Not sure, I am able to reproduce with brand new workspace and latest version of every plugin.
Steps to Reproduce
nx serve test-bug
xxx.module.css
and it's corresponding importFailure Logs
Environment
nx : Not Found
@nrwl/angular : Not Found
@nrwl/cli : 10.2.1
@nrwl/cypress : 10.2.1
@nrwl/eslint-plugin-nx : 10.2.1
@nrwl/express : Not Found
@nrwl/jest : 10.2.1
@nrwl/linter : 10.2.1
@nrwl/nest : Not Found
@nrwl/next : 10.2.1
@nrwl/node : Not Found
@nrwl/react : 10.2.1
@nrwl/schematics : Not Found
@nrwl/tao : 10.2.1
@nrwl/web : 10.2.1
@nrwl/workspace : 10.2.1
typescript : 3.9.7
The text was updated successfully, but these errors were encountered: