无法构建包含自定义工作文件的项目

Unable to build project that includes a custom worker file

我在使用 next-pwa 时遇到了一个非常奇怪的问题。每当我尝试构建一个包含自定义 worker js 文件的 next-pwa 项目时,构建都会失败并出现以下错误:

info  - Creating an optimized production build ..buffer.js:333
  throw new ERR_INVALID_ARG_TYPE(
  ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
    at Function.from (buffer.js:333:9)
    at writeOut (C:\Users\sdqui\Desktop\next-pwa\examples\custom-worker\node_modules\next\dist\compiled\webpack\bundle4.js:56716:26)
    at C:\Users\sdqui\Desktop\next-pwa\examples\custom-worker\node_modules\next\dist\compiled\webpack\bundle4.js:56735:7
    at arrayIterator (C:\Users\sdqui\Desktop\next-pwa\examples\custom-worker\node_modules\next\dist\compiled\neo-async\async.js:1:14270)
    at timesSync (C:\Users\sdqui\Desktop\next-pwa\examples\custom-worker\node_modules\next\dist\compiled\neo-async\async.js:1:5037)
    at Object.eachLimit (C:\Users\sdqui\Desktop\next-pwa\examples\custom-worker\node_modules\next\dist\compiled\neo-async\async.js:1:14216)
    at emitFiles (C:\Users\sdqui\Desktop\next-pwa\examples\custom-worker\node_modules\next\dist\compiled\webpack\bundle4.js:56617:13)
    at C:\Users\sdqui\Desktop\next-pwa\examples\custom-worker\node_modules\next\dist\compiled\webpack\bundle4.js:36508:26
    at FSReqCallback.oncomplete (fs.js:184:5) {
  code: 'ERR_INVALID_ARG_TYPE'
}

到目前为止我已经尝试了几种方法来解决这个问题:

我也尝试过使用回购的 /example 文件夹中提供的 code,但同样的问题也出现在那里。

我不是唯一一个面临这个问题的人,所以我很感激帮助。

您可以使用 Gitpod 自行测试,只需打开此 link 并导航到示例,安装依赖项并尝试 yarn build

您使用的是 webpack 5 还是 4.x?我在 webpack > 5 时遇到了同样的问题,并通过将 future 对象传递给 withPWA " future: { webpack5: true }" 来修复了同样的问题。

module.exports = withPWA({ future: { webpack5: true },
pwa: {
    disable: false,
    dest: "public",
    //register: false,
    skipWaiting: false,
    runtimeCaching
}

})