调试webpack
第一种方式:
webpack.config.js 直接打印:
console.log(webpackconfig)
第二种方式:
监视调试的过程
$ npx node --inspect-brk ./node_modules/.bin/webpack --inline --progress # 苹果$ npx node --inspect-brk ./node_modules/webpack/bin/webpack --inline --progress # windows
—inline —progress 加上这两个属性就可以看到webpack打包的过程
运行后终端出现下图内容
执行之后,谷歌浏览器打开 chrome://inspect/#devices
点击inspect,就可以打开期待已久的 webpack 调试窗口
之后需要修改一下 webpack.config.js
在const webpackconfig 前添加debugger断点
完整代码:
const path = require('path')const nodeExternals = require('webpack-node-externals')const { CleanWebpackPlugin } = require('clean-webpack-plugin');debugger;const webpackconfig = {target: 'node',mode: 'development', // 开发模式entry: {server: path.join(__dirname, 'src/index.js') // 入口文件},output: {filename: '[name].bundle.js',path: path.join(__dirname, './dist') // 打包之后存放的目录},devtool: 'eval-source-map',module: {rules: [{test: /\.(js|jsx)$/, // 匹配目录下所有 js 文件use: {loader: 'babel-loader'},exclude: [path.join(__dirname, 'node_modules')] // 排除}]},externals: [nodeExternals()],plugins: [new CleanWebpackPlugin()],node: {console: true,global: true,process: true,Buffer: true,__filename: true,__dirname: true,setImmediate: true,path: true}}console.log(webpackconfig)module.exports = webpackconfig
之后再次进入到调试窗口,点击运行,会自动的跳到 debugger 上去,这时候可以随意的添加断点进行调试了
我们把这个脚本添加到 package.json 的脚本中,方便快速启用
"scripts": {"start": " nodemon --exec babel-node src/index.js","webpack:debug": "node --inspect-brk ./node_modules/webpack/bin/webpack --inline --progress"}
启动:
$ npm run webpack:debug
第三种方式:
使用 VScode 进行调试
首先,打开vscode编辑器,点击右侧 小虫子运行 按钮,创建调试选择node.js环境,会为我们创建一个 launch.json 配置
修改一下里面的配置
{// 使用 IntelliSense 了解相关属性。// 悬停以查看现有属性的描述。// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"type": "node","request": "launch","name": "nodemon","runtimeExecutable": "${workspaceFolder}/node_modules/.bin/nodemon","program": "${workspaceFolder}/src/index.js","restart": true,"console": "integratedTerminal","internalConsoleOptions": "neverOpen","runtimeArgs": ["--exec", "${workspaceFolder}/node_modules/.bin/babel-node"]}]}
之后点击运行按钮,会自动启动 nodemon
接下来我们可以在 vscode 打上断点,浏览器访问会自动跳到断点处,之后就可以愉快的调试啦
在这里也可以直接输入我们要监视的东西,比如 ctx.requers 请求
