3

I am trying to get TypeScript, react-templates, and webpack to play together.

I started from the sample code at https://www.typescriptlang.org/docs/handbook/react-&-webpack.html. The webpack.config.js file contains

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"
    },
};

and Hello.tsx contains

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>;
    }
}

All required npm packages have been installed. When I run webpack, the application runs correctly.

If, after using npm to install react-templates and react-templates-loader, I added the following line to the loaders section of webpack.config.js

{test: /\.rt$/, loaders: ['react-templates-loader?modules=typescript']}

and updated the extensions section to include .rt. I extracted the embedded HTML from 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);
    }
}

and Hello.rt:

<h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>

I get the following errors:

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)

plus a stack trace.

Is there any way to get TypeScript, react-templates, and webpack to play together? I found an example of using TypeScript with react-templates and grunt/browserify at https://github.com/bgrieder/react-templates-typescript-test, but I would rather use webpack.

3
  • where you success on this topic? I am having exactly the same problem! Thanks! Commented Aug 24, 2016 at 0:18
  • I have not gotten any further with this. I switched to Angular 2 and am having better luck. Commented Aug 24, 2016 at 12:58
  • I acomplished this creating a custom node program that imported the react-templates package and using glob to find the *.rt file inside the source folder I compiled them and write them back into folders Commented Aug 24, 2016 at 20:53

1 Answer 1

2

I gave some hints here: https://github.com/wix/react-templates/issues/193

Basically switch back to commonjs modules and import in Typescript with import template = require('./Hello.rt')

Please let me know if the hints aren't enough and still need help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.