我当时正在写webpack 4.44.2,当我转换到webpack 5.0.0时,我面临这个错误。
./src//sass/模块构建失败(来自E:\maktab\Control-panel\newcontrol\final-control\node_modules\css-loader\dist\cjs.js!错误:此浏览器在E:\maktab\Control-panel\newcontrol\final-control\node_modules\css-loader\dist\cjs.js!不支持自动publicPath )
错误来自于fonts.scss中的字体文件池
@font-face {
font-family: "Janna LT";
src: local("Janna LT"), url(../fonts/janna.woff) format("woff");
font-weight: normal;
}
@font-face {
font-family: "Janna LT";
src: local("Janna LT"), url(../fonts/janna-bold.woff) format("woff");
font-weight: bold;
}
我的src结构https://i.stack.imgur.com/vKyfW.png
dist结构https://i.stack.imgur.com/mLgmF.png
webpack.config.js
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
entry: {
'main': './src/index.js',
},
output: {
path: path.join(__dirname, "/dist"),
filename: '[name].js',
},
devServer: {
contentBase: path.join(__dirname, "/dist"),
port: 8087,
writeToDisk: true,
overlay :true
},
module: {
rules: [
{
test: /\.html$/,
use: [
{
loader: "html-loader",
}
]
},
{
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader'
]
},
{
test: /\.(png|svg|jpe?g|gif)$/,
exclude: /fonts/,
use: [
{
loader: "file-loader",
options: {
name: '[name].[ext]',
outputPath: "/assets/images",
}
}
]
},
{
test: /\.(svg|eot|woff|woff2|ttf)$/,
exclude: /images/,
use: [
{
loader: "file-loader",
options: {
name: '[name].[ext]',
outputPath: "assets/fonts",
}
}
]
},
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
filename: "index.html",
template: "./src/index.html",
chunks: ['main']
}),
new MiniCssExtractPlugin({filename: "assets/css/styles.css"}),
new OptimizeCSSAssetsPlugin({}),
]
}
styles.scss
@import "base/fonts";
@import "base/global";
@import "base/typography";
@import "base/links";
@import "components/components";
@import "components/demo";
index.js
import './assets/sass/styles.scss';
import 'normalize.css/normalize.css';
console.log("hellow from webpack5");
发布于 2020-11-06 12:54:43
建议的解决办法对我没有用。但是,我发现将publicPath设置为空字符串是很有用的。
output: {
publicPath: '',
...
}发布于 2020-10-25 16:04:13
我也遇到过同样的问题。我的代码编译到dist文件夹中,没有任何进一步的结构。下面的代码适用于我,并且很简单,因为我需要一个空路径。
'module': {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: ''
}
},
{
loader: "css-loader"
}
]
}
]
}你也可以疯狂的去做这样的事情:
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: (resourcePath, context) => {
return path.relative(path.dirname(resourcePath), context) + '/';
},
},
},您可以在这里找到详细信息:https://webpack.js.org/plugins/mini-css-extract-plugin/#the-publicpath-option-as-function
发布于 2021-02-26 10:29:58
这个错误是由Min-CSS-Extract-plugin1.3.8和更低版本的bug和Webpack 5一起造成的。当样式表引用使用url(...)的资源时触发它,而publicPath选项没有显式地在Webpack配置中设置。
我花时间在这里复制并报告了这个问题:https://github.com/webpack-contrib/mini-css-extract-plugin/issues/707
昨天,版本1.3.9已经发布,它包含了解决此问题的方法。您应该能够通过升级来解决此错误。
https://stackoverflow.com/questions/64294706
复制相似问题