在WebStorm 11中调试WebPack

问题描述 投票:9回答:3

我正在尝试使用源映射调试WebStorm中与WebPack捆绑的javascript应用程序。我当前的webpack.config.js看起来像这样:

var path = require('path');

module.exports = {
    debug: true,
    devtool: 'source-map',

    context: path.join(__dirname, 'js'),
    entry: './main.js',
    output: {
        path: path.join(__dirname, 'Built'),
        filename: '[name].bundle.js'
    }
}

生成源映射,如下所示:

{"version":3,"sources":["webpack:///webpack/bootstrap 2919a5f916c286b8e21a","webpack:///./main.js","webpack:///./structure_editor/content.js","webpack:///./structure_editor/test_bundle.js"],"names":[],"mappings":";AAAA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA,uBAAe;AACf;AACA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;;AAGA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;;;;;;;ACtCA;AACA;;AAEA;AACA;;AAEA;;AAEA;;AAEA;;;;;;;ACVA,8C;;;;;;ACAA;;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA,6B","file":"main.bundle.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\texports: {},\n \t\t\tid: moduleId,\n \t\t\tloaded: false\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.loaded = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(0);\n\n\n\n/** WEBPACK FOOTER **\n ** webpack/bootstrap 2919a5f916c286b8e21a\n **/","document.write(require(\"./structure_editor/content.js\"));\r\nvar TestBundle = require(\"./structure_editor/test_bundle.js\");\r\n\r\nvar test = new TestBundle();\r\ntest.testMe();\r\n\r\n//var StructureEditor = require(\"./structure_editor/structure_editor.js\");\r\n\r\n//var editor = new StructureEditor(0x00FF00);\r\n\r\n//editor.run();\r\n\n\n\n/*****************\n ** WEBPACK FOOTER\n ** ./main.js\n ** module id = 0\n ** module chunks = 0\n **/","module.exports = \"It works from content.js.\";\n\n\n/*****************\n ** WEBPACK FOOTER\n ** ./structure_editor/content.js\n ** module id = 1\n ** module chunks = 0\n **/","var TestBundle = function () {\r\n    \r\n}\r\n\r\nTestBundle.prototype.testMe = function() {\r\n    var a = 10;\r\n    var b = 12;\r\n    var c = a + b;\r\n    document.write(c);\r\n};\r\n\r\nmodule.exports = TestBundle;\n\n\n/*****************\n ** WEBPACK FOOTER\n ** ./structure_editor/test_bundle.js\n ** module id = 2\n ** module chunks = 0\n **/"],"sourceRoot":""}

现在,我发现WebStorm 11将完全支持WebPack及其源映射[例如。 here]但它提供的信息很少。使用我提供的配置调试不起作用,忽略断点。经过多次尝试后,我发现了唯一一个允许我进行调试的配置(正确地,其他尝试有时可能会破坏代码,但行和代码执行不匹配),通过设置devtool: 'eval'。但是,这与我尝试使用的源映射无关。

生成的源映射适用于所有流行的浏览器,让我调试其中的原始源,那么为什么WebStorm不起作用?在使用源映射之前,是否需要在WebStorm中执行某些配置?

我正在使用的当前WS版本是142.4148,并且通过chrome扩展完成调试。我很感激有关如何在这里设置调试的任何想法或教程,即使对于较旧的WS 10版本(我使用WS 11只是因为它应该与WebPack很好地配合使用)

javascript debugging webstorm webpack source-maps
3个回答
2
投票

WebStorm 11主要支持Webpack源映射,但是您需要在Javascript Debug Run配置中相应地设置远程URL映射,以便让WebStorm知道带有Webpack输出文件的目录(包括源映射)以及如何在指定的源文件中指定源文件。 sourcemap映射到项目中的位置。因此,您需要指定已编译的脚本Web服务器URL到其本地路径的映射,并将源URL(在源映射中列出)映射到项目中的本地路径。听起来很奇怪,但并不复杂。对于像您这样的配置文件,您可能必须为包含文件和源图所在的http://localhost:63342/webpack/Built目录指定远程URL 'Built',并为webpack:///.目录指定'js'。这对我来说很好...我们计划很快发布关于webpack调试的博客文章...现在,我可以建议查看https://github.com/prigara/debugging-webpack这个简单的例子


1
投票

我绞尽脑汁待了几个小时,我希望它可以帮助别人。这篇博客文章中的说明实际上有效:https://blog.jetbrains.com/webstorm/2015/09/debugging-webpack-applications-in-webstorm/

因此,按照说明配置您的webstorm实例,但不要使用webpack-dev-server运行它,使用不同的Web服务器,如Ruby mine / rails中使用的WEBrick :: HTTPServer或内置调试服务器。由于某种原因,webpack-dev-server没有将源映射正确地与行号相关联。


0
投票

我想补充一点,你可以把声明

调试器;

在你的javascript / typescript中甚至在angular或vue2的框架文件中。

因此,即使您的路径映射到URL不起作用,它仍然会步进。

© www.soinside.com 2019 - 2024. All rights reserved.