admin管理员组文章数量:1434921
src/index.html
src/layout/header.html
src/css/common/iconfont.css
head.html:
<link rel="stylesheet" href="@styles/common/iconfont.css">
index.html:
<%~ include('layout/header.html',{title:title}) %>
webpack config:
module.exports = (env, argv) =>{
const isProd = argv.mode === 'production';
return {
mode: argv.mode,
devtool: isProd?'hidden-source-map':'eval-cheap-module-source-map',
output: {
path: path.resolve(__dirname, 'dist'),
clean: true,
},
resolve: {
alias: {
'@scripts': path.join(__dirname, 'src/js/'),
'@styles': path.join(__dirname, 'src/css/'),,
'@fonts': path.join(__dirname, 'src/fonts/'),
},
},
plugins:[
new HtmlBundlerPlugin({
entry: {
index: {
import: path.join(__dirname, 'src/index.html'),
data: { title: 'index' },
},
contact:{
import: path.join(__dirname, 'src/page-contact.html'),
data: { title: 'contact' },
}
},
preprocessor: 'eta',
preprocessorOptions: {
views: path.join(__dirname, 'src'),
},
js: {
filename: 'js/[name].[contenthash:8].js',
inline: {
chunk: [/runtime.+[.]js/],
},
},
css: {
filename: 'css/[name].[contenthash:8].css',
},
minify: {
removeComments: true,
collapseWhitespace: false,
minifyJS: true,
minifyCSS: true,
},
}),
],
module:{
rules: [
{
test: /\.(ico|png|jp?g|svg)$/,
type: 'asset',
parser:{
dataUrlCondition:{ maxSize: 10*1024 }
},
generator:{
filename: 'images/[name].[hash:8][ext]',
},
},
{
test: /\.css$/i,
use: [
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [['postcss-preset-env']],
},
},
}
],
}
]
},
devServer: {
static: path.join(__dirname, 'dist'),
watchFiles: {
paths: [ path.join(__dirname, 'src/**/*.*') ],
options: {
usePolling: false,
},
},
},
optimization: {
splitChunks: {
minSize: 30,
cacheGroups: {
default: {
name: 'commons',
chunks: 'all',
minChunks: 2,
priority: -20,
},
vendors: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'all',
priority: -10
}
},
},
},
};
}
error:
PluginException:
HTML Bundler Plugin Can't resolve D:\webpack\src\css\common\iconfont.css in the file src\page-contact.html
The handling of @import at-rules in CSS is not supported. Disable the 'import' option in 'css-loader':
{
test: /\.css$/i,
use: [
{
loader: 'css-loader',
options: {
import: false, // disable @import at-rules handling
},
},
],
},
When I run dev, an error occurs. How can I modify and solve it? It seems that there is an error in the alias reference to the css when packaging.
Only index.html runs normally, but when page-contact.html (including header.html and footer.html) is added and run, an error is reported.
src/index.html
src/layout/header.html
src/css/common/iconfont.css
head.html:
<link rel="stylesheet" href="@styles/common/iconfont.css">
index.html:
<%~ include('layout/header.html',{title:title}) %>
webpack config:
module.exports = (env, argv) =>{
const isProd = argv.mode === 'production';
return {
mode: argv.mode,
devtool: isProd?'hidden-source-map':'eval-cheap-module-source-map',
output: {
path: path.resolve(__dirname, 'dist'),
clean: true,
},
resolve: {
alias: {
'@scripts': path.join(__dirname, 'src/js/'),
'@styles': path.join(__dirname, 'src/css/'),,
'@fonts': path.join(__dirname, 'src/fonts/'),
},
},
plugins:[
new HtmlBundlerPlugin({
entry: {
index: {
import: path.join(__dirname, 'src/index.html'),
data: { title: 'index' },
},
contact:{
import: path.join(__dirname, 'src/page-contact.html'),
data: { title: 'contact' },
}
},
preprocessor: 'eta',
preprocessorOptions: {
views: path.join(__dirname, 'src'),
},
js: {
filename: 'js/[name].[contenthash:8].js',
inline: {
chunk: [/runtime.+[.]js/],
},
},
css: {
filename: 'css/[name].[contenthash:8].css',
},
minify: {
removeComments: true,
collapseWhitespace: false,
minifyJS: true,
minifyCSS: true,
},
}),
],
module:{
rules: [
{
test: /\.(ico|png|jp?g|svg)$/,
type: 'asset',
parser:{
dataUrlCondition:{ maxSize: 10*1024 }
},
generator:{
filename: 'images/[name].[hash:8][ext]',
},
},
{
test: /\.css$/i,
use: [
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [['postcss-preset-env']],
},
},
}
],
}
]
},
devServer: {
static: path.join(__dirname, 'dist'),
watchFiles: {
paths: [ path.join(__dirname, 'src/**/*.*') ],
options: {
usePolling: false,
},
},
},
optimization: {
splitChunks: {
minSize: 30,
cacheGroups: {
default: {
name: 'commons',
chunks: 'all',
minChunks: 2,
priority: -20,
},
vendors: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'all',
priority: -10
}
},
},
},
};
}
error:
PluginException:
HTML Bundler Plugin Can't resolve D:\webpack\src\css\common\iconfont.css in the file src\page-contact.html
The handling of @import at-rules in CSS is not supported. Disable the 'import' option in 'css-loader':
{
test: /\.css$/i,
use: [
{
loader: 'css-loader',
options: {
import: false, // disable @import at-rules handling
},
},
],
},
When I run dev, an error occurs. How can I modify and solve it? It seems that there is an error in the alias reference to the css when packaging.
Only index.html runs normally, but when page-contact.html (including header.html and footer.html) is added and run, an error is reported.
Share Improve this question edited Nov 18, 2024 at 13:35 Ryan Yang asked Nov 18, 2024 at 10:11 Ryan YangRyan Yang 133 bronze badges 3 |1 Answer
Reset to default 0The problem is in the splitChunks
configuration.
Webpack compile all assets in JavaScript modules and split it, including CSS modules defined in HTML. Therefore CSS files are lost.
Solution
Add the test
option to every splitChunks.cacheGroups
to avoid splitting of CSS files:
optimization: {
splitChunks: {
minSize: 100,
cacheGroups: {
default: {
test: /.+\.(js|ts)$/, // <= split only scripts, excluding style files
name: 'commons',
chunks: 'all',
minChunks: 2,
priority: -20,
},
vendors: {
test: /[\\/]node_modules[\\/].+\.(js|ts)$/, // <= split only scripts, excluding style files
name: 'vendor',
chunks: 'all',
priority: -10,
},
},
},
},
本文标签: javascriptHtmlBundlerPlugin errorimport atrules in CSS is not bugStack Overflow
版权声明:本文标题:javascript - HtmlBundlerPlugin error, import at-rules in CSS is not bug? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745627243a2667048.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
module: { rules: [{ test: /\.css$/, use: ['css-loader'] }] }
– biodiscus Commented Nov 18, 2024 at 10:48