2

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

./client/components/App/index.tsx 中的错误 15:9 模块解析失败:意外令牌 (15:9) 您可能需要适当的加载程序来处理此文件类型。

|
|
> 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 =真

这是我的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应该能够处理反应。

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

4

1 回答 1

0

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

于 2018-10-27T20:10:42.357 回答