gpt4 book ai didi

typescript - Webpack + React + typescript

转载 作者:搜寻专家 更新时间:2023-10-30 20:33:45 25 4
gpt4 key购买 nike

是否可以将 webpack 与 React 和 Typescript 一起使用,并能够将它们捆绑到一个网络包中,但仍然能够调试原始的 TypeScript 和 React 代码?在 webpack 中,我使用 ts-loader 和 ts-jsx-loader 加上 devtool: "source-map",但是当我尝试进行浏览器调试时,我看不到原始的 ts 代码,而是看到了已更改的代码通过 webpack。

我当前的基本 webpack.config.js 文件:

var webpack = require('webpack');
module.exports = {
entry: ['./app/main.ts'],
output: {
path: './build',
filename: 'bundle.js'
},
debug: true,
devtool: 'source-map',
plugins: [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin()
],
resolve: {
extensions: ['', '.ts', '.js']
},
module: {
loaders: [
{
test: /\.ts$/,
loader: 'ts-loader!ts-jsx-loader'
}
]
}
};

tsconfig.json:

{
"compileOnSave": false,
"version": "1.5.0-alpha",
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"noLib": false,
"sourceMap": true,
"noImplicitAny": true,
"removeComments": true
},
"files": [
"./AppComponent.ts",
"./libs/jsx.d.ts",
"./libs/react.d.ts",
"./libs/webpack-runtime.d.ts",
"./main.ts"
]
}

例如 - 我的原始 .ts 文件如下所示:

import React = require('react');

class AppComponent extends React.Component<any, any> {
render () {
return React.jsx(`
<h1>He world!</h1>
`);
}
};
export = AppComponent;

在 chrome 调试器中它看起来像这样:

var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var React = __webpack_require__(2);
var AppComponent = (function (_super) {
__extends(AppComponent, _super);
function AppComponent() {
_super.apply(this, arguments);
}
AppComponent.prototype.render = function () {
return (React.createElement("h1", null, "He world!"));
};
return AppComponent;
})(React.Component);
;
module.exports = AppComponent;


/*****************
** WEBPACK FOOTER
** ./app/AppComponent.ts
** module id = 158
** module chunks = 0
**/

最佳答案

您不需要使用 ts-jsx-loader。

在你的 tsconfig.json 文件中,你只需要这样的东西:

{
"compilerOptions": {
"jsx": "react",
"sourceMap": true,
// ... other options
}
}

当然,你仍然需要webpack配置文件中的devtool选项

关于typescript - Webpack + React + typescript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31390247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com