如何将CSS文件导入webpack?

WoJ*_*WoJ 17 html javascript css webpack webpack-2

根据文档,应该只import编辑CSS文件.

我刚刚开始webpack并尝试导入一个CSS文件,但我得到一个关于模块丢失的消息:

D:\Dropbox\dev\jekyll\blog>webpack --display-error-details
Hash: 0cabc1049cbcbdb8d134
Version: webpack 2.6.1
Time: 74ms
   Asset     Size  Chunks             Chunk Names
build.js  2.84 kB       0  [emitted]  main
   [0] ./webpack/entry.js 47 bytes {0} [built]

ERROR in ./webpack/entry.js
Module not found: Error: Can't resolve 'navbar.css' in 'D:\Dropbox\dev\jekyll\blog\webpack'
resolve 'navbar.css' in 'D:\Dropbox\dev\jekyll\blog\webpack'
  Parsed request is a module
  using description file: D:\Dropbox\dev\jekyll\blog\package.json (relative path: ./webpack)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: D:\Dropbox\dev\jekyll\blog\package.json (relative path: ./webpack)
    resolve as module
      D:\Dropbox\dev\jekyll\blog\webpack\node_modules doesn't exist or is not a directory
      D:\Dropbox\dev\jekyll\node_modules doesn't exist or is not a directory
      D:\Dropbox\dev\node_modules doesn't exist or is not a directory
      D:\Dropbox\node_modules doesn't exist or is not a directory
      D:\node_modules doesn't exist or is not a directory
      looking for modules in D:\Dropbox\dev\jekyll\blog\node_modules
        using description file: D:\Dropbox\dev\jekyll\blog\package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
        after using description file: D:\Dropbox\dev\jekyll\blog\package.json (relative path: ./node_modules)
          using description file: D:\Dropbox\dev\jekyll\blog\package.json (relative path: ./node_modules/navbar.css)
            as directory
              D:\Dropbox\dev\jekyll\blog\node_modules\navbar.css doesn't exist
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              D:\Dropbox\dev\jekyll\blog\node_modules\navbar.css doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              D:\Dropbox\dev\jekyll\blog\node_modules\navbar.css.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              D:\Dropbox\dev\jekyll\blog\node_modules\navbar.css.json doesn't exist
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules]
[D:\Dropbox\dev\jekyll\node_modules]
[D:\Dropbox\dev\node_modules]
[D:\Dropbox\node_modules]
[D:\node_modules]
[D:\Dropbox\dev\jekyll\blog\node_modules\navbar.css]
[D:\Dropbox\dev\jekyll\blog\node_modules\navbar.css]
[D:\Dropbox\dev\jekyll\blog\node_modules\navbar.css.js]
[D:\Dropbox\dev\jekyll\blog\node_modules\navbar.css.json]
 @ ./webpack/entry.js 1:0-21
Run Code Online (Sandbox Code Playgroud)

webpack遇到以下情况webpack.config.js:

const path = require('path');

module.exports = {
  entry: path.join(__dirname, 'webpack/entry.js'),
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'build.js'
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      }
    ],
    rules: [{
            test: /\.css$/,
            use: [ 'style-loader', 'css-loader' ]
        }]
  }
}
Run Code Online (Sandbox Code Playgroud)

我最初认为(在使用之前--display-error-details)这是由于路径结构的某些问题(特别是前向与后向斜杠)但后来移动navbar.css到文件夹的根目录webpack- 同样的问题.

详细的错误表明CSS文件是在追求的nodes_module(通过所有目录树追捕).为什么?那么我应该如何导入webpack/static/css/myfile.css相对于该位置的文件webpack.config.js

注意:尝试require代替时存在同样的问题import

Mic*_*ngo 30

您必须像任何JavaScript模块一样导入它们.这意味着,当导入的文件既不是相对路径(以../或开头./),也不是绝对路径(以...开头/)时,它将被解析为模块.默认情况下,webpack将在node_modules目录中查找模块(在当前目录和所有父目录中).这与Node.js使用的行为相同.

要导入webpack/static/css/myfile.css,webpack/entry.js必须使用相对路径.

import './static/css/myfile.css';
Run Code Online (Sandbox Code Playgroud)

如果您不想使用相对路径,则可以更改webpack用于查找具有resolve.modules或可以使用的模块的目录resolve.alias.


在您的webpack配置中,您还定义了module.rulesmodule.loaders.当webpack看到module.rulesmodule.loaders完全忽略时,你babel-loader就不会被使用了.你应该只使用module.rules.

module: {
  rules: [
    {
      test: /\.js$/,
      loader: 'babel-loader',
      exclude: /node_modules/
    },
    {
      test: /\.css$/,
      use: ['style-loader', 'css-loader']
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢迈克尔的两个信息.我认为`webpack`非常好,但文档并不是特别的 - 特别是对于那些不熟悉JS的人. (5认同)