完全错误:
[./assets/1.jpg] 281 bytes {mini-css-extract-plugin} [built] [failed] [1 error]
ERROR in ./assets/1.jpg 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
@ ./scss/index.scss (../node_modules/css-loader/dist/cjs.js!../node_modules/sass-loader/dist/cjs.js!./scss/index.scss) 4:36-62
js:import './scss/index.scss'
html:<div class="app"></div>
css:
.app {
height: 500px;
width: 500px;
background: url("../assets/1.jpg");
}
网页包配置
const path = require('path')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const HTMLWebpackPlugin = require('html-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const isProd = process.env.NODE_ENV === 'production'
const isDev = !isProd
const filename = ext => isDev ? `bundle.${ext}` : `bundle.[hash].${ext}`
const jsLoaders = () => {
const loaders = [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-proposal-class-properties']
}
}
]
if (isDev) {
loaders.push(`eslint-loader`)
}
return loaders
}
module.exports = {
context: path.resolve(__dirname, 'src'),
mode: 'development',
entry: ['@babel/polyfill', './index.js'],
output: {
filename: filename('js'),
path: path.resolve(__dirname, 'dist')
},
resolve: {
extensions: ['.js'],
alias: {
'@': path.resolve(__dirname, 'src'),
'@core': path.resolve(__dirname, 'src/core')
}
},
devtool: isDev ? 'source-map' : false,
devServer: {
port: 3000,
hot: isDev
},
plugins: [
new CleanWebpackPlugin(),
new HTMLWebpackPlugin({
template: 'index.html',
minify: {
removeComments: isProd,
collapseWhitespace: isProd
}
}),
new CopyPlugin([
{
from: path.resolve(__dirname, 'src/favicon.ico'),
to: path.resolve(__dirname, 'dist')
}
]),
new MiniCssExtractPlugin({
filename: filename('css')
}),
],
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: isDev,
reloadAll: true
}
},
'css-loader',
'sass-loader'
],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: jsLoaders()
}
]
}
}
图像的路径以不同的方式指示并且一切都相同(有时会出现这样的错误:
ERROR in ./scss/index.scss (../node_modules/css-loader/dist/cjs.js!../node_modules/sass-loader/dist/cjs.js!./scss/index.scss)
Module not found: Error: Can't resolve './assets/1.jpg' in 'C:\Users\Сергей\Desktop\Frontend\Project's\My Portfolio on Pure JS\src\scss'
@ ./scss/index.scss (../node_modules/css-loader/dist/cjs.js!../node_modules/sass-loader/dist/cjs.js!./scss/index.scss) 4:36-61
)
url("../assets/1.jpg") url("./assets/1.jpg") url("assets/1.jpg") url("src/assets/1.jpg")
我知道问题出在 webpack 程序集中,但我不知道解决方案