Skip to content

Commit

Permalink
fix: set the correct asset_info when extracting css (#8454)
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn authored Nov 18, 2024
1 parent b5fd823 commit bf83b48
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
14 changes: 7 additions & 7 deletions crates/rspack_plugin_extract_css/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -412,7 +412,7 @@ impl PluginCssExtract {
RenderManifestEntry::new(
Arc::new(external_source),
filename,
AssetInfo::default(),
asset_info,
false,
false,
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./style.css";
Original file line number Diff line number Diff line change
@@ -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
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.example {
margin-left: 22px;
margin-top: 22px;
margin-bottom: 22px;
}

2 comments on commit bf83b48

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ❌ failure
examples ✅ success
devserver ❌ failure

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-11-18 9112e35) Current Change
10000_development-mode + exec 1.8 s ± 34 ms 1.83 s ± 45 ms +1.68 %
10000_development-mode_hmr + exec 643 ms ± 13 ms 643 ms ± 4.2 ms -0.02 %
10000_production-mode + exec 2.43 s ± 21 ms 2.42 s ± 44 ms -0.35 %
arco-pro_development-mode + exec 1.79 s ± 74 ms 1.78 s ± 77 ms -0.93 %
arco-pro_development-mode_hmr + exec 430 ms ± 1.5 ms 430 ms ± 0.78 ms -0.02 %
arco-pro_production-mode + exec 3.19 s ± 40 ms 3.14 s ± 73 ms -1.46 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.21 s ± 87 ms 3.24 s ± 81 ms +0.73 %
threejs_development-mode_10x + exec 1.58 s ± 23 ms 1.58 s ± 20 ms -0.30 %
threejs_development-mode_10x_hmr + exec 777 ms ± 5.5 ms 778 ms ± 7.2 ms +0.12 %
threejs_production-mode_10x + exec 4.97 s ± 26 ms 4.97 s ± 26 ms -0.04 %
10000_big_production-mode_disable-minimize + exec - 40.1 s ± 804 ms -

Please sign in to comment.