Webpack - postcss-import无法找到带有VueJs的CSS文件

时间:2018-06-16 09:43:01

标签: css webpack vuejs2 postcss css-loader

我无法让它在我的VueJs项目中加载css文件 我最近添加了less-loader和sass-loader,这些工作正常。但是当我在sass文件或main.js中添加css文件并导入时,它无法找到该文件。

这就是我正在做的事情:

SASS:

@import "../../../../node_modules/pnotify/dist/PNotifyBrightTheme.css";

或js:

import 'module/pnotify/dist/PNotifyBrightTheme.css'

webpack.base.config:

resolve: {
  extensions: ['.js', '.vue', '.json', '.css'],
  alias: {
    'vue$': 'vue/dist/vue.esm.js',
    '@': resolve('src'),
    'module': resolve('node_modules')
  }
},
module: {
  rules: [{
    test: /\.css$/,
    use: [
      'style-loader',
      {
        loader: 'css-loader',
        options: { importLoaders: 1 }
      },
      'postcss-loader'
    ]
  }]
}

postcssrc.js文件:

module.exports = {
  "plugins": {
    "postcss-import": {},
    "postcss-url": {},
    "autoprefixer": {}
  }
}

已安装的模块:

"css-loader": "^0.28.11",
"postcss-load-config": "^1.2.0",
"postcss-import": "^11.0.0",
"postcss-loader": "^2.1.4",
"postcss-url": "^7.2.1",
"style-loader": "^0.21.0",

带有css文件的库

"pnotify": "^4.0.0-alpha.4",

例外:

in ./src/assets/scss/index.scss

Module build failed: Error: Failed to find '../../../../node_modules/pnotify/dist/PNotifyBrightTheme.css'
  in [
    /src/assets/scss
  ]
    at resolveModule.catch.catch (/node_modules/postcss-import/lib/resolve-id.js:35:13)
    at <anonymous>

 @ ./src/assets/scss/index.scss 4:14-212 13:3-17:5 14:22-220
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js

有人知道怎么做吗? 我绝望了:(

1 个答案:

答案 0 :(得分:0)

经过多次研究后,我通过这样做解决了这个问题。

杀死我的CSS加载/导入的一些示例代码: 在build / utils.js

const cssLoader = {
    loader: 'css-loader',
    options: {
      sourceMap: options.sourceMap
    }
  }

  //remove this
  const postcssLoader = {
    loader: 'postcss-loader',
    options: {
      sourceMap: options.sourceMap
    }
  }

  // generate loader string to be used with extract text plugin
  function generateLoaders (loader, loaderOptions) {
    //change to const loaders = [cssLoader]
    const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader]

    if (loader) {
      loaders.push({
        loader: loader + '-loader',
        options: Object.assign({}, loaderOptions, {
          sourceMap: options.sourceMap
        })
      })
    }
  }
}

同时删除所有这些代码: 在webpack配置

 {
   test: /\.css$/,
   use: [
     'style-loader',
     {
       loader: 'css-loader',
       options: { importLoaders: 1 }
     },
     'postcss-loader'
   ]
 },