将 CopyPlugin 添加到 next.config.js

2024-01-12

我想将以下内容添加到我的 webpack 配置中!

module.exports = {
  ...otherConfig,
  plugins: [
    new CopyPlugin([{ 
        from: './node_modules/@pdftron/webviewer/public',
        to: './dist/public/webviewer' 
      }]
    ),
  ],
};

但是,由于我使用的是 Next.js,因此我遵循此处的文档:https://nextjs.org/docs/api-reference/next.config.js/custom-webpack-config https://nextjs.org/docs/api-reference/next.config.js/custom-webpack-config

这是我最终得到的代码:

const CopyPlugin = require("copy-webpack-plugin");

module.exports = {
    webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
        // Note: we provide webpack above so you should not `require` it
        // Perform customizations to webpack config
        const newconfig = config.plugins.push(
            new CopyPlugin([
                {
                    from: './node_modules/@pdftron/webviewer/public',
                    to: './public/webviewer',
                },
            ]),
        );

        // Important: return the modified config
        return newconfig
    },
}

为什么不起作用?

这是错误:

ready - started server on http://localhost:3000
ValidationError: Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
 - options[0] misses the property 'patterns'. Should be:
   [non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, transform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)
    at validate (D:\Code\Javascript\nextjs-projects\new-amsterdam\node_modules\copy-webpack-plugin\node_modules\schema-utils\dist\validate.js:104:11)
    at new CopyPlugin (D:\Code\Javascript\nextjs-projects\new-amsterdam\node_modules\copy-webpack-plugin\dist\index.js:40:31)
    at Object.webpack (D:\Code\Javascript\nextjs-projects\new-amsterdam\next.config.js:8:13)
    at getBaseWebpackConfig (D:\Code\Javascript\nextjs-projects\new-amsterdam\node_modules\next\dist\build\webpack-config.js:136:360)
    at async Promise.all (index 0)
    at async HotReloader.start (D:\Code\Javascript\nextjs-projects\new-amsterdam\node_modules\next\dist\server\hot-reloader.js:14:2403)
    at async DevServer.prepare (D:\Code\Javascript\nextjs-projects\new-amsterdam\node_modules\next\dist\server\next-dev-server.js:15:414)
    at async D:\Code\Javascript\nextjs-projects\new-amsterdam\node_modules\next\dist\cli\next-dev.js:22:1 {
  errors: [
    {
      keyword: 'required',
      dataPath: '[0]',
      schemaPath: '#/required',
      params: [Object],
      message: "should have required property 'patterns'",
      schema: [Object],
      parentSchema: [Object],
      data: [Object],
      children: [Array]
    }
  ],
  schema: {
    definitions: { ObjectPattern: [Object], StringPattern: [Object] },
    type: 'object',
    additionalProperties: false,
    properties: { patterns: [Object], options: [Object] },
    required: [ 'patterns' ]
  },
  headerName: 'Copy Plugin',
  baseDataPath: 'options',
  postFormatter: null
}

先感谢您!


您答案中的代码是正确的,我希望我能帮助解释原因。我在您的原始代码中看到了两个错误:

1. copy-webpack-plugin配置不正确。

您的原始配置是一个包含一个对象的数组:

new CopyPlugin([
  {
    from: './node_modules/@pdftron/webviewer/public',
    to: './public/webviewer',
  },
]),

但为了得到你想要的结果,你需要为插件提供配置object使用键“模式”(包含一个数组),就像您在答案中所做的那样:

new CopyPlugin({
  patterns: [
    {
      from: './node_modules/@pdftron/webviewer/public',
      to: './public/webviewer',
    },
  ],
})

您在问题中发布的错误消息解释了插件是如何错误配置的,只是不是以最直观的方式:

ValidationError: Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
 - options[0] misses the property 'patterns'. Should be:
   [non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, transform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)

2.您的“webpack”方法的返回值不正确。

在最初的代码中,您分配了变量“newconfig”来获取结果config.plugins.push(...),然后返回它:

const newconfig = config.plugins.push(

// ... plugin config ...

// Important: return the modified config
return newconfig

但是,正如你可以看到的Array.push 上的 MDN 文档 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push,Array.push的返回值为数组的长度.

因此,当你写下return newconfig,这就像写作return config.plugins.length,当 next.js 期望您返回整个配置对象时。

当您将一个项目推送到数组时,数组会就地修改,因此您不需要捕获新值。因此,您可以简单地返回原始配置:

// Important: return the modified config
return config

您的答案中的代码整体可以正常工作:

const CopyPlugin = require('copy-webpack-plugin')

module.exports = {
    webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
        // Note: we provide webpack above so you should not `require` it
        // Perform customizations to webpack config
        config.plugins.push(
            new CopyPlugin({
                patterns: [
                    {
                        from: './node_modules/@pdftron/webviewer/public',
                        to: './public/webviewer',
                    },
                ],
            })
        )

        // Important: return the modified config
        return config
    },
}

我不确定为什么您还需要卸载并重新安装copy-webpack-plugin包裹,除非我缺少什么东西。

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

将 CopyPlugin 添加到 next.config.js 的相关文章

