简体   繁体   English

webpack tsx 模块解析失败:意外令牌

[英]webpack tsx Module parse failed: Unexpected token

I am trying to write both frontend (React) and backend (Express) in TypeScript.我正在尝试在 TypeScript 中编写前端(React)和后端(Express)。 At the moment, my webpack.config.js in the root folder encounters an error even though I have ts-loader for it.目前,我在根文件夹中的webpack.config.js遇到错误,即使我有ts-loader也是如此。

This is webpack.config.js这是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这是我的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]

change include section in tsconfig to the followingtsconfig中的include部分更改为以下内容

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

Thanks to FunkeyFlo answer, I found out the answer myself.感谢 FunkeyFlo 的回答,我自己找到了答案。 I have to change both:我必须改变两者:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Webpack React 错误:模块解析失败:意外的令牌 - Webpack React error: Module parse failed: Unexpected token Webpack + React:模块解析失败:意外的令牌:您可能需要一个合适的加载器 - Webpack + React: Module parse failed: Unexpected token: You may need an appropriate loader React App:“模块解析失败:意外令牌” - React App: "Module parse failed: Unexpected token" Vue 模块解析失败:意外的令牌 - Vue Module parse failed: Unexpected token 添加类字段时出现“模块解析失败:意外令牌” - "Module parse failed: Unexpected token" when added the class field 模块解析失败:带有 babel-loader 的意外令牌 (9:37) - Module parse failed: Unexpected token (9:37) with babel-loader 模块解析失败:意外的令牌谷歌电子表格 - Module parse failed: Unexpected token google-spreadsheet 模块解析失败:意外字符&#39;@&#39; - Module parse failed: Unexpected character '@' Webpack 4:模块解析失败:意外字符“@”。 你可能需要一个合适的加载器来处理这种文件类型…… - Webpack 4: Module parse failed: Unexpected character '@'. You may need an appropriate loader to handle this file type… 我需要哪些额外的装载机? (模块解析失败:意外令牌 (16:9)) - What additional loaders do I need? (Module parse failed: Unexpected token (16:9))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM