[英]Next.js - React SSR Cannot find module './page.scss' when rendering on the server (client side rendered works fine after saving the file)
When you're using the create-react-app
package you're able to have .scss
-files compiled into .css
-files as you type in them. 当你使用
create-react-app
包你能有.scss
-files编译成.css
,你在其中键入-files。 You can then do import './Header.css';
然后你可以
import './Header.css';
in your React component files and the styles are applied. 在您的React组件文件中并应用样式。 It's easy to use your dev-tools and see where the styles are coming from.
使用您的开发工具很容易,看看样式的来源。
Next.js tried to get everyone to use Styled-JSX to have your stylesheets inline inside your JSX files, similar to how web components (or Polymer) do it. Next.js试图让每个人都使用Styled-JSX将样式表内联到JSX文件中,类似于Web组件(或Polymer)的工作方式。 I personally strongly dislike that approach.
我个人非常不喜欢这种做法。
Other problems: 其他问题:
<link>
to my <head>
? <head>
添加<link>
<head>
吗? For everything external? node_modules
feels awkward and bad, too. node_modules
内部的文件node_modules
感觉很尴尬和不好。 So, just add rules to next.config.js
, right? 那么,只需将规则添加到
next.config.js
,对吧?
module.exports = {
webpack: (config, { dev }) => {
config.module.rules.push(
{
test: /\.(css|scss)/,
loader: "style-loader!css-loader"
}
);
return config
}
};
And then just import './Page.scss';
然后只需
import './Page.scss';
(Don't worry, it's valid CSS, not even SASS yet, I know I did not include the sass-loader here just yet. I try to keep it simple first.) (别担心,它是有效的CSS,甚至不是SASS,我知道我还没有在这里包含sass-loader。我首先尝试保持简单。)
So, why doesn't it work with SSR? 那么,为什么它不适用于SSR?
Note that v5 of next.js will support CSS/Sass/Less almost out of the box : 需要注意的是next.js的V5将支持CSS /无礼/减少近开箱 :
For importing .css .scss or .less we've created modules which have sane defaults for server side rendering.
为了导入.css .scss或.less,我们创建了具有服务器端渲染的默认默认值的模块。
Until then, the following set of rules worked for me (assuming the .sass
files are in /styles
dir): 在此之前,以下一套规则适用于我(假设
.sass
文件位于/styles
目录中):
config.module.rules.push(
{
test: /\.(css|sass)/,
loader: 'emit-file-loader',
options: {
// this path is relative to the .next directory
name: 'dist/[path][name].[ext]'
}
},
{
test: /\.css$/,
use: ['babel-loader', 'raw-loader', 'postcss-loader']
},
{
test: /\.sass$/,
use: ['babel-loader', 'raw-loader', 'postcss-loader',
{ loader: 'sass-loader',
options: {
includePaths: ['styles', 'node_modules']
.map((d) => path.join(__dirname, d))
.map((g) => glob.sync(g))
.reduce((a, c) => a.concat(c), [])
}
}
]
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.