Webpack doesn't bundle TypeScript

Petr Shypila picture Petr Shypila · Mar 14, 2018 · Viewed 13k times · Source

I run into the issue of bundling my TS projects. It seems like ts-loader doesn't recognise TypeScript syntax.

The errors I receive:

ERROR in ./src/common/components/chat/chatComponent.tsx
Module parse failed: The keyword 'interface' is reserved (10:0)
You may need an appropriate loader to handle this file type.
| import { ChatItemComponent, ChatMessageItem, isMessage, Props as Item } from "./chatItem";
|
| interface Props {
|   className?: string;
|   fullHistoryFetched?: boolean;

And

ERROR in ./src/common/closer.tsx
Module parse failed: Unexpected token (10:13)
You may need an appropriate loader to handle this file type.
| import * as chatItem from "./components/chat/chatItem";
| import * as chatItemStyles from "./components/chat/chatItem.css";
| import Timer = NodeJS.Timer;
| import { ImageComponent } from "./components/image/image";
| import { InitialsAvatar } from "./components/initialsAvatar";

My webpack.config.js is following:

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

module.exports = {
    mode: "development",
    devtool: "inline-source-map",
    entry: {
        index: './src/agent/index.tsx',
        preloader: './src/agent/preloader.js',
    },
    target: 'web',
    output: {
        path: path.resolve(__dirname, 'dist/agent'),
        filename: '[name].js',
        publicPath: '/dist/agent/'
    },
    module: {
        rules: [
            {
                test: /\.(ts|tsx)?$/,
                include: path.resolve(__dirname, 'src/agent'),
                use: [
                    {
                        loader: 'ts-loader'
                    }
                ]
            },
            ...
        ]
    },
    resolve: {
        extensions: ['.ts', '.tsx', '.js', '.jsx', '.json']
    }
};

And here is my tsconfig.json

{
  "buildOnSave": false,
  "compileOnSave": false,
  "compilerOptions": {
    "alwaysStrict": true,
    "experimentalDecorators": true,
    "jsx": "react",
    "module": "commonjs",
    "noFallthroughCasesInSwitch": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "pretty": true,
    "removeComments": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strictNullChecks": true,
    "target": "es5",
    "lib": [
      "es2015",
      "dom"
    ],
    "types": [
      "jasmine",
      "node",
      "react"
    ]
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

The versions I use:

"typescript": "2.5.3", "ts-loader": "4.0.1", "webpack": "^4.1.1.

Am I missing something? In my tsconfig.json file I target sources to be generated into ES5 so from my knowledge I don't need babel.

Answer

lukas-reineke picture lukas-reineke · Mar 14, 2018

The include is the problem. You limit the ts-loader to only src/agent. The error you get is for a file in src/common.
include can also be an array, just pass every folder that has typescript files.