Reactjs 配置next.config文件
我正在使用Next.js,并希望添加,以使用他们的一个登录组件 在前端,我遇到以下错误: 未能编译Reactjs 配置next.config文件,reactjs,next.js,semantic-ui-react,Reactjs,Next.js,Semantic Ui React,我正在使用Next.js,并希望添加,以使用他们的一个登录组件 在前端,我遇到以下错误: 未能编译 ./node_modules/semantic-ui-css/semantic.min.css ModuleParseError: Module parse failed: Unexpected character '' (1:0) You may need an appropriate loader to handle this file type. (Source code omitted f
./node_modules/semantic-ui-css/semantic.min.css
ModuleParseError: Module parse failed: Unexpected character '' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
这是登录组件:
import React from 'react'
import { Button, Form, Grid, Header, Image, Message, Segment } from 'semantic-ui-react'
const Login = () => (
/* login JSX markup */
)
export default Login
这是我的下一个.config.js
module.exports = {
webpack: (config, { dev }) => {
config.module.rules.push(
{
test: /\.css$/,
loader: 'style-loader!css-loader'
},
{
test: /\.s[a|c]ss$/,
loader: 'sass-loader!style-loader!css-loader'
},
{
test: /\.(png|svg|eot|otf|ttf|woff|woff2)$/,
use: {
loader: "url-loader",
options: {
limit: 100000,
publicPath: "./",
outputPath: "static/",
name: "[name].[ext]"
}
}
},
{
test: [/\.eot$/, /\.ttf$/, /\.svg$/, /\.woff$/, /\.woff2$/],
loader: require.resolve('file-loader'),
options: {
name: '/static/media/[name].[hash:8].[ext]'
}
}
)
return config
}
}
const withCSS = require('@zeit/next-css')
module.exports = withCSS()
这是我的package.js
{
"name": "create-next-example-app",
"scripts": {
"dev": "nodemon server/index.js",
"build": "next build",
"start": "NODE_ENV=production node server/index.js"
},
"dependencies": {
"@zeit/next-css": "^1.0.1",
"body-parser": "^1.18.3",
"cors": "^2.8.5",
"express": "^4.16.4",
"mongoose": "^5.4.19",
"morgan": "^1.9.1",
"next": "^8.0.3",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"semantic-ui-css": "^2.4.1",
"semantic-ui-react": "^0.86.0"
},
"devDependencies": {
"css-loader": "^2.1.1",
"file-loader": "^3.0.1",
"node-sass": "^4.11.0",
"nodemon": "^1.18.10",
"sass-loader": "^7.1.0",
"url-loader": "^1.1.2"
}
}
我在某个地方读到,你必须在pages目录中包含一个\u document.js
// _document is only rendered on the server side and not on the client side
// Event handlers like onClick can't be added to this file
// ./pages/_document.js
import Document, { Html, Head, Main, NextScript } from 'next/document';
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx);
return { ...initialProps };
}
render() {
return (
<Html>
<Head>
<link rel='stylesheet'
href='//cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css'
/>
</Head>
<body className="custom_class">
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument;
这会奏效的
但是就像我说的,你仍然不能像这样导入模块:
import 'semantic-ui-css/semantic.min.css'
看来我必须做以下几件事才能让它工作: 将我的
next.config.js
文件更改为:
const withCSS = require('@zeit/next-css')
module.exports = withCSS({
webpack: function (config) {
config.module.rules.push({
test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif)$/,
use: {
loader: 'url-loader',
options: {
limit: 100000,
name: '[name].[ext]'
}
}
})
return config
}
})
做一个npmicss加载器文件加载器url加载器-D
就成功了
然而,我不明白为什么需要
css加载器文件加载器
?我习惯于在webpack配置中显式添加加载程序(就像我们在上面添加url加载程序
)。。。我不必在这里 看来我必须做以下几件事才能让它起作用:
将我的next.config.js
文件更改为:
const withCSS = require('@zeit/next-css')
module.exports = withCSS({
webpack: function (config) {
config.module.rules.push({
test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif)$/,
use: {
loader: 'url-loader',
options: {
limit: 100000,
name: '[name].[ext]'
}
}
})
return config
}
})
做一个npmicss加载器文件加载器url加载器-D
就成功了
然而,我不明白为什么需要
css加载器文件加载器
?我习惯于在webpack配置中显式添加加载程序(就像我们在上面添加url加载程序
)。。。我不必在这里 如果有人使用next compose插件
并出现上述错误,这里有一个修复方法:
const withCSS = require('@zeit/next-css');
const withImages = require('next-images');
const withPlugins = require('next-compose-plugins');
// fix: prevents error when .css files are required by node
if (typeof require !== 'undefined') {
require.extensions['.css'] = (file) => {};
}
const nextConfig = {
target: 'server',
webpack: (config, { dev }) => {
config.module.rules.push({
test: /\.(raw)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: 'raw-loader'
});
return config;
}
};
module.exports = withPlugins([withImages, withCSS({target: 'serverless',
webpack (config) {
config.module.rules.push({
test: /\.(png|svg|eot|otf|ttf|woff|woff2)$/,
use: {
loader: 'url-loader',
options: {
limit: 8192,
publicPath: '/_next/static/',
outputPath: 'static/',
name: '[name].[ext]'
}
}
})
return config
}})], nextConfig);
如果有人使用了
next compose plugins
并出现上述错误,这里有一个修复方法:
const withCSS = require('@zeit/next-css');
const withImages = require('next-images');
const withPlugins = require('next-compose-plugins');
// fix: prevents error when .css files are required by node
if (typeof require !== 'undefined') {
require.extensions['.css'] = (file) => {};
}
const nextConfig = {
target: 'server',
webpack: (config, { dev }) => {
config.module.rules.push({
test: /\.(raw)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: 'raw-loader'
});
return config;
}
};
module.exports = withPlugins([withImages, withCSS({target: 'serverless',
webpack (config) {
config.module.rules.push({
test: /\.(png|svg|eot|otf|ttf|woff|woff2)$/,
use: {
loader: 'url-loader',
options: {
limit: 8192,
publicPath: '/_next/static/',
outputPath: 'static/',
name: '[name].[ext]'
}
}
})
return config
}})], nextConfig);
您的应用程序是否使用没有
@import
语句的CSS正常编译?@NinoFiliu它根本不编译…错误可能来自Webpack。检查一下,在解释网页包加载程序方面做得很好-它们是什么,为什么需要它们以及如何使用它们。在您的情况下,您的错误看起来像是Webpack遇到了CSS文件,但没有所需的loaders+config来“理解”它。@NinoFiliu谢谢我的朋友,我来看看。我打赌在某个地方你必须显式地配置Webpack。你的应用程序是否使用没有@import
语句的CSS正常编译?@NinoFiliu它根本不编译…错误可能来自Webpack。检查一下,在解释网页包加载程序方面做得很好-它们是什么,为什么需要它们以及如何使用它们。在您的情况下,您的错误看起来像是Webpack遇到了CSS文件,但没有所需的loaders+config来“理解”它。@NinoFiliu谢谢我的朋友,我来看看。我打赌在某个地方,你必须明确地配置网页包。谢谢你,伙计,这帮了我。谢谢你,伙计,这帮了我