简体   繁体   English

使用 VSCode 调试器和源映射调试 webpack + babel

[英]Debugging webpack + babel with the VSCode Debugger and sourcemaps

So, I have an index.js with some code that is incompatible with Node.js v12.0.0 which I am forced to use.所以,我有一个 index.js,其中的一些代码与我被迫使用的 Node.js v12.0.0 不兼容。 I have a webpack configuration that is running with babel to transpile and minify this.我有一个 webpack 配置,它与 babel 一起运行以转换和缩小它。 I have an NPM script which will run webpack with a devtool specified (see below).我有一个 NPM 脚本,它将运行 webpack 并指定 devtool(见下文)。 Now, all I want to do is step through my original code from within VSCode using those sourcemaps.现在,我要做的就是使用这些源映射从 VSCode 中逐步完成我的原始代码。 I have been twiddling with it for hours and I cannot get VSCode to use the sourcemaps.我已经玩了好几个小时了,我无法让 VSCode 使用源映射。 This is also all quite new to me so please pardon my incompetence.这对我来说也是全新的,所以请原谅我的无能。

index.js index.js

exports.handler = async () => {
    const asdf = {};

    console.log(asdf?.d ?? 3);
}

webpack.config.js webpack.config.js

const path = require('path');

module.exports = {
    entry: './index.js',
    output: {
        path: path.resolve('dist'),
        filename: 'index.js',
        library: {
            type: 'commonjs'
        }
    },
    module: {
        rules: [
            {
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            [
                                '@babel/preset-env', {
                                    targets: {
                                        node: '12.0.0'
                                    },
                                }
                            ]
                        ],
                    }
                },
            }
        ]
    },
    target: 'node12.0'
};

package.json package.json

{
    "scripts": {
        "dev": "webpack --mode=development --devtool=source-map",
        "build": "webpack --mode=production"
    },
    "devDependencies": {
        "@babel/core": "^7.16.5",
        "@babel/preset-env": "^7.16.5",
        "babel-loader": "^8.2.3"
    }
}

launch.json发射.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/tester.js",
            "sourceMaps": true,
            "console": "integratedTerminal",
        }
    ]
}

Finally, I have this little program to execute the handler function in index.js最后,我有这个小程序来执行 index.js 中的处理程序 function

tester.js tester.js

const { handler } = require("./index");

handler();

After running npm run dev , I get dist/index.js and dist/index.map.js.运行npm run dev后,我得到 dist/index.js 和 dist/index.map.js。 But, when I run the launch configuration I have, it seems that VSCode is running node directly on the original index.js instead of the transpiled output and that causes node to break (since the null guard doesn't exist in version 12.0.0).但是,当我运行启动配置时,似乎 VSCode 直接在原始 index.js 上运行节点,而不是转译的 output 并导致节点中断(因为 null 保护在版本 12.0.0 中不存在)。 I would expect the VSCode debugger to catch any breakpoints in the original index.js while referring to the sourcemap for the transpiled code.我希望 VSCode 调试器在引用转译代码的源映射时捕获原始 index.js 中的任何断点。 I am quite clueless at this point.在这一点上我很无知。 Any pointers would be greatly appreciated.任何指针将不胜感激。

Please note that I have webpack and webpack-cli installed globally (hence the absence of them in package.json).请注意,我全局安装了 webpack 和 webpack-cli(因此 package.json 中没有它们)。

Apparently a bit more motivation in my Googling led me to the answer.显然,我在谷歌上搜索的动力让我找到了答案。 In tester.js, I should have been requiring the transpiled index.js from the dist folder.在 tester.js 中,我应该需要 dist 文件夹中的转译 index.js。 You need to run your transpiled file for the debugger to use the source maps, not the other way around.您需要为调试器运行转译文件以使用源映射,而不是相反。

tester.js tester.js

const { handler } = require("./dist/index");

handler();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM