我正在尝试让TypeScript、react-templates和webpack一起玩。
我从https://www.typescriptlang.org/docs/handbook/react-&-webpack.html的示例代码开始。webpack.config.js文件包含
module.exports = {
entry: "./src/index.tsx",
output: {
filename: "./dist/bundle.js",
},
// Enable sourcemaps for debugging webpack's output.
devtool: "source-map",
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
},
module: {
loaders: [
// All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'.
{ test: /\.tsx?$/, loader: "ts-loader" }
],
preLoaders: [
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ test: /\.js$/, loader: "source-map-loader" }
]
},
// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
externals: {
"react": "React",
"react-dom": "ReactDOM"
},
};和Hello.tsx包含
import * as React from "react";
export interface HelloProps { compiler: string; framework: string; }
export class Hello extends React.Component<HelloProps, {}> {
render() {
return <h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>;
}
}所有必需的npm包都已安装。当我运行webpack时,应用程序运行正常。
如果在使用npm安装react-templates和react-templates-loader之后,我将以下行添加到webpack.config.js的loaders部分
{test: /\.rt$/, loaders: ['react-templates-loader?modules=typescript']}并更新了extensions部分以包含.rt。我从Hello.tsx中提取了嵌入式超文本标记语言
import * as React from "react";
import template from "./Hello.rt"
// Also tried `import * as template from "./Hello.rt"`
export interface HelloProps {
compiler: string;
framework: string;
}
export class Hello extends React.Component<HelloProps, {}> {
render() {
return template.apply(this);
}
}和Hello.rt
<h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>我得到以下错误:
ERROR in ./src/components/Hello.tsx
(2,22): error TS2307: Cannot find module './Hello.rt'.
ERROR in ./src/components/Hello.rt
Module parse failed: node_modules/react-templates-loader/index.js?modules=typescript!src/components/Hello.rt Unexpected token (1:13)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (1:13)加上堆栈跟踪。
有没有办法让TypeScript,react-templates和webpack一起玩?我在https://github.com/bgrieder/react-templates-typescript-test上找到了一个将TypeScript与react-templates和grunt/browserify结合使用的示例,但我更喜欢使用webpack。
发布于 2016-09-14 18:02:30
我在这里给出了一些提示:https://github.com/wix/react-templates/issues/193
基本上切换回commonjs模块并使用import template = require('./Hello.rt')在Typescript中导入
如果提示不够充分,还需要帮助,请告诉我。
https://stackoverflow.com/questions/38804312
复制相似问题