随机推荐

  • 将字符串转换为 int (但前提是确实是 int)

    在大学里 我被问到我们的程序是否检测到从命令行参数输入的字符串是否是一个整数 但它没有检测到 Program 3 7 现在我想知道如何检测到这一点 因此输入例如aatoi 检测到的无效 但输入例如3 6应该是无效的 但 atoi 会将其转换
  • Ruby on Rails 显示带有 image_tag src 的图像

    我的里面有一张图片app assets images文件夹 我在视图页面中尝试显示图像 当我在本地主机中加载页面时 我的图像仅显示 assets 在其 src 路径中 img alt Assets src assets 我不明白为什么会出现
  • 如何更改 EKS 中 kube-scheduler 的行为?

    我是 Kubernetes 新手 对设置它完全陌生EKS 我正在努力实现共享GPU在多个 Pod 之间 但是为了查看一些文档和文章 我发现我应该更新kube scheduler configuration带有参数 然后我可以进行必要的更改以
  • 未找到自制程序的 gpg-agent

    我正在尝试安装gpg agent 但我收到一个错误 该公式不存在 brew install gpg agent Error No available formula with the name gpg agent gt Searching
  • 将 RichTextBox 中的制表符转换为空格

    I have a WinForms application with a RichTextBox control on the form Right now I have the AcceptsTabs property set to tr
  • R 中的 Predict.lm() - 如何获得拟合值周围的非常量预测带

    所以我目前正在尝试绘制线性模型的置信区间 我发现我应该为此使用 Predict lm 但我在真正理解该函数时遇到一些问题 并且我不喜欢在不知道发生了什么的情况下使用函数 我找到了几个关于这个主题的操作方法 但只有相应的 R 代码 没有真正的
  • Nunjucks 中的 Javascript 函数

    所以我在 Nunjucks 文档中找到了这个 函数调用 如果您已将 JavaScript 方法传递给模板 则可以像平常一样调用它 foo 1 2 3 但我似乎无法工作 我尝试将我的函数放在 html 页面上
  • 选择文件夹对话框 WPF

    我开发了一个 WPF4 应用程序 在我的应用程序中 我需要让用户选择一个文件夹 应用程序将在其中存储某些内容 文件 生成的报告等 我的要求 能够查看标准文件夹树 能够选择文件夹 WPF 外观和感觉 此对话框必须看起来像为 Windows V
  • 在 R 版本 3.0.2 上安装 Rtools

    我已经安装了devtools对于 R 但是当我使用以下命令调用库时 library devtools 我得到以下输出 WARNING Rtools is required to build R packages but is not cur
  • 使用vba将文件从一个文件夹复制到另一个文件夹

    我知道有一些关于这个主题的类似帖子 但是 我有一个与我在这里看到的所有代码 在谈论这个主题时 不同的代码 我收到的错误是找不到该文件 但这是不可能的 因为我正在 fso CopyFile 中用作 SOURCE 的同一文件夹中搜索文件 所以我
  • C# Winform 关闭程序后进程仍在Windows任务列表管理器中

    为什么关闭程序后该进程仍在Windows任务列表管理器中 我使用登录Form cs STAThread static void Main Application EnableVisualStyles Application SetCompa
  • 我可以从同一解决方案中的不同项目访问另一个项目的嵌入式资源吗?

    我有一个 xml 文件 它作为 Project A 中的嵌入资源 我想从引用 Project A 的 Project B 访问此嵌入资源 基本上 Project B 由单元测试组成 我使用 ReSharper 运行它们 当我在 Projec
  • Python可写缓冲区/内存视图到数组/字节数组/ctypes字符串缓冲区

    Problem 固定大小记录的二进制数据 想要使用struct unpack from和struct pack into来操作二进制数据 不需要数据副本 想要内存中的多个视图来简单地抵消计算等 数据可以位于 array array byte
  • scp通过ssh隧道打开

    我想从已与服务器打开反向隧道的计算机发送文件 反向隧道将计算机上的端口 22 与服务器上的端口 2222 连接 autossh M 0 q f N o ServerAliveInterval 120 o ServerAliveCountMa
  • @AttributeOverride 不适用于继承

    我正在尝试更改子类表中的列名 但 AttributeOverride 注释并未更改它 Entity Table name emp Inheritance strategy InheritanceType TABLE PER CLASS pu
  • 如何删除单击 uib-accordion-heading 时出现的蓝色边框?

    我已尝试以下问题中提出的解决方案但无济于事 从 Chrome 中的 css 自定义样式按钮中删除蓝色边框 https stackoverflow com questions 20340138 remove blue border from
  • 使用 rsync+ssh+公钥作为与 ssh 密钥所有者不同的用户同步本地和远程目录

    目标是通过 ssh 同步本地和远程文件夹 我当前的用户是user1 并且我通过 ssh 对服务器进行了无密码访问设置server1 我想将本地文件夹与上的文件夹同步server1借助于rsync公用事业 通常我会运行 rsync rtvz
  • MergeLatest 的默认值

    官方文档 https doc akka io docs akka current stream operators Source or Flow mergeLatest html of MergeLatest状态 MergeLatest 为
  • QueryPerformanceCounter 和溢出

    我正在使用 QueryPerformanceCounter 在我的应用程序中进行一些计时 然而 运行几天后 该应用程序似乎停止正常运行 如果我只是重新启动应用程序 它就会再次开始工作 这让我相信我的计时代码存在溢出问题 Author Rya
  • 将 CopyPlugin 添加到 next.config.js

    我想将以下内容添加到我的 webpack 配置中 module exports otherConfig plugins new CopyPlugin from node modules pdftron webviewer public to