根据 文档,CSS 文件应该只是 import
ed。
我刚开始使用 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
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' ]
}]
}
}
我最初认为(在使用之前 --display-error-details
)这是由于路径结构的一些问题(特别是正斜杠与反斜杠)但后来移动 navbar.css
到文件夹的根目录 webpack
- 同样的问题。
详细错误显示在 nodes_module
中寻找 CSS 文件(通过所有目录树寻找)。为什么? 然后我应该如何导入 webpack/static/css/myfile.css
相对于 webpack.config.js
位置的文件?
注意:尝试 require
而不是 import
时存在同样的问题
原文由 WoJ 发布,翻译遵循 CC BY-SA 4.0 许可协议
您必须像导入任何 JavaScript 模块一样导入它们。这意味着,当导入的文件既不是相对路径(以
../
或./
),也不是绝对路径(以/
)时,它是-作为模块解决。默认情况下,webpack 将在node_modules
目录(在当前目录和所有父目录中)中查找模块。这与 Node.js 使用 的行为相同。要在 ---
webpack/static/css/myfile.css
webpack/entry.js
您必须使用相对路径。如果您不想使用相对路径,您可以更改 webpack 用于查找带有
resolve.modules
的模块的目录,或者您可以使用resolve.alias
。在您的 webpack 配置中,您还定义了
module.rules
和module.loaders
。当 webpack 看到module.rules
它完全忽略module.loaders
,所以你的babel-loader
不会被使用。您应该只使用module.rules
。