簡體   English   中英

webpack tsx 模塊解析失敗:意外令牌

[英]webpack tsx Module parse failed: Unexpected token

我正在嘗試在 TypeScript 中編寫前端(React)和后端(Express)。 目前,我在根文件夾中的webpack.config.js遇到錯誤,即使我有ts-loader也是如此。

這是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;

這是我的tsconfig.json

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

我得到這樣的錯誤:

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]

tsconfig中的include部分更改為以下內容

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

感謝 FunkeyFlo 的回答,我自己找到了答案。 我必須改變兩者:

  • tsconfig.jsoninclude src/**/*.tsx
  • webpack.config.jsentry ./dist/src/client/User/index

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM