Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ impl ContextReplacementPlugin {

#[plugin_hook(ContextModuleFactoryBeforeResolve for ContextReplacementPlugin)]
async fn cmf_before_resolve(&self, mut result: BeforeResolveResult) -> Result<BeforeResolveResult> {
if let BeforeResolveResult::Data(data) = &mut result {
if self.resource_reg_exp.test(&data.request)
&& let Some(new_content_resource) = &self.new_content_resource
{
if let BeforeResolveResult::Data(data) = &mut result
&& self.resource_reg_exp.test(&data.request)
{
if let Some(new_content_resource) = &self.new_content_resource {
data.request = new_content_resource.clone();
}
if let Some(new_content_recursive) = self.new_content_recursive {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "asset1";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "asset2";
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
it("should not affect contexts that don't match the regex", function () {
// This context does NOT match /components$/ so should not be affected
var assetsContext = require.context("./assets", false, /\.js$/);
var assetKeys = assetsContext.keys();

// Should find the asset files since the plugin shouldn't affect this context
expect(assetKeys.length).toBe(2);
expect(assetKeys).toContain("./file1.js");
expect(assetKeys).toContain("./file2.js");

// Verify we can actually load the files
expect(assetsContext("./file1.js")).toBe("asset1");
expect(assetsContext("./file2.js")).toBe("asset2");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var webpack = require("@rspack/core");

/** @type {import("@rspack/core").Configuration} */
module.exports = {
plugins: [
// This plugin should only affect contexts matching /components$/
// and should NOT affect the "assets" context
new webpack.ContextReplacementPlugin(
/components$/,
"./replaced-components",
true,
/^replaced$/
)
]
};
Loading