necroface
necroface

Reputation: 3465

webpack tsx Module parse failed: Unexpected token

I am trying to write both frontend (React) and backend (Express) in TypeScript. At the moment, my webpack.config.js in the root folder encounters an error even though I have ts-loader for it.

This is webpack.config.js

const webpack = require('webpack');
const path = require('path');

const config = {
    cache: true,
    mode: 'development',
    entry: {
        'user': ['./src/client/User/index.tsx', 'webpack-hot-middleware/client']
    },
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/static'
    },
    devServer: {
        contentBase: './dist',
        hot: true
    },
    plugins: [new webpack.HotModuleReplacementPlugin()],
    resolve: {
        extensions: ['.tsx', '.ts', '.js']
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                exclude: /node_modules/,
                include: path.join(__dirname, 'client')
            }
        ]
    },
    node: { fs: 'empty' }
};

module.exports = config;

And this is my tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "moduleResolution": "node",
        "jsx": "react",
        "esModuleInterop": true,
        "outDir": "dist",
        "sourceMap": true,
        "baseUrl": "."
    },
    "include": ["src/**/*.ts"],
    "exclude": ["node_modules"]
}

I got the error like this:

ERROR in ./src/client/User/index.tsx 10:1
Module parse failed: Unexpected token (10:1)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| const rootComponent = (
>       <Provider store={store}>
|               <BrowserRouter>
|                       <div id="page-container" className="sidebar-o sidebar-dark enable-page-overlay side-scroll page-header-fixed side-trans-enabled">
 @ multi ./src/client/User/index.tsx webpack-hot-middleware/client user[0]

Upvotes: 1

Views: 5806

Answers (2)

necroface
necroface

Reputation: 3465

Thanks to FunkeyFlo answer, I found out the answer myself. I have to change both:

  • tsconfig.json: include also src/**/*.tsx
  • webpack.config.js: entry to ./dist/src/client/User/index

Upvotes: 2

FunkeyFlo
FunkeyFlo

Reputation: 295

change include section in tsconfig to the following

{
    ...
    "include": [
        "src/**/*.ts", 
        "src/**/*.tsx"
    ],
    ...
}

Upvotes: 1

Related Questions