Webpack css-loader失败:“您可能需要一个合适的加载器来处理这种文件类型。”

时间:2018-03-03 01:47:10

标签: reactjs typescript webpack css-loader

我正在构建一个使用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工作得很好。

以下是我认为相关的代码片段:

来自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
  }
}

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

0 个答案:

没有答案