webpack 1.12.13无法解析' file'或者'目录' ./img/readme2.png

时间:2017-02-07 13:24:01

标签: webpack webpack-style-loader

我使用webpack 1.12.13并且webpack产生错误:

错误在./~/css-loader!./src/styles/oil.css中 找不到模块:错误:无法解析' file'或者'目录' C:\ react_oil \ src \ styles中的./img/close.png @ ./~/css-loader!./src/styles/oil.css 6:15780-15806

错误在./~/css-loader!./src/styles/oil.css中 找不到模块:错误:无法解析' file'或者'目录' C:\ react_oil \ src \ styles中的./img/use.png @ ./~/css-loader!./src/styles/oil.css 6:17678-17702

错误可能是什么?

webpack config:

var path = require('path')
var webpack = require('webpack')
var NpmInstallPlugin = require('npm-install-webpack-plugin')
var autoprefixer = require('autoprefixer');
var precss = require('precss');
var ExtractTextPlugin = require ('extract-text-webpack-plugin');

module.exports = {
  devtool: 'cheap-module-eval-source-map',
  entry: [
    'webpack-hot-middleware/client',
    'babel-polyfill',
    './src/index'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static/'
  },
  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new NpmInstallPlugin(),
    new ExtractTextPlugin('bundle.css')
  ],
  module: {
    preLoaders: [
      {
        test: /\.js$/,
        loaders: ['eslint'],
        include: [
          path.resolve(__dirname, "src"),
        ],
      }
    ],
    loaders: [
      {
        loaders: ['react-hot', 'babel-loader'],
        include: [
          path.resolve(__dirname, "src"),
        ],
        test: /\.js$/,
        plugins: ['transform-runtime'],
      },
      {
        test:   /\.css$/,
        //loader: "style-loader!css-loader!postcss-loader"
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader', 'postcss-loader')
      },
      { 
            test: /\.(png|jpg|svg|ttf|eot|woff|woff2)$/, 
            include: /\/node_modules\//,
            loader: "file?name=[1].[ext]&regExp=node_modules/(.*)"
      },
      { 
            test: /\.(png|jpg|svg|ttf|eot|woff|woff2)$/, 
            exclude: /\/node_modules\//,
            loader: "file?name=[path][name].[ext]"
      }
    ]
  },
  postcss: function () {
    return [autoprefixer, precss];
  }
}

文件夹结构:

node_modules
src
 |--styles
      |--oil.css
 |--img
      |--image files
webpack.config.js

2 个答案:

答案 0 :(得分:0)

test:/\.(png|svg|jpeg|ttf)$/,
            use:[{
            loader:require.resolve("file-loader") + "?name=../[path][name].[ext]"

答案 1 :(得分:0)

对于webpack 4,现在必须在加载程序规则中添加整个“文件加载器”,而不仅仅是“文件”。但是我看到您的是webpack1.x。最好也将您的js代码放在这里。 除此之外,尝试将img中的img导入,然后在其中设置img元素。面临类似的问题并为我工作,仅在webpack加载器中使用“文件加载器”。

类似这样的事情: 从“ ./xxx.png”导入图标;

内部脚本: document.getElementById('imgtag-id')。src =图标;