Failed to resolve async component default: TypeError: __webpack_require__(...) is not a function

本文分析了Laravel5.7项目中Vue异步组件加载失败的问题,探讨了extract-text-webpack-plugin在webpack4+环境下的不兼容性,提出了使用MiniCssExtractPlugin替代的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景

  • laravel5.7项目默认的laravel-mix编译
  • 异步路由,观察http异步组件加载成功,路由配置正确

@babel/plugin-syntax-dynamic-import 已正确安装配置并使用

  • 前端没有一个页面有显示内容,只要在路由出口区的部分全无显示

报错

  • 3个警告

app.js:42886 [vue-router] Failed to resolve async component default: TypeError: Cannot read property ‘call’ of undefined
app.js:42886 [vue-router] uncaught error during route navigation:
[vue-router] Failed to resolve async component default: TypeError: webpack_require(…) is not a function

  • 1个致命性错误

app.js:44767 TypeError: Cannot read property ‘call’ of undefined
at webpack_require (app.js:64)
at Object…/node_modules/css-loader/index.js!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src/index.js?!./node_modules/sass-loader/lib/loader.js?!./node_modules/vue-loader/lib/index.js?!./resources/js/components/global/Navigation.vue?vue&type=style&index=0&lang=scss&
(components-global-Navigation-vue~layouts-Layout-vue.js:172)

排错

  • js控制台报错,疑似js逻辑有问题,特别at __webpack_require__ (app.js:64),很显然这是一个webpack自带的模板代码导入,但改为同步代码后正常,排除,只能确认错误与异步行为相关
  • 警告信息is not a function,貌似vue-router异步组件被认为返回的不是一个函数?但network显示可以正常加载 ,返回状态都是200,说明异步组件在前端加载是ok的
  • 猜想*webpack_require* 这种地方报错,只能表明webpack某插件(可影响全局操作及相关loader运行)在某种(这里是异步执行)情况下执行,出现了异常,导致不能正常添加属性方法,从而有TypeError: Cannot read property ‘call’ of undefined的警告,检查流程如下
    • mix-manifest.json检查比对导入的路径信息与在服务器端的真实映射文件 ok
    • 查阅laravel-mix依赖的插件,关键主要信息如下
  "laravel-mix": {
            "version": "4.0.15",
            "dev": true,
            "requires": {
                "@babel/core": "^7.2.0",
                "@babel/plugin-proposal-object-rest-spread": "^7.2.0",
                "@babel/plugin-transform-runtime": "^7.2.0",
                "@babel/preset-env": "^7.2.0",
                "@babel/runtime": "^7.2.0",
                "extract-text-webpack-plugin": "v4.0.0-beta.0"
                "friendly-errors-webpack-plugin": "^1.6.1",
                "optimize-css-assets-webpack-plugin": "^5.0.1",
                "terser-webpack-plugin": "^1.1.0",
                "vue-loader": "^15.4.2",
                "webpack": "^4.27.1",
                "webpack-cli": "^3.1.2",
                "webpack-dev-server": "^3.1.14",
            }
        },

分析

  • Cannot read property 'call' of undefined警告信息为凭据,查询官方资源发现有一个关于extract-text-webpack-plugin的issue值得关注

