【问题标题】:Webpack css-loader failing: "You may need an appropriate loader to handle this file type."Webpack css-loader 失败:“您可能需要适当的加载器来处理此文件类型。”
【发布时间】:2018-03-03 01:47:10
【问题描述】:

我正在构建一个使用 TypeScript 和 Webpack 4 的 React 应用程序。我正在尝试从 react-select 导入一个 CSS 文件,但我收到了一般错误:

ERROR in ./node_modules/react-select/dist/react-select.css
Module parse failed: Unexpected token (8:0)
You may need an appropriate loader to handle this file type.
|  * MIT License: https://github.com/JedWatson/react-select
| */
| .Select {
|   position: relative;
| }
 @ ./src/App.react.tsx 28:0-45
 @ ./src/Root.react.tsx
 @ ./src/index.tsx
 @ multi (webpack)-dev-server/client?http://localhost:3000 webpack/hot/dev-server ./src/index.tsx

我在尝试为.graphql 文件添加加载程序之前遇到了类似的问题......我想这些问题是相关的;我的配置必须关闭,无法利用这些额外的加载器。

我直接从https://github.com/webpack-contrib/css-loader 获取了准系统css-loader 设置。

我拥有的file-loader 工作正常。

我认为相关的sn-ps代码如下:

来自webpack.common.ts

const config: webpack.Configuration = {
  module: {
    rules: [
      ...
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
    ],
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js', '.css'],
  },
  ...
};

来自App.react.tsx

import 'react-select/dist/react-select.css';
...

但猜测是该上下文之外的某些东西导致了问题。更多上下文...

webpack.common.ts

import * as HTMLPlugin from 'html-webpack-plugin';
import * as CleanWebpackPlugin from 'clean-webpack-plugin';
import * as webpack from 'webpack';

const config: webpack.Configuration = {
  // Root TS file for bundling
  entry: './src/index.tsx',
  module: {
    rules: [
      // Bundle files (e.g. images)
      {
        test: /\.(png|jpg|gif)$/,
        use: ['file-loader'],
      },
      // Transpile & type check with babel/typescript loader
      {
        test: /\.tsx?$/,
        use: [
          {
            // Need babel for React HMR support (otherwise could drop babel and use just typescript)
            loader: 'babel-loader',
            options: {
              babelrc: true,
              plugins: ['react-hot-loader/babel'],
            },
          },
          'ts-loader',
        ],
      },
      // Handle .css files
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
    ],
  },
  // Enable served source maps
  devtool: 'inline-source-map',
  resolve: {
    // Include all these extensions in processing (note we need .js because not all node_modules are .ts)
    extensions: ['.ts', '.tsx', '.js', '.css'],
  },
  // Webpack Dev Server for running locally
  devServer: {
    // Play nicely with react-router
    historyApiFallback: true,
    port: 3000,
    // Enable hot module reloading (HMR)
    hot: true,
  },
  plugins: [
    // Cleans the build folder per-build/reload
    new CleanWebpackPlugin(['dist']),
    // Builds the .html file for entering into bundle
    new HTMLPlugin({
      template: 'INDEX_TEMPLATE.html',
    }),
    // HMR plugins
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    // Prevents webpack watch from going into infinite loop (& stopping on retry) due to TS compilation
    new webpack.WatchIgnorePlugin([
      /\.js$/,
      /\.d\.ts$/,
    ]),
  ],
};

export default config;

tsconfig.json

{
  "compilerOptions": {
    // Required option for react-hot-loader
    "target": "es6",
    // Required option for react-hot-loader
    "module": "commonjs",
    "strict": true,
    "jsx": "react",
    // Absolute imports start from here
    "baseUrl": ".",
    "sourceMap": true
  }
}

如果还有其他值得注意的问题可能是问题的一部分,请告诉我...谢谢!

【问题讨论】:

    标签: reactjs typescript webpack css-loader


    【解决方案1】:

    在这里。似乎是一个愚蠢的问题:我在 WebStorm TypeScript 编译器上单击“全部编译”,css-loader 开始正常工作。所以我猜 webpack 在我的问题中使用的是未更新的 webpack.common.js 文件而不是 webpack.common.ts 文件。

    试图理解为什么它引用编译的.js 文件,即使我的命令是:

    webpack-dev-server --mode development --open --config config/webpack.development.ts

    可能与我使用 webpack-merge 并导入 webpack.common 的事实有关:import common from './webpack.common';。导入中的无扩展名,出于某种原因,它必须默认为 .js

    如果其他人遇到这个问题,这个问题的有用提示Configure WebStorm to delete *.ts *.js *.js.map all togetheroutput the generated files (.js and .js.map) to a different folder using the outDir option in tsconfig.json. The build folder can then easily be cleaned

    如果我发现更多信息会报告。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-08
      • 1970-01-01
      • 2019-10-11
      • 2019-09-27
      • 2016-06-02
      • 2019-07-26
      • 1970-01-01
      • 2016-10-31
      相关资源
      最近更新 更多