配置next.config文件

问题描述 投票:1回答:2

我正在使用Next.js并想要添加react-semantic-ui,以使用其中一个登录组件。

在前端我收到此错误:编译失败

./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

这是我的next.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目录中包含一个_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/[email protected]/dist/semantic.min.css'
            />
        </Head>
        <body className="custom_class">
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;

这真的很难吗?

更新

还有另一种方法可以让它发挥作用。当你启动你的下一个应用程序时,你会得到一个组件文件夹,其中包含一个head.js和一个nav.js文件。

head.js文件最终类似于<head></head>中的HTML标签。或者我应该说这就是head.js编写的内容。无论如何,你可以在那里添加:

<link
  rel="stylesheet"
  href="//cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css"
/>

这将有效。

但就像我说你仍然无法导入这样的模块:

import 'semantic-ui-css/semantic.min.css'
reactjs next.js semantic-ui-react
2个回答
1
投票

如果有人使用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);



0
投票

因此看起来我必须执行以下操作才能使其工作:

把我的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
  }
})

做一个npm i css-loader file-loader url-loader -D做了伎俩。

但是我很困惑为什么需要css-loader file-loader?我已经习惯了webpack配置你明确添加加载器(就像我们在上面添加url-loader)...我没有必要在这里!

© www.soinside.com 2019 - 2024. All rights reserved.