【发布时间】:2020-10-10 14:54:42
【问题描述】:
我终于在我的项目中使用了 typescript,但是断点并没有在正确的行中停止,并且变量值不可用,直到您在逐步执行中进入更多代码行。如果源映射不匹配,似乎有一些 ttype。 我试图解决这个问题,将 tje SourceMapDevToolPlugin 添加到 webpack 配置文件中,如here 的指示。 但并没有解决问题。
下面是我的意思的截图:
myString 未定义,尽管该行应该已被执行。
直接跳转到函数后(不是跳转到调用函数的const myNumber = myFunc(5);),并且字符串的值是availabe,所以这很奇怪。
在我的 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"; 已被注释。那是编译打字稿文件的预启动任务,但是,一旦我将 webpack 和 tsc 配置为使用 'inline-source-map' 模式,即使是预启动任务注释的 .js 编译文件,应用程序也会运行,并且打字稿文件中有断点(行为怪异,行不匹配)。
所以我不知道我在 inline-source-map 模式配置中缺少什么,或者是否应该使用正常的'souceMAps': true 模式,其中源映射是在 typescript 文件编译时生成的。
非常感谢您的任何帮助。
【问题讨论】:
-
您确定吗,自从您评论了
tsc任务后,代码每次都在编译?因为源图是由tsc添加的 -
即使我取消注释代码,也会发生同样的情况。为什么我解释注释行的事情是因为如果我注释该行并删除 .js 编译文件,应用程序运行并且断点命中无论如何,我认为是由于 inline-source-map 配置。我认为它是在某处或其他地方创建的,因为在这种情况下,我看不到任何 .js 文件和 .map 文件生成,但是无论如何,刹车点都会命中,但虽然不正确
-
你能检查一下这个解决方案吗github.com/facebook/create-react-app/issues/…
-
我在 repo 中说明的选项 webpackConfig 中尽可能地添加了 sourceMap: true ,每次我为每个规则部分添加时,都会运行 1 by 1(我的意思是添加更改 1 by te have如果问题已解决,请明确问题出在哪里)。不解决问题。我还尝试将 MiniCssExtractPlugin sourmap 选项切换为 true 和 false 以防可能产生影响...
标签: typescript webpack visual-studio-code source-maps