调试器断点行不正确,Webpack配置类型脚本

rryofs0p  于 2023-06-23  发布在  Webpack
关注(0)|答案(2)|浏览(121)

我终于让typescript在我的项目中工作了,但是断点并没有停在正确的行中,并且变量值是不可用的,直到你在一步一步的执行中进入更多的代码行。源Map中似乎有一些ttype,如果不匹配。我试图解决这个问题,将SourceMapDevToolPlugin添加到webpack配置文件中作为instructed here。但并不能解决问题。
下面是我的意思的截图:

myString是undefine的,尽管这一行应该已经被执行了。

在它直接跳转到函数之后(而不是调用函数的const myNumber = myFunc(5);),字符串的值是可用的,所以这太奇怪了。
下面是我的webpack配置,launch.json和tsconfig。
webpack配置:

const webpack = require('webpack');
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// const SourceMapDevToolPlugin = require('');

const modulesPath = path.resolve(__dirname, 'node_modules');
const srcPath = path.resolve(__dirname, 'src');
const outputPath = path.resolve(__dirname, 'dist');

const basename = process.env.BASENAME || '/';
const mode = process.env.NODE_ENV === 'production' ? 'production' : 'development';

module.exports = {
  mode,
  devtool: 'inline-source-map',
  devServer: {
    hot: true,
    historyApiFallback: true,
    port: 3000,
    stats: 'minimal',
  },
  entry: [
    path.join(srcPath, 'index.css'),
    path.join(srcPath, './trial.ts'),
  ],
  output: {
    path: outputPath,
    publicPath: basename,
  },
  resolve: {
    alias: {
      '@': srcPath,
    },
    extensions: ['.tsx', '.ts', '.js', '.js', '.json'],
  },
  module: {
    rules: [
      ...(mode === 'development' ? [
        {
          test: /\.(js)$/,
          enforce: 'pre',
          loader: 'eslint-loader',
          options: {
            emitWarning: true,
          },
          include: srcPath,
          exclude: modulesPath,
        },
      ] : []),
      {
        test: /\.(js)$/,
        loader: 'babel-loader',
        options: {
          presets: [
            ['@babel/preset-env', { modules: false }],
          ],
        },
        include: srcPath,
        exclude: modulesPath,
      },
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: { sourceMap: true },
          },
        ],
        include: [srcPath, modulesPath],
      },
      {
        test: /\.(vert|frag)$/,
        loader: 'raw-loader',
        include: srcPath,
        exclude: modulesPath,
      },
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: modulesPath,
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.join(srcPath, 'index.ejs'),
      title: 'WebGL boilerplate',
      favicon: './favicon.ico',
    }),
    new MiniCssExtractPlugin(),
    ...(mode !== 'production' ? [
      new webpack.NamedModulesPlugin(),
      new webpack.HotModuleReplacementPlugin(),
      new webpack.EvalSourceMapDevToolPlugin({
        moduleFilenameTemplate: (info) => (
          `file:///${info.absoluteResourcePath}`
        ),
      }),
    ] : []),
    new webpack.SourceMapDevToolPlugin({
      filename: null,
      exclude: [/node_modules/],
      test: /\.ts($|\?)/i,
    }),
  ],
};

launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "chrome",
            "request": "launch",
            "name": "Launch Chrome",
            "url": "http://localhost:3000",
            "webRoot": "${workspaceFolder}/src",
            // "preLaunchTask": "tsc",
            "sourceMaps": true,
            "sourceMapPathOverrides":{
                "webpack:///./*": "${webRoot}/*",
                "webpack:///src/*": "${webRoot}/*",
                "webpack:///*": "*",
                "webpack:///./~/*": "${webRoot}/node_modules/*",
                "meteor://💻app/*": "${webRoot}/*"
            }
        }
    ]
}

tsconfig:

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */

    /* Basic Options */
    // "incremental": true,                   /* Enable incremental compilation */
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "inlineSourceMap": true,                  /* Emit a single file with source maps instead of having a separate file. */
    "inlineSources": true,                    /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

    /* Advanced Options */
    "skipLibCheck": true,                     /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
    "outDir": "./src/ts-built/",
    "rootDir": "./src/"
  }
}

注意,// "preLaunchTask": "tsc";被注解了。这是编译typescript文件的prelaunch任务,然而,一旦我将webpack和tsc配置为使用'inline-source-map'模式,即使是prelaunchtask注解的.js编译文件,应用程序也会运行,在typescript文件中有制动点(奇怪的行为是一行不匹配)。
所以我不知道我在inline-source-map模式配置中缺少了什么,或者是否应该使用普通的'souceMAps': true模式,其中源代码Map是在类型脚本文件编译时生成的。
非常感谢提前任何帮助。

gpfsuwkq

gpfsuwkq1#

我将tsconfig更改为SourMaps:true;模式,解决了这个问题。下面是更正后的tsconfig:

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */

    /* Basic Options */
    // "incremental": true,                   /* Enable incremental compilation */
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    //"inlineSourceMap": true,                  /* Emit a single file with source maps instead of having a separate file. */
    //"inlineSources": true,                    /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
    "sourceMap": true,
    /* Advanced Options */
    "skipLibCheck": true,                     /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
    "outDir": "./src/ts-built",
    "rootDir": "./src"
  }
}

据我研究,我想我得到了"sourceMap": true模式的工作原理。tsc编译器生成的.map文件用于在调试时将编译后的.jsMap到.ts文件。看起来//"inlineSourceMap": true,//"inlineSources": true,是从.js文件末尾生成的文本获取源Map,而不是分离的.map文件,而这种模式正是我所期望的,因为webpack配置中devtool的名称是'inline-source-map'。我不知道这两种模式是否可以在webpack中使用,只要其中一种工作正常就可以了。但是,如果只有NO inline模式工作,并且devtool名称为'inline-source-map',我发现这非常容易误导。
感谢您在这方面的任何解释性意见。

z4bn682m

z4bn682m2#

当我开发grafana-plugin时,这个被接受的答案并不适用。我不知道有没有什么特别的地方。奋斗了几天,终于找到了这个bug:https://github.com/swc-project/jest/issues/61
所以sourceMap=true是不够的,但我们必须使用inlineSourceMap来代替。我们必须同时更改以下两个文件才能使其工作:
1.在.config/jest.config.js中:change sourceMaps: "inline"来自sourceMaps: true
1.在.config/tsconfig.json中:为compilerOptions添加"inlineSourceMap": true

相关问题