Skip to content

fix(webpack-plugin): 兼容 webpack 5.109 的 AST 位置信息变更 - #2567

Open
mackwang112 wants to merge 1 commit into
masterfrom
fix/webpack-5-109-require-async-location
Open

fix(webpack-plugin): 兼容 webpack 5.109 的 AST 位置信息变更#2567
mackwang112 wants to merge 1 commit into
masterfrom
fix/webpack-5-109-require-async-location

Conversation

@mackwang112

@mackwang112 mackwang112 commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

问题背景

webpack 5.109.0 起不再在 AST 节点上提供 loc,第三方插件需要通过 parser.getLocation(node) 获取位置信息。@mpxjs/webpack-plugin 在处理 require.async 时仍读取 expr.loc,导致新版 webpack 下依赖对象的 loc 为 undefined,并触发构建错误。

Module parse   
  failed: Cannot use 'in' operator to search for 'start' in undefined                                                                                                                        
  File was parsed as module type 'javascript/auto'.                                                                                                                                          
  File was processed with these loaders:                                                                                                                                                     
   * ./node_modules/.pnpm/babel-loader@8.4.1_@babel+core@7.29.7_webpack@5.109.2_postcss@8.5.26_/node_modules/babel-loader/lib/index.js                                                       
  You may need an additional loader to handle the result of these loaders.                                                                                                                   
  TypeError: Cannot use 'in' operator to search for 'start' in undefined                                                                                                                     
      at set loc xxxxxxxxxxx
      ......

修改内容

  • webpack 提供 parser.getLocation 时,使用该方法获取 AST 位置信息
  • 旧版本 webpack 不存在该方法时,回退使用 expr.loc
  • AsyncDependenciesBlock 与 ImportDependency 共用兼容后的位置信息
  • 在相关代码处增加中文注释和 webpack 变更链接

验证结果

  • ESLint 检查通过
  • webpack-plugin 现有 util 测试通过:2 个测试套件、18 个测试
  • 已分别验证 webpack 5.101.3 的 expr.loc 回退路径和 webpack 5.109.2 的 parser.getLocation 路径
  • git diff --check 通过

@mackwang112
mackwang112 force-pushed the fix/webpack-5-109-require-async-location branch from f530545 to 4586167 Compare August 13, 2026 06:43
@mackwang112 mackwang112 changed the title fix(webpack-plugin): support webpack 5.109 parser locations fix(webpack-plugin): 兼容 webpack 5.109 的 AST 位置信息变更 Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant