-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Importing CSS files? #544
Comments
You mean this doesn't work on server ? |
oh right, i guess there'd need to be a way for the webpack config in next.config.js to work on both sides in order for this to work. |
I've got a kind of ballpark similar issue in that I just can't seem to get Next to play nicely with CSS or SASS. I have a Generally in React I import SASS files and have Webpack compile using style, css and sass loaders. I've attempted to add these to the My module.exports = {
webpack: (config, { dev }) => {
config.module.rules.push({ test: /\.scss$/, loader: ['style-loader', 'css-loader', 'sass-loader'] });
return config;
}
} Sorry if these questions sound dumb or I've missed something obvious in the docs that spell out the answers to them, but if anyone has a working example of importing / compiling SASS (or at the least CSS) into a component or a page with whatever's needed to add to the |
I'm using css-modules-require-hook |
@spacedragon Do you have an example how to integrate css-modules-require-hook with Next.js? I'm having problems with getting it working. |
I'm still having issues with getting SASS to compile if anyone could shed some light on how to do this or just import a CSS file within Next it would be appreciated (via a code example). |
Interesting that the README file has been updated to remove the SVG loader example and changed to say adding loaders for file like SVG, CSS and SASS is discouraged. I'm not sure why inlined CSS is OK but imported CSS isn't but I'm sure there's a good reason why. I'm just currently uncertain about the best strategy to handle none JS defined / inlined CSS and SASS. |
@MikeDigitize See comment on #627 and #638 . |
It's actually possible and pretty easy to process styles on server side. directly in node: require.extensions['.css'] = function(file) {
console.log(file.id)
return;
} via babel register: // from https://babeljs.io/docs/core-packages/babel-register/
require("babel-register")({
// Optional ignore regex - if any filenames **do** match this regex then they
// aren't compiled.
ignore: /regex/,
// Ignore can also be specified as a function.
ignore: function(filename) {
if (filename === '/path/to/es6-file.js') {
return false;
} else {
return true;
}
},
// Optional only regex - if any filenames **don't** match this regex then they
// aren't compiled
only: /my_es6_folder/,
// Setting this will remove the currently hooked extensions of .es6, `.es`, `.jsx`
// and .js so you'll have to add them back if you want them to be used again.
extensions: [".es6", ".es", ".jsx", ".js"]
}); via webpack loaders:
I personally use isomorphic-style-loader as it allows me to inline critical CSS while rendering on server. Hot reloading and other DX related stuff works too. I'm not really fan of CSS in JS as it complicates using 3rd party components and and somehow takes away the C from CSS. |
@viktorbezdek Have you successfully used isomorphic-style-loader with next.js? |
@noeljackson Not really, but I intend to. Next.js looks promising and could save me lot of time if I make it work. Will look into it in next one or two weeks and submit pull request if I'm successful. |
@viktorbezdek I will put a bounty on this one, as it's really crucial to a project I'm working on. I know I'm not inept, but I do not understand how to debug the babel transformations enough to figure this out. I've tried permutation of these ideas and nothing works 100%. I have been able to get raw-loader to pull in a data-encoded stylesheet using babel-plugin-webpack-loaders, but none of the style loaders work. Thanks for chiming in! :) Much appreciated. |
Is there a solution to this? I would like to see a solution so I don't have to include css globally. |
FWIW, I've just been putting my CSS files in the |
There will be a solution. I just didn't manage to finish it. I've locally very first prototype which seems to work, however it needs few hours to be finished. I'm pretty sure I'll be done after weekend. Stay tuned. |
@matthewmueller You are using CSS modules? |
@viktorbezdek Thanks for working on this! CSS Modules support (or similar) is important for this project IMO. Styled jsx, is ok for simple situations but is hard to read for heavily styled components. Would a babel plugin like this be an option (server side as well)? https://github.com/gajus/babel-plugin-react-css-modules I tried to get this to work but but no luck :/ |
I got CSS modules sort of working with The downside is you have to kill the server & restart it every time you make a change to the CSS. |
Some React components expose default styling though a import 'rc-slider/assets/index.css'; It sure is possible to copy paste this stylesheet in the |
The problem is that those CSS files introduce global effects. We need to be able to capture the CSS and put it inside the React lifecycle, so that it gets unmounted, server-rendered, etc. Many libraries do that, but I don't think it's a good pattern. |
I'm not familiar with Zeit Next internals, but could some static analysis of the |
We could, but it would be really strange. Similar to something that's outside of |
// Figured I'd share this for anyone else Well... I just spent a bit too much time trying to hack CSS in here, BUT I landed on a solution that works (for me). Admittedly, it's a hack, but hot reloading works and so does server-side building. Using (shudder) gulp, with this gulpfile (https://gist.github.com/auser/25e88e39d83413773c01f4c000ec2806) all Hope this helps someone else until we can get true postcss support in next. |
Thanks for the hack @auser, I've been looking at the webpack config all day with no luck! Edit: |
Yes and no... I just use the |
Seems like that is currently the best solution, using gulp to compile css file and build it inline or just even in /static if you don't mind not having hot reloading. |
How do you include more than one external css?
|
@CHarnel try |
@almeynman getting this:
|
@CHarnel try putting both in template string |
@CHarnel try this approach |
i try to put those css in one js file and export it
and then just
|
With https://github.com/sheerun/extracted-loader I've created you can use ExtractTextPlugin for both development and production, no need for different html in development or injecting css into js. |
@comus I used your approach, works good, thank you. |
@sheerun nice one ,thank you |
I've submitted even more comprehensive example to next.js: |
This used to work before nextjs v4
What's the reason for using this aproach to load jsx global styles? |
I've made myself a solution based on Other than that you can write |
@nikolakanacki official support will be introduced very soon 🙏 #3578 |
@timneutkens I've seen this one, is there an estimation on "very soon"? I see it's in canary already. The main problem behind these issues I think is that there is a lot of "magic" going on in the background behind production and dev builds, with hot reloading and stuff... One could probably read the source and see how things are assembled together, but it would be awesome if someone who built it wrote some docs on that, I think more people could contribute. |
@nikolakanacki very soon 🙏 🤐
The plugins that I wrote for next v5 already do this, they'll be open sourced soon 👍 Regarding writing docs on the internals, we're planning to document how everything works after v5 is out 🙏 |
@timneutkens Thank you! |
@timneutkens thank you for the update, please post an update here when this thing lands! |
Any news on this? |
I'm not sure what other news you're expecting 🤔 This was released in Next.js v5. Also, the mentioned PR was merged and this issue is closed. |
just create /static folder in root project, and put your file.css inside /static, then to Header html structure hock.
and then use className anywhere! |
Much comprehensive and smart..thank you for this..I have been searching this kind of solution all day.. |
Create a 'public' folder in the root of your application(where package.json file is located). |
Support is being added via #8626! |
Sometimes it's nice to break out your CSS into a separate
.css
file. I've tried to do the following:Then in the index.js, I've tried to do:
And in next.config.js:
But unfortunately, it keeps giving me:
Seems like it's not resolving to the right place for some reason, the local
component.js
works though viaimport component from './component.js'
, so I'm not sure what's going on here.The text was updated successfully, but these errors were encountered: