From bf83b48ef04066228967763559f6ff943503392b Mon Sep 17 00:00:00 2001 From: inottn Date: Mon, 18 Nov 2024 19:38:25 +0800 Subject: [PATCH] fix: set the correct asset_info when extracting css (#8454) --- .../rspack_plugin_extract_css/src/plugin.rs | 14 +++--- .../css/rspack-issue-8431/index.js | 1 + .../css/rspack-issue-8431/rspack.config.js | 43 +++++++++++++++++++ .../css/rspack-issue-8431/style.css | 5 +++ ...1$.css => main.$31d6cfe0d16ae931b73c$.css} | 0 5 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 packages/rspack-test-tools/tests/configCases/css/rspack-issue-8431/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/css/rspack-issue-8431/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/css/rspack-issue-8431/style.css rename tests/plugin-test/css-extract/cases/contenthash-1/expected/{main.$8f7fd37a1cf4defaff11$.css => main.$31d6cfe0d16ae931b73c$.css} (100%) diff --git a/crates/rspack_plugin_extract_css/src/plugin.rs b/crates/rspack_plugin_extract_css/src/plugin.rs index 9ecc6a9f2cb..ba2f3d86096 100644 --- a/crates/rspack_plugin_extract_css/src/plugin.rs +++ b/crates/rspack_plugin_extract_css/src/plugin.rs @@ -7,11 +7,11 @@ use rspack_collections::{DatabaseItem, IdentifierMap, IdentifierSet, UkeySet}; use rspack_core::ChunkGraph; use rspack_core::{ rspack_sources::{ConcatSource, RawSource, SourceMap, SourceMapSource, WithoutOriginalOptions}, - ApplyContext, AssetInfo, Chunk, ChunkGroupUkey, ChunkKind, ChunkUkey, Compilation, - CompilationContentHash, CompilationParams, CompilationRenderManifest, - CompilationRuntimeRequirementInTree, CompilerCompilation, CompilerOptions, Filename, Module, - ModuleGraph, ModuleIdentifier, ModuleType, NormalModuleFactoryParser, ParserAndGenerator, - ParserOptions, PathData, Plugin, PluginContext, RenderManifestEntry, RuntimeGlobals, SourceType, + ApplyContext, Chunk, ChunkGroupUkey, ChunkKind, ChunkUkey, Compilation, CompilationContentHash, + CompilationParams, CompilationRenderManifest, CompilationRuntimeRequirementInTree, + CompilerCompilation, CompilerOptions, Filename, Module, ModuleGraph, ModuleIdentifier, + ModuleType, NormalModuleFactoryParser, ParserAndGenerator, ParserOptions, PathData, Plugin, + PluginContext, RenderManifestEntry, RuntimeGlobals, SourceType, }; use rspack_error::{Diagnostic, Result}; use rspack_hash::RspackHash; @@ -315,7 +315,7 @@ impl PluginCssExtract { let mut source = ConcatSource::default(); let mut external_source = ConcatSource::default(); - let (filename, _) = compilation.get_path_with_info(filename_template, path_data)?; + let (filename, asset_info) = compilation.get_path_with_info(filename_template, path_data)?; for module in used_modules { let content = Cow::Borrowed(module.content.as_str()); @@ -412,7 +412,7 @@ impl PluginCssExtract { RenderManifestEntry::new( Arc::new(external_source), filename, - AssetInfo::default(), + asset_info, false, false, ), diff --git a/packages/rspack-test-tools/tests/configCases/css/rspack-issue-8431/index.js b/packages/rspack-test-tools/tests/configCases/css/rspack-issue-8431/index.js new file mode 100644 index 00000000000..4fe51c72d64 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/css/rspack-issue-8431/index.js @@ -0,0 +1 @@ +import "./style.css"; diff --git a/packages/rspack-test-tools/tests/configCases/css/rspack-issue-8431/rspack.config.js b/packages/rspack-test-tools/tests/configCases/css/rspack-issue-8431/rspack.config.js new file mode 100644 index 00000000000..35aeab87794 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/css/rspack-issue-8431/rspack.config.js @@ -0,0 +1,43 @@ +const { rspack } = require("@rspack/core"); + +class Plugin { + apply(compiler) { + compiler.hooks.thisCompilation.tap("MyPlugin", compilation => { + compilation.hooks.processAssets.tap( + { + name: "MyPlugin", + stage: compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH + }, + () => { + const css = compilation + .getAssets() + .find(asset => asset.name.endsWith(".css")); + expect(css.info.contenthash.length).toBeGreaterThan(0); + } + ); + }); + } +} + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + entry: "./index.js", + target: "web", + module: { + rules: [ + { + test: /\.css$/, + use: [rspack.CssExtractRspackPlugin.loader, "css-loader"] + } + ] + }, + plugins: [ + new rspack.CssExtractRspackPlugin({ + filename: "[name].[contenthash].css" + }), + new Plugin() + ], + experiments: { + css: false + } +}; diff --git a/packages/rspack-test-tools/tests/configCases/css/rspack-issue-8431/style.css b/packages/rspack-test-tools/tests/configCases/css/rspack-issue-8431/style.css new file mode 100644 index 00000000000..0ddfcd71c10 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/css/rspack-issue-8431/style.css @@ -0,0 +1,5 @@ +.example { + margin-left: 22px; + margin-top: 22px; + margin-bottom: 22px; +} diff --git a/tests/plugin-test/css-extract/cases/contenthash-1/expected/main.$8f7fd37a1cf4defaff11$.css b/tests/plugin-test/css-extract/cases/contenthash-1/expected/main.$31d6cfe0d16ae931b73c$.css similarity index 100% rename from tests/plugin-test/css-extract/cases/contenthash-1/expected/main.$8f7fd37a1cf4defaff11$.css rename to tests/plugin-test/css-extract/cases/contenthash-1/expected/main.$31d6cfe0d16ae931b73c$.css