【发布时间】:2019-08-31 07:35:33
【问题描述】:
我正在尝试设置 VScode 来调试电子应用程序,但是当我尝试开始调试时,我的断点都未得到验证。
我很确定这是一个源映射问题,来自我所做的研究,而且所有日志都引用了捆绑代码中的行号。
electron版本是2.0.0,使用的webpack版本是4.8.2。
实际上有大约九个不同的 webpack.config.js 文件,用于环境,因此它可以构建为电子应用程序,或作为 chrome 的标准 Web 应用程序。话虽如此,我认为这是将代码捆绑到各种库中的相关 webpack 文件:
const webpack = require('webpack');
const baseConfig = (root, src, options, entry, name) => {
return {
entry: entry,
output: {
filename: '[name]-bundle.js',
path: `${root}${options.output.path}/libs`,
library: name,
libraryTarget: 'umd',
chunkFilename: '[name]-bundle.js'
},
plugins: [
new webpack.ProvidePlugin({
_: 'lodash'
}),
new webpack.DefinePlugin(options.defines)
],
target: options.platform.lib,
context: src,
resolve: {
modules: [
src,
'node_modules'
],
alias: {
app: 'app',
util: 'app/util',
robot: 'app/robot',
platform: 'platform/' + options.platform.name
},
extensions: ['.json', '.js']
},
module: {
rules: [
{
test: /\.js$/,
loader: 'eslint-loader',
enforce: 'pre',
exclude: /node_modules/
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.jpe?g$|\.gif$|\.png$|\.svg$|\.mp4$|\.woff2?$|\.otf$|\.ttf$|\.eot$/,
loader: 'file-loader'
}
]
}
};
};
module.exports = (root, src, options) => {
const configs = [];
const libraries = [
{
entry: {messagingservice: 'util/messaging/messaging-service.js'},
name: 'MessagingService'
},
{
entry: {messagingclient: 'util/messaging/messaging-client.js'},
name: 'MessagingClient'
},
{
entry: {utils: 'util/utils-helper.js'},
name: 'Utils'
}
];
libraries.forEach(library => {
const config = baseConfig(root, src, options, library.entry, library.name);
if (options.sourceMap) {
config.devtool = 'source-map';
}
configs.push(config);
});
return configs;
};
这里是开发环境的配置:
module.exports = {
defines: {
VERBOSE: true,
SHOW_DEV_TOOLS: true,
},
output: {
path: 'dev'
},
clean: true,
sourceMap: true,
mode: 'development',
};
最后是 vscode 中的 launch.json 文件:
{
"type": "node",
"request": "launch",
"name": "Electron: Main",
"protocol": "inspector",
"cwd": "${workspaceFolder}/build/electron/dev",
"program": "${workspaceRoot}/build/electron/dev/index.js",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron.cmd"
},
"runtimeArgs": [
"--enable-logging",
"--remote-debugging-port=9223"
],
"sourceMaps": true,
"outFiles": [
"${workspaceFolder}/build/electron/dev/**"
]
},
我真正苦苦挣扎的是弄清楚我是否应该将时间花在 webpack 文件中(它们确实会创建源映射),是否这一切都与让 launch.json 正确找到源映射有关.
VScode 确实提供了这个模板:
"sourceMapPathOverrides": {
"webpack:///./~/*": "${workspaceRoot}/node_modules/*",
"webpack:///./*": "${workspaceRoot}/*",
"webpack:///*": "*"
}
但我有点不清楚我要在左侧和右侧匹配哪些路径。我认为这个模板已经过时了,workspaceRoot 现在应该是workspaceFolder,但是我不确定为什么这个模板让我给出节点模块文件夹的路径,或者我是否需要确保这些与路径等效我在 outFiles 中提供。
还有我最关心调试的文件在此文件夹${workspaceFolder}/build/electron/dev/libs/ 中,但我也无法在 dev 文件夹中的文件中发生断点。
任何关于我应该将精力集中在哪里的见解将不胜感激。
【问题讨论】:
标签: webpack electron vscode-debugger