Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: module.rule[].dependency #2562

Merged
merged 5 commits into from
Mar 31, 2023
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
6 changes: 6 additions & 0 deletions .changeset/moody-needles-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rspack/binding": patch
"@rspack/core": patch
---

feat: module.rule[].dependency
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export interface RawModuleRule {
generator?: RawModuleRuleGenerator
resolve?: RawResolveOptions
issuer?: RawRuleSetCondition
dependency?: RawRuleSetCondition
oneOf?: Array<RawModuleRule>
}
export interface RawModuleRuleGenerator {
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_options/src/options/raw_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ pub struct RawModuleRule {
pub generator: Option<RawModuleRuleGenerator>,
pub resolve: Option<RawResolveOptions>,
pub issuer: Option<RawRuleSetCondition>,
pub dependency: Option<RawRuleSetCondition>,
pub one_of: Option<Vec<RawModuleRule>>,
}

Expand Down Expand Up @@ -589,6 +590,7 @@ impl TryFrom<RawModuleRule> for ModuleRule {
resolve: value.resolve.map(|raw| raw.try_into()).transpose()?,
side_effects: value.side_effects,
issuer: value.issuer.map(|raw| raw.try_into()).transpose()?,
dependency: value.dependency.map(|raw| raw.try_into()).transpose()?,
one_of,
})
}
Expand Down
20 changes: 19 additions & 1 deletion crates/rspack_core/src/dependency/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ pub use common_js_require_context_dependency::*;
pub use const_dependency::ConstDependency;
pub use import_context_dependency::*;
mod require_context_dependency;
use std::{any::Any, fmt::Debug, hash::Hash};
use std::{
any::Any,
fmt::{Debug, Display},
hash::Hash,
};

use dyn_clone::{clone_trait_object, DynClone};
pub use require_context_dependency::RequireContextDependency;
Expand Down Expand Up @@ -95,6 +99,20 @@ impl From<&str> for DependencyCategory {
}
}

impl Display for DependencyCategory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DependencyCategory::Unknown => write!(f, "unknown"),
DependencyCategory::Esm => write!(f, "esm"),
DependencyCategory::CommonJS => write!(f, "commonjs"),
DependencyCategory::Url => write!(f, "url"),
DependencyCategory::CssImport => write!(f, "css-import"),
DependencyCategory::CssCompose => write!(f, "css-compose"),
DependencyCategory::Wasm => write!(f, "wasm"),
}
}
}

pub trait Dependency:
CodeGeneratable + AsAny + DynHash + DynClone + DynEq + Send + Sync + Debug
{
Expand Down
45 changes: 19 additions & 26 deletions crates/rspack_core/src/normal_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use swc_core::common::Span;

use crate::{
cache::Cache, module_rule_matcher, resolve, AssetGeneratorOptions, AssetParserOptions,
CompilerOptions, Dependency, FactorizeArgs, MissingModule, ModuleArgs, ModuleDependency,
ModuleExt, ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier,
ModuleRule, ModuleType, NormalModule, NormalModuleFactoryResolveForSchemeArgs, RawModule,
Resolve, ResolveArgs, ResolveError, ResolveResult, ResourceData, SharedPluginDriver,
CompilerOptions, Dependency, DependencyCategory, FactorizeArgs, MissingModule, ModuleArgs,
ModuleDependency, ModuleExt, ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult,
ModuleIdentifier, ModuleRule, ModuleType, NormalModule, NormalModuleFactoryResolveForSchemeArgs,
RawModule, Resolve, ResolveArgs, ResolveError, ResolveResult, ResourceData, SharedPluginDriver,
};

#[derive(Debug)]
Expand Down Expand Up @@ -172,26 +172,11 @@ impl NormalModuleFactory {
}
};
//TODO: with contextScheme
let loaders = self
.context
.options
.module
.rules
.iter()
.filter_map(|module_rule| -> Option<Result<&ModuleRule>> {
match module_rule_matcher(
module_rule,
&resource_data,
importer.map(|i| i.to_string_lossy()).as_deref(),
) {
Ok(val) => val.map(Ok),
Err(err) => Some(Err(err)),
}
})
.collect::<Result<Vec<_>>>()?;
let resolved_module_rules =
self.calculate_module_rules(&resource_data, data.dependency.category())?;

let loaders = loaders
.into_iter()
let loaders = resolved_module_rules
.iter()
.flat_map(|module_rule| module_rule.r#use.iter().cloned().rev())
.collect::<Vec<_>>();

Expand All @@ -210,7 +195,6 @@ impl NormalModuleFactory {

let file_dependency = resource_data.resource_path.clone();

let resolved_module_rules = self.calculate_module_rules(&resource_data)?;
let resolved_module_type =
self.calculate_module_type(&resolved_module_rules, self.context.module_type);
let resolved_resolve_options = self.calculate_resolve_options(&resolved_module_rules);
Expand Down Expand Up @@ -270,15 +254,24 @@ impl NormalModuleFactory {
))
}

