Webpack 4:模块解析失败:意外的令牌

时间:2018-10-26 19:12:53

标签: reactjs typescript webpack ts-loader webpack-dev-middleware

在构建期间,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 = 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应该能够处理反应。

Herewebpack.config团队提供的示例ts-loader,该团队在使用打字稿并做出反应的应用中使用。设置与我自己的设置非常相似,但是我不使用webpack-dev-server,而是使用webpack-dev-middleware

1 个答案:

答案 0 :(得分:0)

niba对原始问题的评论已解决了该问题。将ts-loader分配给单个模块时,似乎include不会遍历链接的模块。删除include字段或使用文件夹名称可以解决此错误。