TL;DR Use mini-css-extract-plugin instead ?
The webpack >= v4.0.0 ‘support’ for extract-text-webpack-plugin was moved to mini-css-extract-plugin as more lightweight approach for webpack >= v4.0.0 and ETWP entered maintenance mode. We are sorry for the inconvenience here, but ETWP reached a point where maintaining it become too much of a burden and it’s not the first time upgrading a major webpack version was complicated and cumbersome due to issues with ETWP. The mid-term plan is integrate CSS support directly into webpack to avoid this kind of issue in the future

  • 简单点讲:webpack4以上放弃了对extract-text-webpack-plugin插件的治疗,需要用`MiniCssExtractPlugin 来修复这个问题。判断可能是该插件在异步操作抽取css时,出错。
  • __webpack_require__依赖于mix-mainifest.json,而且webpack的一些内部引导代码会依据此内容,生成moduleid(模块标识),管理模块文件的加载顺序及引用次数。

解决

  • 基于上述两点有如下操作
  • 第一次使用laravel-mix操作只用同步形式生成CSS,不涉及js的编译操作
  • 二次编译生成的异步js,确保mix-mainifest.json只有纯js模块关联
  • 在入口html模板文件内,引入全局第一次编译的css就可以了

小结

  • 对于异步组件加载,需要加载css文件,但extract-text-webpack-plugin在异步中不给力的加载行为,会有Cannot read property 'call' of undefined警告,间接影响webpack的模板代码加载,最终出现__webpack_require__(...) is not a function,从而报致命错误Cannot read property 'call' of undefined

  • 所以如果不用替代插件,需要在__webpack_require__的导入内容内容上作手脚,将CSS相关的异步加载排除在外,不给webapck来处理加载,交给浏览器同步下载。

  • 附laravel-mix配置文件

const mix = require('laravel-mix');

// 先单独编译css,二次编译关闭,主要是避免mix配置对象污染
// mix.sass('resources/sass/app.scss', 'public/css')

mix.js('resources/js/app.js', 'public/js')
    .webpackConfig({
        output: {
            chunkFilename: 'js/[name].js'
        },
        module: {
            rules: [{
                    test: /\.jsx?$/,
                    exclude: /node_modules(?!\/foundation-sites)|bower_components/,
                    use: [{
                        loader: 'babel-loader',
                        // 使用mix默认配置babel,会读取项目根目录下的,babelrc配置(内支持import函数)
                        options: Config.babel()
                    }]
                }
            ]
        },
        // 配置路径别名
        resolve: {
            alias: {
                '@': path.resolve(__dirname, 'resources/sass')
            }
        }
    })
选择的环境为 h5 H5-local 👽 Taro v3.6.2 stderr:Webpack █████████████████████████ setup (9%) compilation TaroH5Plugin ● Webpack █████████████████████████ building (23%) 1/4 entries 1/4 dependencie ● Webpack █████████████████████████ sealing (92%) asset processing ✖ WebpackWebpack Compiled with some errors in 13.55s <i> [webpack-dev-server] Project is running at: <i> [webpack-dev-server] On Your Network (IPv4): http://192.168.2.144:8080/ <i> [webpack-dev-server] Content not from webpack is served from 'E:\hy\mall\mini-program\dist\h5' directory <i> [webpack-dev-server] 404s will fallback to '/index.html' <i> [webpack-dev-middleware] wait until bundle finished: /mobile/ (node:25932) [DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_HASH] DeprecationWarning: [hash] is now [fullhash] (also consider using [chunkhash] or [contenthash], see documentation for details) (Use `node --trace-deprecation ...` to show where the warning was created) ✖ Errors: Error: plugin x failed to invoke plugin on 'Some("E:\\hy\\mall\\mini-program\\src\\app.config.ts")' → Watching... [2025/7/1 18:11:45] ℹ Listening at http://192.168.2.144:8080/mobile/ asset js/app.3753405147285bf9cda9.js 977 KiB [emitted] [immutable] (name: app) asset js/taro.3753405147285bf9cda9.js 179 KiB [emitted] [immutable] (name: taro) (id hint: taro) asset js/runtime.3753405147285bf9cda9.js 45.4 KiB [emitted] [immutable] (name: runtime) asset index.html 8.28 KiB [emitted] Entrypoint app 1.17 MiB = js/runtime.3753405147285bf9cda9.js 45.4 KiB js/taro.3753405147285bf9cda9.js 179 KiB js/app.3753405147285bf9cda9.js 977 KiB runtime modules 30 KiB 14 modules modules by path ./node_modules/core-js-pure/ 132 KiB 127 modules modules by path ./node_modules/@pmmmwh/react-refresh-webpack-plugin/ 43.9 KiB 22 modules modules by path ./node_modules/@tarojs/webpack5-runner/node_modules/webpack-dev-server/client/ 61.6 KiB 12 modules modules by path ./node_modules/html-entities/dist/ 67.8 KiB modules by path ./node_modules/html-entities/dist/esm/*.js 33.5 KiB 4 modules modules by path ./node_modules/html-entities/dist/commonjs/*.js 34.2 KiB 4 modules modules by path ./node_modules/webpack/hot/*.js 5.82 KiB ./node_modules/webpack/hot/only-dev-server.js 2.58 KiB [built] [code generated] ./node_modules/webpack/hot/log.js 1.74 KiB [built] [code generated] + 2 modules modules by path ./node_modules/react-refresh/ 21.8 KiB ./node_modules/react-refresh/runtime.js 222 bytes [built] [code generated] ./node_modules/react-refresh/cjs/react-refresh-runtime.development.js 21.6 KiB [built] [code generated] + 7 modules ERROR in ./src/app.config.ts plugin x failed to invoke plugin on 'Some("E:\\hy\\mall\\mini-program\\src\\app.config.ts")' ERROR in ./src/app.config.ts Cannot read properties of undefined (reading 'length') TypeError: Cannot read properties of undefined (reading 'length') at WasmHash._updateWithBuffer (E:\hy\mall\mini-program\node_modules\webpack\lib\util\hash\wasm-hash.js:108:23) at WasmHash.update (E:\hy\mall\mini-program\node_modules\webpack\lib\util\hash\wasm-hash.js:51:8) at BatchedHash.update (E:\hy\mall\mini-program\node_modules\webpack\lib\util\hash\BatchedHash.js:50:14) at NormalModule.updateHash (E:\hy\mall\mini-program\node_modules\webpack\lib\NormalModule.js:1365:8) at Compilation._createModuleHash (E:\hy\mall\mini-program\node_modules\webpack\lib\Compilation.js:3970:11) at Compilation.createModuleHashes (E:\hy\mall\mini-program\node_modules\webpack\lib\Compilation.js:3927:25) at E:\hy\mall\mini-program\node_modules\webpack\lib\Compilation.js:3027:11 at Hook.eval [as callAsync] (eval at create (E:\hy\mall\mini-program\node_modules\webpack\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:10:1) at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (E:\hy\mall\mini-program\node_modules\webpack\node_modules\tapable\lib\Hook.js:20:14) at E:\hy\mall\mini-program\node_modules\webpack\lib\Compilation.js:2983:36 at eval (eval at create (E:\hy\mall\mini-program\node_modules\webpack\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:18:1) at E:\hy\mall\mini-program\node_modules\@tarojs\webpack5-runner\node_modules\html-webpack-plugin\lib\cached-child-compiler.js:237:53 at processTicksAndRejections (node:internal/process/task_queues:95:5) webpack 5.90.1 compiled with 2 errors in 13559 ms 升级tarojscli出现的问题
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值