最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - `type: 'assetsource'` in Webpack v5.96.0 Causing CSS Files Not to Be Packaged - Stack Overflow

programmeradmin4浏览0评论

Issue with type: 'asset/source' in Webpack v5.96.0 Causing CSS Files Not to Be Packaged

My project is an Angular project with a custom-written Webpack configuration.


Problem Description

When using Webpack version 5.95.0, I used the following configuration to handle CSS files, and the CSS files were correctly packaged during the build process, with styles displaying properly on the page. However, after upgrading Webpack to v5.96.0, the CSS files are no longer packaged during the build process, causing the page styles to fail.


mjs Configuration Code:

{
    test: /\.css$/,
    type: 'asset/source',
    include: [
        helpers.root('folder1', 'folder2', 'webcomponent'),
        new RegExp('folder\\' + path.sep + '.*\\' + path.sep + 'src\\' + path.sep + '.*\\' + path.sep)
    ]
}

header Component:

@Component({
    selector: "header",
    templateUrl: "./headerponent.html",
    styleUrls: ["./header.css"]
})

header.css:

.sample {
    background-color: #ccc;
}

Debugging Findings

After debugging, I found that the [webpack\lib\asset\AssetSourceGenerator.js] file in Webpack v5.96.0 has been modified. In v5.95.0, it directly returns [return TYPES]. The new logic in v5.96.0 causes originModule to be null, which prevents the CSS files from being correctly processed.

If I comment out type: 'asset/source' and replace it with use: ['raw-loader'], the CSS files are correctly packaged and the styles display properly:


Alternative Configuration:

{
    test: /\.css$/,
    include: [
        helpers.root('folder1', 'folder2', 'webcomponent'),
        new RegExp('folder\\' + path.sep + '.*\\' + path.sep + 'src\\' + path.sep + '.*\\' + path.sep)
    ],
    use: ['raw-loader']
}

Environment Information

  • Webpack Version: v5.96.0
  • Node.js Version: 20.18.2
  • Operating System: Windows 11 22H2

Question

What additional configuration is needed when using type: 'asset/source' to ensure that CSS files are correctly packaged?

发布评论

评论列表(0)

  1. 暂无评论