Ralph
Ralph

Reputation: 32294

Harmony between react-templates, TypeScript, and webpack

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.

Upvotes: 3

Views: 1134

Answers (1)

nino.porcino
nino.porcino

Reputation: 1183

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.

Upvotes: 2

Related Questions