gpt4 book ai didi

reactjs - 网页包 4 : Module parse failed: Unexpected token

转载 作者:搜寻专家 更新时间:2023-10-30 21:57:20 26 4
gpt4 key购买 nike

在我的构建过程中,webpack 给我这个错误:

ERROR in ./client/components/App/index.tsx 15:9 Module parse failed: Unexpected token (15:9) You may need an appropriate loader to handle this file type.

|
|
> const App: SFC = () => (
| <div style={{ background: "red" }}>
| <h3>test</h3>

@ ./client/index.tsx 11:4-14:6 12:24-51 @ multi react-hot-loader/patch ./client/index.tsx webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000&reload=true

这是我的webpack.config.ts:

import CleanWebpackPlugin from "clean-webpack-plugin";
import HtmlWebpackPlugin from "html-webpack-plugin";
import path from "path";
import { Configuration, HotModuleReplacementPlugin } from "webpack";

const rootDir = ["..", "..", "..", ".."];
const distDir = ["..", ".."];

// this file lives in one place as `.ts` and another as `.js` grab the
// file extension to determine the include path relative to its location
const include =
path.extname(module.id) === ".ts"
? path.resolve(__dirname, "client", "index.tsx")
: path.resolve(__dirname, ...rootDir, "client", "index.tsx");
const exclude = /node_modules/;
const tsconfig = path.resolve(
__dirname,
...rootDir,
"config",
"tsconfig.client.json"
);

// development plugins
const plugins = [
new HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "..", "..", "..", "index.html")
}),
new CleanWebpackPlugin([path.resolve(__dirname, ...distDir, "*.*")], {
allowExternal: true,
root: __dirname,
verbose: true
})
];

// script for webpack-hot-middleware
const hotMiddlewareScript: string =
"webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000&reload=true";

const webpackDevConfig: Configuration = {
context: path.resolve(__dirname, ...rootDir),
devtool: "source-map",
entry: {
app: ["react-hot-loader/patch", include, hotMiddlewareScript]
},
mode: "development",
module: {
rules: [
{
exclude,
include,
test: /\.css$/,
use: ["style-loader", "css-loader"]
},
{
exclude,
include,
loader: "ts-loader",
options: {
configFile: tsconfig,
transpileOnly: true
},
test: /\.tsx?$/
},
{
enforce: "pre",
exclude,
include,
loader: "source-map-loader",
test: /\.js$/
}
]
},
optimization: {
nodeEnv: "development"
},
output: {
filename: "[name].bundle.js",
path: path.join(__dirname, ...distDir),
publicPath: path.join(__dirname, ...distDir, "static/")
},
plugins,
resolve: {
extensions: [".js", ".ts", ".tsx", "*"]
},
target: "web"
};

export default webpackDevConfig;

我的 App.tsx:

import React, { SFC } from "react";
import ReactDOM from "react-dom";

const App: SFC = () => (
<div style={{ background: "red" }}>
<h3>test</h3>
</div>
);

我的 index.tsx:

import React from "react";
import ReactDOM from "react-dom";
import { App } from "./components";

ReactDOM.render(<App />, document.getElementById("app"));

// enables Hot Module Replacement (HMR)
if ((module as any).hot) {
(module as any).hot.accept("./components/App", () => {
// for HMR to work, `App` must be re-required
const NextApp = require("./components/App").default;
ReactDOM.render(<NextApp />, document.getElementById("app"));
});
}

我的tsconfig.json:

{
"compilerOptions": {
"allowJs": true,
"jsx": "react",
"module": "commonjs",
...
}
}

错误本身似乎给出了解决方案:You may need an appropriate loader to handle this file type.,然而,据我了解,ts-loader 应该能够处理 react 。

Herets-loader 团队提供的 webpack.config 示例,用于使用 typescript 和 react 的应用程序。该设置与我自己的设置非常相似,但是,我不使用 webpack-dev-server,而是使用 webpack-dev-middleware

最佳答案

问题已由 niba 解决对原始问题的评论。似乎 ts-loader 在给定单个模块 include 时不会遍历链接的模块。删除 include 字段或使用文件夹名称为我修复了此错误。

关于reactjs - 网页包 4 : Module parse failed: Unexpected token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53014993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com