fn calculate_module_rules(&self, resource_data: &ResourceData) -> Result<Vec<&ModuleRule>> {
fn calculate_module_rules(
&self,
resource_data: &ResourceData,
dependency: &DependencyCategory,
) -> Result<Vec<&ModuleRule>> {
self
.context
.options
.module
.rules
.iter()
.filter_map(|module_rule| -> Option<Result<&ModuleRule>> {
match module_rule_matcher(module_rule, resource_data, self.context.issuer.as_deref()) {
match module_rule_matcher(
module_rule,
resource_data,
self.context.issuer.as_deref(),
dependency,
) {
Ok(val) => val.map(Ok),
Err(err) => Some(Err(err)),
}
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/options/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub struct ModuleRule {
/// A condition matcher against the resource query.
pub resource_query: Option<RuleSetCondition>,
pub side_effects: Option<bool>,
pub dependency: Option<RuleSetCondition>,
/// The `ModuleType` to use for the matched resource.
pub r#type: Option<ModuleType>,
pub r#use: Vec<BoxLoader>,
Expand Down
14 changes: 11 additions & 3 deletions crates/rspack_core/src/utils/module_rules.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
use rspack_error::{internal_error, Result};
use rspack_loader_runner::ResourceData;

use crate::ModuleRule;
use crate::{DependencyCategory, ModuleRule};

/// Match the `ModuleRule` against the given `ResourceData`, and return the matching `ModuleRule` if matched.
pub fn module_rule_matcher<'a>(
module_rule: &'a ModuleRule,
resource_data: &ResourceData,
issuer: Option<&str>,
dependency: &DependencyCategory,
) -> Result<Option<&'a ModuleRule>> {
if module_rule.test.is_none()
&& module_rule.resource.is_none()
&& module_rule.resource_query.is_none()
&& module_rule.include.is_none()
&& module_rule.exclude.is_none()
&& module_rule.issuer.is_none()
&& module_rule.dependency.is_none()
&& module_rule.one_of.is_none()
{
return Err(internal_error!(
"ModuleRule must have at least one condition"
));
}

module_rule_matcher_inner(module_rule, resource_data, issuer)
module_rule_matcher_inner(module_rule, resource_data, issuer, dependency)
}

pub fn module_rule_matcher_inner<'a>(
module_rule: &'a ModuleRule,
resource_data: &ResourceData,
issuer: Option<&str>,
dependency: &DependencyCategory,
) -> Result<Option<&'a ModuleRule>> {
// Include all modules that pass test assertion. If you supply a Rule.test option, you cannot also supply a `Rule.resource`.
// See: https://webpack.js.org/configuration/module/#ruletest
Expand Down Expand Up @@ -66,9 +69,14 @@ pub fn module_rule_matcher_inner<'a>(
return Ok(None);
}

if let Some(dependency_rule) = &module_rule.dependency
&& !dependency_rule.try_match(&dependency.to_string())? {
return Ok(None);
}

if let Some(one_of) = &module_rule.one_of {
for rule in one_of {
if let Some(rule) = module_rule_matcher_inner(rule, resource_data, issuer)? {
if let Some(rule) = module_rule_matcher_inner(rule, resource_data, issuer, dependency)? {
return Ok(Some(rule));
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/rspack/src/config/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ const getRawModuleRule = (
test: rule.test ? getRawRuleSetCondition(rule.test) : undefined,
include: rule.include ? getRawRuleSetCondition(rule.include) : undefined,
exclude: rule.exclude ? getRawRuleSetCondition(rule.exclude) : undefined,
issuer: rule.issuer ? getRawRuleSetCondition(rule.issuer) : undefined,
dependency: rule.dependency
? getRawRuleSetCondition(rule.dependency)
: undefined,
resource: rule.resource ? getRawRuleSetCondition(rule.resource) : undefined,
resourceQuery: rule.resourceQuery
? getRawRuleSetCondition(rule.resourceQuery)
Expand All @@ -274,7 +278,6 @@ const getRawModuleRule = (
parser: rule.parser,
generator: rule.generator,
resolve: rule.resolve ? getRawResolve(rule.resolve) : undefined,
issuer: rule.issuer ? getRawRuleSetCondition(rule.issuer) : undefined,
oneOf: rule.oneOf
? rule.oneOf.map(i => getRawModuleRule(i, options))
: undefined
Expand Down
13 changes: 12 additions & 1 deletion packages/rspack/src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ const applyModuleDefaults = (
}
]
});

if (asyncWebAssembly) {
const wasm = {
type: "webassembly/async",
Expand All @@ -255,6 +254,18 @@ const applyModuleDefaults = (
...wasm
});
}
rules.push({
dependency: "url",
oneOf: [
// {
// scheme: /^data$/,
// type: "asset/inline"
// },
{
type: "asset/resource"
}
]
});

return rules;
});
Expand Down
8 changes: 8 additions & 0 deletions packages/rspack/src/config/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,14 @@ module.exports = {
}
]
},
dependency: {
description: "Match dependency type.",
oneOf: [
{
$ref: "#/definitions/RuleSetConditionOrConditions"
}
]
},
oneOf: {
description: "Only execute the first matching rule in this array.",
type: "array",
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export interface RuleSetRule {
exclude?: RuleSetCondition;
include?: RuleSetCondition;
issuer?: RuleSetCondition;
dependency?: RuleSetCondition;
resource?: RuleSetCondition;
resourceFragment?: RuleSetCondition;
resourceQuery?: RuleSetCondition;
Expand Down
8 changes: 8 additions & 0 deletions packages/rspack/tests/Defaults.unittest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ describe("snapshots", () => {
],
"test": /\\\\\\.css\\$/i,
},
{
"dependency": "url",
"oneOf": [
{
"type": "asset/resource",
},
],
},
],
"parser": {
"asset": {
Expand Down
1 change: 0 additions & 1 deletion packages/rspack/tests/case.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export function describeCases(config: { name: string; casePath: string }) {
},
...config, // we may need to use deepMerge to handle config merge, but we may fix it until we need it
output: {
publicPath: "/",
// @ts-ignore
...config.output,
path: outputPath
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
output: {
publicPath: "/",
cssFilename: "css/[name].css"
},
resolve: {
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/tests/cases/css/rewrite-url/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ it("should rewrite the css url()", function () {
expect(a.startsWith("./")).toBe(false);
expect(a.includes("./logo.png")).toBe(false);
expect(a.endsWith(".png")).toBe(true);
expect(a === "/19f79ab99b58da29.png").toBe(true);
expect(a === "19f79ab99b58da29.png").toBe(true);
const b = /b: url\((.*)\);/.exec(css)[1];
expect(b).toBe(
'"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAOCAYAAAAbvf3sAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAEWSURBVHgBjZFRSsNAEIb/3fQAOUK9gfpe6YIovjU3UE+gJ2g8gTcw3sC+itUFrW+BeAN7ATFSKAjbHWcSC+mSlszLDrvfzvzzj0LHmNpZBtBQd4Gf7fuYgHPPueoCe1DK1UsHd7Dzw+P0daQj/SC5h09OzdGktw221vZXUBlVMN0ILPd6O9yzBBXX8CBdv6kWOGa4YLivQJNjM0ia73oTLsBwJjCB5gu4i7CgbsIey5ThEcOfKziTGFOGHypJeZ7jZ/Gbst4x2/fN9h2eGTNvgk92VrDEWLNmfJXLpIYrRy5b4Fs+9v2/pL0oUncI7FuHLI6PK5lJZGoe8qXNvrry27DesoRLpLPmNkTw9yEcxPWJMR+S/AFbfpAZqxwUNQAAAABJRU5ErkJggg=="'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
output: {
publicPath: "/"
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const currentDir = require("url").pathToFileURL(__dirname);

it("should handle import.meta.url in URL()", () => {
const { href } = new URL("./index.css", import.meta.url);

expect(href).toBe(currentDir + "/public/index.css");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
mode: "development",
target: "node",
devtool: false,
output: {
assetModuleFilename: "[name][ext]",
publicPath: "public/"
},
module: {
rules: [
{
test: /\.css$/,
dependency: ["esm", "commonjs"],
use: "url-loader"
}
]
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const currentDir = require("url").pathToFileURL(__dirname);

it("should handle import.meta.url in URL()", () => {
const { href } = new URL("./index.css", import.meta.url);

expect(href).toBe(currentDir + "/index.css");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
mode: "development",
target: "node",
devtool: false,
output: {
assetModuleFilename: "[name][ext]"
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module.exports = {
context: __dirname,
output: {
publicPath: "/",
assetModuleFilename: "asset/[name][ext]"
},
module: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default () => new Worker(new URL("./chunk2.js", import.meta.url)); // TODO(ahabhgk): should be "./chunk2" WorkerDependency instead of URLDependency
export default () => new Worker(new URL("./chunk2", import.meta.url));
---
export default 42;
Loading