当我在SCSS文件中使用@import
指令时,导入文件的相对路径被计算为用JS导入的SCSS文件的相对路径。
fs结构:
/script.js
/subfolder/
/image.png
/substyle.scss
/webpack.config.js
webpack.config.js
module.exports = {
mode: 'production',
entry: './script.js',
module: {
rules: [
{
test: /\.scss$/i,
use: [
'style-loader',
'css-loader',
'sass-loader'
]
},
{
test: /\.css$/i,
use: 'css-loader'
}
]
}
}
script.js
import './style.scss'
style.scss
@import "subfolder/substyle";
subfolder/substyle.scss
body{
background: url(image.png);
}
测试结果:
Module not found: Error: Can't resolve 'image.png' in 'E:\webpacktest'
resolve 'image.png' in 'E:\webpacktest'
Parsed request is a module
using description file: E:\webpacktest\package.json (relative path: .)
using description file: E:\webpacktest\package.json (relative path: ./image.png)
no extension
E:\webpacktest\image.png doesn't exist
as directory
E:\webpacktest\image.png doesn't exist
resolve as module
looking for modules in E:\webpacktest\node_modules
single file module
using description file: E:\webpacktest\package.json (relative path: ./node_modules/image.png)
no extension
E:\webpacktest\node_modules\image.png doesn't exist
E:\webpacktest\node_modules\image.png doesn't exist
E:\node_modules doesn't exist or is not a directory
1条答案
按热度按时间9jyewag01#
我找到解决办法了
在sass-loader之前的列表中应该添加额外的loader。
{ loader: 'resolve-url-loader', options: { sourceMap: true } }