我尝试了多种方法来显示背景图像,但没有任何效果,现在我的 Sass 文件也无法编译。
不再显示错误,但样式不存在,只是 html。
webpack.config:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'development', //production
entry: {
main: path.resolve(__dirname, 'src/app.js'),
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[contenthash].js',
assetModuleFilename: '[name][ext]',
clean: true,
},
devtool: 'inline-source-map',
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
port: 8080, //default 8080
open: true,
hot: true,
watchContentBase: true,
},
//loaders
module: {
rules: [
//css
{
test: /\.scss$/, use: ['style-loader', 'css-loader',
'sass-loader']
},
//images
{ test: /\.(svg|ico|png|webp|jpg|gif|jpeg)$/, type: 'asset/resource' },
//js for babel
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
],
},
//plugins
plugins: [
new HtmlWebpackPlugin({
title: 'Just a Demo',
filename: 'index.html',
template: path.resolve(__dirname, 'src/index.html'),
}),
],
};
包.json
"devDependencies": {
"@babel/core": "^7.14.8",
"@babel/preset-env": "^7.14.8",
"babel-loader": "^8.2.2",
"css-loader": "^6.2.0",
"html-webpack-plugin": "^5.3.2",
"style-loader": "^3.2.1",
"webpack": "^5.45.1",
"webpack-cli": "^4.7.2",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"normalize.css": "^8.0.1",
"sass": "^1.41.1",
"sass-loader": "^12.1.0"
}
}
main.scss 文件(我正在使用@use 和@forward) - 它以前工作过,所以我认为我导入文件的方式是正确的。现在 main.css 文件中显示了同样的东西,这很奇怪
@use 'abstracts';
@use 'base';
@use 'layouts';
@use 'pages';
和我的项目结构
如果有人可以帮助我,那就太好了,因为它已经让我发疯了两个多星期了
谢谢!