首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-templates、TypeScript和webpack之间的和谐

react-templates、TypeScript和webpack之间的和谐
EN

Stack Overflow用户
提问于 2016-08-06 20:29:09
回答 1查看 1K关注 0票数 3

我正在尝试让TypeScriptreact-templateswebpack一起玩。

我从https://www.typescriptlang.org/docs/handbook/react-&-webpack.html的示例代码开始。webpack.config.js文件包含

代码语言:javascript
复制
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包含

代码语言:javascript
复制
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-templatesreact-templates-loader之后,我将以下行添加到webpack.config.jsloaders部分

代码语言:javascript
复制
{test: /\.rt$/, loaders: ['react-templates-loader?modules=typescript']}

并更新了extensions部分以包含.rt。我从Hello.tsx中提取了嵌入式超文本标记语言

代码语言:javascript
复制
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

代码语言:javascript
复制
<h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>

我得到以下错误:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2016-09-14 18:02:30

我在这里给出了一些提示:https://github.com/wix/react-templates/issues/193

基本上切换回commonjs模块并使用import template = require('./Hello.rt')在Typescript中导入

如果提示不够充分,还需要帮助,请告诉我。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38804312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档