通过chrome调试

  1. node --inspect-brk ./node_modules/webpack-cli/bin/cli.js

通过执行命令调试

  • 打开工程目录,点击调试按钮,再点击小齿轮的配置按钮系统就会生成 launch.json 配置文件
  • 修改好了以后直接点击 F5 就可以启动调试

.vscode\launch.json

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "type": "node",
  6. "request": "launch",
  7. "name": "debug webpack",
  8. "cwd": "${workspaceFolder}",
  9. "program": "${workspaceFolder}/node_modules/webpack-cli/bin/cli.js"
  10. }
  11. ]
  12. }

debugger.js

  1. const webpack = require("webpack");
  2. const webpackOptions = require("./webpack.config");
  3. const compiler = webpack(webpackOptions);
  4. //4.执行对象的run方法开始执行编译
  5. compiler.run((err, stats) => {
  6. console.log(err);
  7. console.log(stats.toJson(
  8. {
  9. assets: true,
  10. chunks: true,
  11. modules: true,
  12. entries: true,
  13. }
  14. ));
  15. });

image.png

调试webpack-cli源码

  1. git clone https://github.com/webpack/webpack.git
  2. git reset --hard vx.x.x
  3. yarn
  4. yarn link
  5. git clone https://github.com/webpack/webpack-cli.git
  6. git reset --hard webpack-cli@x.x.x
  7. cd packages\webpack-cli
  8. yarn link webpack
  9. yarn link
  1. yarn link webpack
  2. yarn link webpack-cli
  3. yarn unlink webpack
  4. yarn unlink webpack-cli