简体   繁体   English

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

[英]Webpack css-loader failing: “You may need an appropriate loader to handle this file type.”

I'm building a React app that uses TypeScript and Webpack 4. I'm trying to import a CSS file from react-select , and I'm getting the generic error: 我正在构建一个使用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

I had a similar issue with trying to add a loader for .graphql files before punting it... I imagine the issues are related; 在尝试为.graphql文件添加加载器之前,我遇到了类似的问题......我想这些问题是相关的; something with my config must be off that's failing to leverage these extra loaders. 我的配置必须关闭,否则无法利用这些额外的加载器。

I took the barebones css-loader setup straight from https://github.com/webpack-contrib/css-loader . 我直接从https://github.com/webpack-contrib/css-loader获取了准系统css-loader设置。

The file-loader I have is working perfectly fine. 我拥有的file-loader工作得非常好。

Here's what I think the relevant snippets of code are: 以下是我认为相关的代码片段:

From webpack.common.ts : 来自webpack.common.ts

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

From App.react.tsx : 来自App.react.tsx

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

But guessing there's something outside of that context that's causing the problem. 但是猜测那些背景之外的东西会导致问题。 More context... 更多背景......

webpack.common.ts 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 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
  }
}

Please let me know if there's anything else worth noting that could be part of the problem... Thanks! 请告诉我是否还有其他值得注意的问题可能是问题的一部分...谢谢!

OP here. OP在这里。 Appears to have been a silly issue: I clicked "Compile All" on the WebStorm TypeScript compiler and the css-loader started working fine. 似乎是一个愚蠢的问题:我点击了WebStorm TypeScript编译器上的“Compile All”,css-loader开始正常工作。 So I guess webpack was using an un-updated webpack.common.js file instead of the webpack.common.ts file in my question. 所以我猜webpack在我的问题中使用了未更新的webpack.common.js文件而不是webpack.common.ts文件。

Trying to understand why it was referencing the compiled .js file even though my command was: 试图理解为什么它引用了编译的.js文件,即使我的命令是:

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

Likely something to do with the fact that I was using webpack-merge and so importing webpack.common: import common from './webpack.common'; 可能与我使用webpack-merge并因此导入webpack.common的事实有关: import common from './webpack.common'; . Sans extension in the import, for some reason it must default to .js . 在导入中没有扩展,由于某种原因它必须默认为.js

If anyone else runs into this issue, useful tip from this question Configure WebStorm to delete *.ts *.js *.js.map all together : output 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 如果有其他人遇到这个问题,那么这个问题的有用提示配置WebStorm将* .ts * .js * .js.map一起删除output 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 output 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

Will report back if I find out anything more. 如果我发现更多信息,将报告回来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您可能需要适当的加载程序来处理此文件类型。 与webpack - You may need an appropriate loader to handle this file type. with webpack 您可能需要一个合适的加载器来处理这种文件类型。 ReactJs - You may need an appropriate loader to handle this file type. ReactJs Webpack和babel配置问题:“您可能需要适当的加载程序来处理此文件类型。” - Webpack and babel configuration issue: “You may need an appropriate loader to handle this file type.” React / Webpack-“模块解析失败:意外的令牌-您可能需要适当的加载器来处理此文件类型。” - React / Webpack - “Module parse failed: Unexpected token - You may need an appropriate loader to handle this file type.” 模块解析失败:意外字符'@'您可能需要一个合适的加载器来处理此文件类型。(Webpack:4.4.0) - Module parse failed: Unexpected character '@'You may need an appropriate loader to handle this file type.(Webpack : 4.4.0) 您可能需要适当的加载程序来处理此文件类型。 Webpack和Babel - You may need an appropriate loader to handle this file type. Webpack and Babel Webpack 2:您可能需要适当的加载器来处理此文件类型React - Webpack 2 : You may need an appropriate loader to handle this file type React webpack错误:您可能需要适当的加载程序来处理此文件类型 - webpack ERROR :You may need an appropriate loader to handle this file type webpack错误-您可能需要适当的加载程序来处理此文件类型 - error with webpack - You may need an appropriate loader to handle this file type Webpack 错误:您可能需要合适的加载程序来处理此文件类型 - Webpack error: You may need an appropriate loader to handle this file type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM