我终于让打字稿在我的项目中工作了,但是断点不会停在正确的行上,并且变量值不可用,直到您在逐步执行中进入更多行代码。如果源映射中不匹配,则似乎存在某些 ttype。 我尝试按照here的说明将 SourceMapDevToolPlugin 添加到 webpack 配置文件中来解决此问题。 但并不能解决问题。
下面的截图说明了我的意思:
myString 未定义,尽管该行应该已被执行。
在它直接跳转到函数之后(而不是跳转到调用函数的
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}/*"
}
}
]
}
ts配置:
{
"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 文件的预启动任务,但是,一旦我配置了 webpack 和 tsc 以使用 'inline-source-map'
模式,即使是 prelaunchtask 注释的 .js 编译文件,应用程序也会运行,并在 typescript 文件中带有刹车点(行为奇怪,线路不匹配)。
所以我不知道我在内联源映射模式配置中缺少什么,或者是否应该使用正常的
'souceMAps': true
模式,其中源映射是在打字稿文件编译时生成的。
非常感谢您的帮助。
我将 tsconfig 更改为
sourceMap: 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
模式是如何工作的。
//"inlineSourceMap": true,
和//"inlineSources": true,
从.js文件末尾生成的文本而不是分离的.map文件中获取源映射,并且该模式是我期望的名称webpack 配置中的 devtool 是 'inline-source-map'
。我不知道这两种模式是否都可以在 webpack 中使用,只要两种模式之一有效就可以。但是,如果只有 NO 内联模式有效,并且开发工具名称是
'inline-source-map'
,我发现这相当具有误导性。
感谢您对此方面的任何解释性评论。
当我开发grafana-plugin时,接受的答案对我不起作用。不确定那里有没有什么特别的。折腾了几天,终于找到这个bug了:https://github.com/swc-project/jest/issues/61
所以
sourceMap=true
还不够,我们还得用inlineSourceMap
来代替。而且我们必须同时更改以下两个文件才能使其正常工作:
.config/jest.config.js
中:change sourceMaps: "inline"
来自sourceMaps: true
.config/tsconfig.json
中:为 "inlineSourceMap": true
添加
compilerOptions