【问题标题】:Visual Studio debug not working on typescript (angular) - Visual Studio 2017 keeps skipping breakpointsVisual Studio 调试不适用于打字稿(角度) - Visual Studio 2017 不断跳过断点
【发布时间】:2018-09-08 12:44:37
【问题描述】:
// ============================== see updates below ============================== //

我试图在 Visual Studio 2017(不是 Visual Studio Code)上调试打字稿应用程序,但是当我在 .ts 文件上插入断点时,Visual Studio 告诉我: 断点当前不会被命中没有可执行代码与此行相关联

我想我已经尝试了所有互联网建议的解决方案,但没有任何帮助我解决这个问题,而且似乎没有任何效果。

实际上,这个问题只存在于一个项目中。

我的意思是,我有另一个项目,我可以在 Visual Studio 2017 上使用断点调试打字稿,并且我可以在 ts 文件上使用它们,所以 我认为这不是设置问题。

现在,我的 typescript 调试设置由 tsconfig.json 管理,我认为这个问题可能是由于其中的某些错误,或者可能是在我的 webpack.config.js 文件中。然而,这只是一个假设。 然而: tsconfig.json 内容如下:

{
  "compilerOptions": {
    "target": "es5",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "types": [ "node", "jasmine", "core-js" ],
    "noEmitOnError": true
  },
  "compileOnSave": true,
  "exclude": [
    "node_modules",
    "wwwroot/lib",
    "bin",
    "obj"
  ]
}

Webpack 配置文件内容如下:

const path = require('path');
const webpack = require('webpack');

module.exports = {
    context: __dirname,
    resolve: { extensions: ['.ts', '.js'] }, // .ts is first so that .ts files are prefered over js file, this ensures
    // that angular 2 components are passed through the angular2-template-loader and have their templates and styles inlined
    entry: {
        'polyfills': './App/polyfills.ts',
        'main': './App/main.ts'
    },
    output: {
        path: path.join(__dirname, './wwwroot/dist'),
        filename: '[name].js',
        publicPath: '/dist/'
    },
    module: {
        rules: [

            { test: /\.ts$/, include: /App/, use: ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] },
            { test: /\.html$/, use: 'html-loader?minimize=false' },
            { test: /\.css$/, use: ['to-string-loader', 'css-loader'] }
        ]
    },
    plugins: [
        new webpack.DllReferencePlugin({
            context: __dirname,
            manifest: require('./wwwroot/dist/vendor-manifest.json')
        }),
        new webpack.optimize.UglifyJsPlugin(),
        new webpack.HotModuleReplacementPlugin()
    ]
};

最后,我正在使用 Microsoft.AspNetCore.SpaServices.Webpack; 来启用 webpack 热模块替换,这在我的 startup.cs 中:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            // HMR //
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
                {
                    HotModuleReplacement = true
                });
            }
            // HMR //

//(...) do more stuff under here

您对此有任何解决方案或故障排除吗?

[如果您需要更多信息,请告诉我]

// =============================================================================== //

更新 #1

看起来,在 Visual Studio 2017 和 dot net v.2+ 中集成在微软的 asp net core web 应用程序项目中的 angular 项目天生就有工作断点和调试选项。

不幸的是,这只是另一个问题。它不是 Angular 5 原生应用,而是 Angular 4 应用!此外,任何软件包都是最新的。必须手动更新每一个。

一旦我更新了它们,它似乎就可以工作了。在那个应用程序中,断点有效!
但是我不知道为什么...

【问题讨论】:

  • 您不能在 Typescript 上放置断点,因为生成的 javsacript 是在浏览器上执行的,而不是在您的 VS 代码实例中。您必须在浏览器的源映射中设置断点。
  • @chrispy 谢谢...但是,在 Visual Studio 2017 上的另一个 Angular 应用程序中,我可以在 .ts 文件上放置断点,它们工作正常。这东西怎么可能?我的意思是,为什么我可以在该应用程序的打字稿文件上放置断点,但不能在另一个应用程序中放置断点?我的意思是,一定有办法让它们发挥作用,我认为
  • @chrispy 我开始认为(但这只是一个假设)由于 Webpack 的 AngularCompilerPlugin 插件(来自 ('@ngtools/webpack').AngularCompilerPlugin),断点可以在另一个项目上工作.在您看来,这个假设有意义吗? PS:我使用的是 Visual Studio 2017,而不是 Visual Studio 代码

标签: angular typescript debugging webpack visual-studio-2017


【解决方案1】:

将 webpack 上下文设置为 ClientApp 目录(或 TS 示例中的 App)为我完成了这项工作:

  context: path.join(__dirname, "ClientApp"),
  entry: {
    //'main': './ClientApp/main.ts'
    'main': './main.ts'
  }

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,但是是的,正如您在更新 #1 中所说,新的 Visual Studio 正确的 Angular 项目提供了工作调试。
    在我的应用程序中,我发现将包更新到最新版本以及将旧项目复制到具有工作断点的新项目中更容易。

    但是..如果您仔细阅读新 VS 集成 Angular 应用程序中的 webpack.config.js 文件,您肯定会注意到以下字符串:

            new webpack.SourceMapDevToolPlugin({
                filename: '[file].map', // Remove this line if you prefer inline source maps
                moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
    

    实际上,SourceMapDevToolPlugin 提供 - 如果配置正确 - 实时映射,这使得使用断点成为可能。
    您选择是否使用在线源地图。

    您还应该设置属性:devtool: 'inline-source-map'
    并启用 UglifyPlugin 的源地图。

    我发现的唯一缺点是:

    • 从应用启动的第一刻起,断点不可用,您必须等待几秒钟
    • 使用断点有点慢
    • 使用 chrome 控制台禁用断点

    【讨论】:

    • 我用的是Angular 5,项目里没有webpack.config.js文件。
    • @Bigeyes 我的答案已经过时了(而且这个问题似乎也过时了,OP 也关闭了他/她的帐户,所以......),因为从 Angular 4 开始(如果我没记错的话) , webpack.config 被隐藏。您可以使用ng eject 命令将其弹出,但相信我,编辑 webpack.js 是一个雷区,我个人宁愿完全不碰它。顺便说一句,如果您正在寻找调试 Angular 应用程序的方法,我建议您使用 chrome 的 devtools
    • @Bigeyes 简而言之,要使用 Chrome 开发工具进行调试,只需打开 Chrome 的控制台并转到“源”选项卡。然后,在左侧树视图中查找“webpack”元素并展开它。然后展开点文件夹(“.”),从那里您可以调试整个应用程序或至少调试已加载的模块(如果您使用延迟加载)。 You can also debug with VS Code 但最终它依赖于 Chrome Devtools 所以在我看来使用 Chrome Devtools 似乎是最好的选择
    • 其实我试过4200端口,还是不行。坚果它适用于launchsetting.json中的端口
    • IR/Edge 仍然无法正常工作。我为 Visual Studio Code 下载了 chrome 调试器(marketplace.visualstudio.com/…)。然后我使用 chrome 调试器在 Visual Studio(不是 Visual Studio Code)中进行调试。非常有趣的是,如果我没有 Visual Studio Code 的 chrome 调试器,那么在 Visual Studio(不是 Visual Studio Code)中使用 chrome 调试器会失败。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多