配置next.config文件

2023-12-26

我正在使用 Next.js 并想添加反应语义用户界面 https://react.semantic-ui.com,使用他们的登录组件之一。

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

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

我在某处读到你必须包括_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-u[email protected] /cdn-cgi/l/email-protection/dist/semantic.min.css'
            />
        </Head>
        <body className="custom_class">
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;

这真的有这么难吗?

Update

还有另一种方法可以使其发挥作用。 当您启动 Next 应用程序时,您会得到一个组件文件夹,其中包括head.js and a nav.js file.

head.js 文件最终类似于<head></head> tag in HTML。或者我应该说这就是head.js编译为.无论如何,你可以在其中添加以下内容:

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

那会起作用的。

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

import 'semantic-ui-css/semantic.min.css'

万一有人使用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);


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

配置next.config文件 的相关文章

随机推荐

  • Oracle 中的动态表分区

    我正在为我的应用程序构建一个数据库存储 该存储由一个具有巨大数据量 数亿条记录 的表组成 我计划在日期字段上建立索引 因为我将不时地对给定时间段内的所有记录进行批量恢复 例如 检索第二天的所有记录 午夜 由于记录数量巨大并且性能是该系统中的
  • 递归地 (?) 将 LINQ 谓词组合成单个谓词

    编辑 我问错了问题 real我遇到的问题已经结束将 LINQ to SQL 谓词组合成单个谓词 https stackoverflow com questions 3782940 compose linq to sql predicates
  • 在 OS X 上以 32 位模式使用 virtualenv 运行非系统 Python

    简短的问题使用 virtualenv virtualenvwrapper 是否可以添加前缀python调用链接到特定的虚拟环境 背景我想使用多个虚拟环境已安装酿造 https github com mxcl homebrewPython 2
  • 如何从 Kotlin DSL build.gradle 中的所有依赖项中排除库?

    我开始迁移build gradle 时髦 到build gradle kts Kotlin DSL 事情是这样的com google common util concurrent ListenableFuture from com goog
  • Netbeans:自动格式 - 防止变量赋值时出现空格格式

    我更喜欢这样的格式化作业 foo bar long foo bar 而不是这个 foo bar long foo bar 然而 Netbeans 在使用自动格式时使用后者 有人知道如何解决这个问题吗 这个问题已经发布三年了 但我认为它非常有
  • 电子邮件主题分隔符间距中的重音单词 - 如何阻止这种情况?

    我们有一个自定义的 php 电子邮件营销应用程序 还有一个有趣的问题 如果邮件的主题行包含带有重音符号的单词 则会 吞掉 该单词与下一个单词之间的空格 示例 短语 安赫尔 里奥斯 埃斯库查 索普伦德 显示 至少通过 gmail 和 Lotu
  • 数组和右值(作为参数)

    我想知道是否有任何方法可以区分以下代码中显示的函数调用 以数组作为参数 include
  • Python:遍历列表

    我有一个智力挑战谜题 我想用 python 来解决 他们给出了 4 个数字 25 28 38 35 他们希望我们将这些数字放在 一种可能的解决方案是 25 38 35 28 我尝试过 从数字中列出一个列表 用一些循环和 if 来迭代它们 l
  • 无法从我的学校网站获取我的日程安排数据。使用 cURL 登录不起作用

    Edit 为什么要负一呢 我想做的是 我正在尝试使用 cURL 登录我的学校网站并获取时间表以将其用于我的 AI 因此 我需要使用我的通行证和号码登录 但学校网站上的表格还需要一个隐藏的 令牌
  • 将多个 .sql 表转储文件合并到单个文件中

    假设我有数据库A和表b 给定多个 sql 文件 b1 b2 bn 其中每个文件对应于 b 的互斥表转储 我如何将所有文件 b1 b2 bn 合并到单个 sql 表文件中 或者我如何将各个文件的导入合并到一个表中 没有特殊的工具可以做到这一点
  • 循环遍历所有文件夹及其所有子文件夹VBA

    我知道这个问题之前被问过很多次 我已经检查了之前的建议 但我无法让我的代码运行 所以 我有一个名为 Report 的文件夹 其中也包含多个文件夹 这些文件夹包含 xlsx 和 zip 文件 每个文件还包含一个名为 2016 的文件夹 其下有
  • /www 之外的指定虚拟主机上的 Wampserver 403

    当我尝试在 c wamp www 目录之外创建虚拟主机时 Wampserver 告诉我访问被拒绝 我可以在该目录中制作一个罚款 即使对文件夹建立符号链接也可以 但我宁愿不必使用符号链接 为什么不起作用 这是我在 httpd conf 末尾使
  • Django 上多租户应用程序的最佳架构

    我一直在思考创建基于多租户应用程序的正确 最佳方法 关于姜戈 一些解释 应用程序可由多个租户 tenant1 tenant2 使用 所有租户个人数据都必须受到保护 防止其他租户 及其用户 访问 租户可以选择为应用程序对象创建额外的自定义字段
  • 计算数组数组中的项目数?

    如果我有一个声明为的对象 let compoundArray Array
  • 在 Visual Studio 中对文件进行分组

    我正在考虑在 Visual Studio 中整理我的项目布局 我想知道是否有任何 hack 插件或技巧可以将 xml 文件与 cs 文件关联起来同名所以它们出现在我的解决方案导航器 资源管理器中 类似于代码隐藏文件与其 aspx 关联的方式
  • 从 Mac 上的 SWT 应用程序打开 LWJGL 窗口

    我有一个 SWT 应用程序 可以在按下按钮后打开 OpenGL 窗口 使用 LWJGL 库 它应该关闭它的主 SWT 窗口并打开一个带有 OpenGL 上下文的新窗口 在 Windows 上运行良好 在 Mac 上 我收到此错误 2010
  • Azure 托管和 MVC5 报告

    我正在使用 Visual Studio 2013 开发托管在 Azure 上的 MVC5 EF6 Web 应用程序 我刚刚完成需要创建报告的开发部分 我试图使用 Microsoft ReportViewer 来实现此目的 尽管它在本地工作得
  • Haskell:有条件地中断循环

    我想在这样的情况下打破循环 import Data Maybe fromJust isJust Maybe Just tryCombination Int gt Int gt Maybe String tryCombination x y
  • LaTeX 相当于 Google Chart API

    我目前正在寻找不同的解决方案 将二维数学公式放入网页中 我认为维基百科解决方案 从 LaTeX 源代码生成 png 图像 已经足够好了 直到我们在网络浏览器中获得对 MathML 的支持 我突然意识到也许可以创建一个谷歌图表 API htt
  • 配置next.config文件

    我正在使用 Next js 并想添加反应语义用户界面 https react semantic ui com 使用他们的登录组件之一 在前端我收到此错误 编译失败 node modules semantic ui css semantic