调试 WebPack 生成的代码的方法是什么?

What is the way to debug code generated by WebPack?

提问人:Cashif Ilyas 提问时间:10/8/2023 最后编辑:Cashif Ilyas 更新时间:10/8/2023 访问量:24

问:

这可能是一个非常幼稚的问题,但我还是会问它。我的背景是,我会在没有 Typescript 和 Webpack 的情况下编写 Chrome 扩展程序。它运行良好,因为我可以将我的确切代码视为背景脚本和内容脚本,因此无论不同上下文之间的通信多么复杂,调试都非常容易。

最近,我看到越来越多的项目使用 Typescript 和 Webpack 来编写 chrome 扩展。在调试这些代码时,我遇到了一个问题,即代码看起来与原始代码有很大不同(当然!),因此,很难实际调试上下文之间发生的任何消息传递。源映射已经启用,我可以在 DevTools 中按名称打开文件,但尽管如此,代码看起来像一堆语句和一些奇怪的外观和函数。我的问题是,你们如何在不发疯的情况下调试此类应用程序?一定有办法,否则人们就不会加入 WebPack 的行列。switch__generator__awaiter

下面是一个示例:

原始代码片段:

  static async getSearchResultsForSavedList(): Promise<Array<ParsedUser>> {
    const source = await waitForPageSource('[class="profile-card-col"]');

    let domProfiles = $(source).find('[class="profile-card-col"]');

    if (!domProfiles?.length) {
      return [];
    }

    const defaultPhoto = browserAPI.getURL('images/user.png');
...

我在 DevTools 中看到的代码:

RecruiterParser.getSearchResultsForSavedList = function () {
        return __awaiter(this, void 0, void 0, function () {
            var source, domProfiles, defaultPhoto;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, waitForPageSource('[class="profile-card-col"]')];
                    case 1:
                        source = _a.sent();
                        domProfiles = $(source).find('[class="profile-card-col"]');
                        if (!(domProfiles === null || domProfiles === void 0 ? void 0 : domProfiles.length)) {
                            return [2 /*return*/, []];
                        }
...
javascript typescript 调试 webpack google-chrome-extension

评论

0赞 wOxxOm 10/8/2023
如果你有源代码,你应该通过添加devtool: 'inline-source-map'

答:

0赞 Cashif Ilyas 10/8/2023 #1

好的,所以帮助我摆脱所有这些奇怪的陈述和奇怪的功能的是更改为(或更高)。switchtargettsconfig.jsones6

它被设置为,因此 TS 编译器将其编译成看起来很奇怪的 javascript。es5

评论

1赞 wOxxOm 10/8/2023
您应该使用 browserslist 将 Chrome/Firefox 的最低版本设置为 ManifestV3 的 100 或更高,如果您使用最近出现的新 API。它将保留所有现代 JS 语法,如 、 等。?.??...