Skip to content

Commit 0befe44

Browse files
authored
feat: add url config to css parser (#10229)
1 parent 8a5dba1 commit 0befe44

File tree

13 files changed

+176
-11
lines changed

13 files changed

+176
-11
lines changed

crates/node_binding/binding.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,7 @@ export interface RawCssAutoGeneratorOptions {
16211621

16221622
export interface RawCssAutoParserOptions {
16231623
namedExports?: boolean
1624+
url?: boolean
16241625
}
16251626

16261627
export interface RawCssExtractPluginOption {
@@ -1649,10 +1650,12 @@ export interface RawCssModuleGeneratorOptions {
16491650

16501651
export interface RawCssModuleParserOptions {
16511652
namedExports?: boolean
1653+
url?: boolean
16521654
}
16531655

16541656
export interface RawCssParserOptions {
16551657
namedExports?: boolean
1658+
url?: boolean
16561659
}
16571660

16581661
export interface RawDllEntryPluginOptions {

crates/node_binding/src/raw_options/raw_module/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,14 @@ impl From<RawAssetParserDataUrlOptions> for AssetParserDataUrlOptions {
409409
#[napi(object)]
410410
pub struct RawCssParserOptions {
411411
pub named_exports: Option<bool>,
412+
pub url: Option<bool>,
412413
}
413414

414415
impl From<RawCssParserOptions> for CssParserOptions {
415416
fn from(value: RawCssParserOptions) -> Self {
416417
Self {
417418
named_exports: value.named_exports,
419+
url: value.url,
418420
}
419421
}
420422
}
@@ -423,12 +425,14 @@ impl From<RawCssParserOptions> for CssParserOptions {
423425
#[napi(object)]
424426
pub struct RawCssAutoParserOptions {
425427
pub named_exports: Option<bool>,
428+
pub url: Option<bool>,
426429
}
427430

428431
impl From<RawCssAutoParserOptions> for CssAutoParserOptions {
429432
fn from(value: RawCssAutoParserOptions) -> Self {
430433
Self {
431434
named_exports: value.named_exports,
435+
url: value.url,
432436
}
433437
}
434438
}
@@ -437,12 +441,14 @@ impl From<RawCssAutoParserOptions> for CssAutoParserOptions {
437441
#[napi(object)]
438442
pub struct RawCssModuleParserOptions {
439443
pub named_exports: Option<bool>,
444+
pub url: Option<bool>,
440445
}
441446

442447
impl From<RawCssModuleParserOptions> for CssModuleParserOptions {
443448
fn from(value: RawCssModuleParserOptions) -> Self {
444449
Self {
445450
named_exports: value.named_exports,
451+
url: value.url,
446452
}
447453
}
448454
}

crates/rspack/src/builder/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,16 +1731,19 @@ impl ModuleOptionsBuilder {
17311731
if css {
17321732
let css_parser_options = ParserOptions::Css(CssParserOptions {
17331733
named_exports: Some(true),
1734+
url: Some(true),
17341735
});
17351736
parser.insert("css".to_string(), css_parser_options.clone());
17361737

17371738
let css_auto_parser_options = ParserOptions::CssAuto(CssAutoParserOptions {
17381739
named_exports: Some(true),
1740+
url: Some(true),
17391741
});
17401742
parser.insert("css/auto".to_string(), css_auto_parser_options);
17411743

17421744
let css_module_parser_options = ParserOptions::CssModule(CssModuleParserOptions {
17431745
named_exports: Some(true),
1746+
url: Some(true),
17441747
});
17451748
parser.insert("css/module".to_string(), css_module_parser_options);
17461749

crates/rspack_core/src/options/module.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,18 +286,21 @@ pub struct AssetParserDataUrlOptions {
286286
#[derive(Debug, Clone, MergeFrom)]
287287
pub struct CssParserOptions {
288288
pub named_exports: Option<bool>,
289+
pub url: Option<bool>,
289290
}
290291

291292
#[cacheable]
292293
#[derive(Debug, Clone, MergeFrom)]
293294
pub struct CssAutoParserOptions {
294295
pub named_exports: Option<bool>,
296+
pub url: Option<bool>,
295297
}
296298

297299
impl From<CssParserOptions> for CssAutoParserOptions {
298300
fn from(value: CssParserOptions) -> Self {
299301
Self {
300302
named_exports: value.named_exports,
303+
url: value.url,
301304
}
302305
}
303306
}
@@ -306,12 +309,14 @@ impl From<CssParserOptions> for CssAutoParserOptions {
306309
#[derive(Debug, Clone, MergeFrom)]
307310
pub struct CssModuleParserOptions {
308311
pub named_exports: Option<bool>,
312+
pub url: Option<bool>,
309313
}
310314

311315
impl From<CssParserOptions> for CssModuleParserOptions {
312316
fn from(value: CssParserOptions) -> Self {
313317
Self {
314318
named_exports: value.named_exports,
319+
url: value.url,
315320
}
316321
}
317322
}

crates/rspack_plugin_css/src/parser_and_generator/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub struct CssParserAndGenerator {
8080
pub exports_only: bool,
8181
pub named_exports: bool,
8282
pub es_module: bool,
83+
pub url: bool,
8384
#[cacheable(with=AsOption<AsMap<AsCacheable, AsVec>>)]
8485
pub exports: Option<CssExports>,
8586
pub local_names: Option<FxHashMap<String, String>>,
@@ -168,6 +169,10 @@ impl ParserAndGenerator for CssParserAndGenerator {
168169
if request.trim().is_empty() {
169170
continue;
170171
}
172+
if !self.url {
173+
continue;
174+
}
175+
171176
let request = replace_module_request_prefix(
172177
request,
173178
&mut diagnostics,

crates/rspack_plugin_css/src/plugin/impl_plugin_for_css_plugin.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ impl Plugin for CssPlugin {
488488
named_exports: p.named_exports.expect("should have named_exports"),
489489
es_module: g.es_module.expect("should have es_module"),
490490
hot: false,
491+
url: p.url.expect("should have url"),
491492
}) as Box<dyn ParserAndGenerator>
492493
}),
493494
);
@@ -516,6 +517,7 @@ impl Plugin for CssPlugin {
516517
named_exports: p.named_exports.expect("should have named_exports"),
517518
es_module: g.es_module.expect("should have es_module"),
518519
hot: false,
520+
url: p.url.expect("should have url"),
519521
}) as Box<dyn ParserAndGenerator>
520522
}),
521523
);
@@ -544,6 +546,7 @@ impl Plugin for CssPlugin {
544546
named_exports: p.named_exports.expect("should have named_exports"),
545547
es_module: g.es_module.expect("should have es_module"),
546548
hot: false,
549+
url: p.url.expect("should have url"),
547550
}) as Box<dyn ParserAndGenerator>
548551
}),
549552
);

packages/rspack-test-tools/tests/defaultsCases/experiments/future-defaults.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ module.exports = {
4141
+ Object {
4242
+ "descriptionData": Object {
4343
+ "type": "module",
44-
+ },
44+
@@ ... @@
4545
+ "resolve": Object {
4646
+ "fullySpecified": true,
4747
+ },
4848
+ },
4949
+ ],
5050
+ "type": "webassembly/async",
5151
+ },
52-
+ Object {
52+
@@ ... @@
5353
+ "resolve": Object {
5454
+ "fullySpecified": true,
5555
+ "preferRelative": true,
@@ -72,6 +72,8 @@ module.exports = {
7272
+ "preferRelative": true,
7373
+ },
7474
+ "type": "css",
75+
+ },
76+
+ Object {
7577
@@ ... @@
7678
+ "css": Object {
7779
+ "esModule": true,
@@ -94,20 +96,21 @@ module.exports = {
9496
+ },
9597
+ "css": Object {
9698
+ "namedExports": true,
97-
@@ ... @@
99+
+ "url": true,
100+
+ },
98101
+ "css/auto": Object {
99102
+ "namedExports": true,
100-
+ },
103+
+ "url": true,
104+
@@ ... @@
101105
+ "css/module": Object {
102106
+ "namedExports": true,
107+
+ "url": true,
103108
@@ ... @@
104109
+ "css",
105110
@@ ... @@
106111
- "charset": true,
107112
+ "charset": false,
108113
@@ ... @@
109-
+ "...",
110-
+ ],
111114
+ },
112115
+ "css-import": Object {
113116
+ "conditionNames": Array [
@@ -120,7 +123,8 @@ module.exports = {
120123
+ ],
121124
+ "mainFields": Array [
122125
+ "style",
123-
@@ ... @@
126+
+ "...",
127+
+ ],
124128
+ "mainFiles": Array [],
125129
+ "preferRelative": true,
126130
`)

packages/rspack/src/config/adapter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,8 @@ function getRawCssParserOptions(
607607
parser: CssParserOptions
608608
): RawCssParserOptions | RawCssAutoParserOptions | RawCssModuleParserOptions {
609609
return {
610-
namedExports: parser.namedExports
610+
namedExports: parser.namedExports,
611+
url: parser.url
611612
};
612613
}
613614

packages/rspack/src/config/defaults.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,17 @@ const applyModuleDefaults = (
344344
F(module.parser, "css", () => ({}));
345345
assertNotNill(module.parser.css);
346346
D(module.parser.css, "namedExports", true);
347+
D(module.parser.css, "url", true);
347348

348349
F(module.parser, "css/auto", () => ({}));
349350
assertNotNill(module.parser["css/auto"]);
350351
D(module.parser["css/auto"], "namedExports", true);
352+
D(module.parser["css/auto"], "url", true);
351353

352354
F(module.parser, "css/module", () => ({}));
353355
assertNotNill(module.parser["css/module"]);
354356
D(module.parser["css/module"], "namedExports", true);
357+
D(module.parser["css/module"], "url", true);
355358

356359
// IGNORE(module.generator): already check to align in 2024.6.27
357360
F(module.generator, "css", () => ({}));

packages/rspack/src/config/types.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,7 @@ export type AssetParserOptions = {
962962
};
963963

964964
export type CssParserNamedExports = boolean;
965+
export type CssParserUrl = boolean;
965966

966967
/** Options object for `css` modules. */
967968
export type CssParserOptions = {
@@ -970,6 +971,12 @@ export type CssParserOptions = {
970971
* @default true
971972
* */
972973
namedExports?: CssParserNamedExports;
974+
975+
/**
976+
* Allow to enable/disables handling the CSS functions url.
977+
* @default true
978+
* */
979+
url?: CssParserUrl;
973980
};
974981

975982
/** Options object for `css/auto` modules. */
@@ -979,6 +986,12 @@ export type CssAutoParserOptions = {
979986
* @default true
980987
* */
981988
namedExports?: CssParserNamedExports;
989+
990+
/**
991+
* Allow to enable/disables handling the CSS functions url.
992+
* @default true
993+
* */
994+
url?: CssParserUrl;
982995
};
983996

984997
/** Options object for `css/module` modules. */
@@ -988,6 +1001,12 @@ export type CssModuleParserOptions = {
9881001
* @default true
9891002
* */
9901003
namedExports?: CssParserNamedExports;
1004+
1005+
/**
1006+
* Allow to enable/disables handling the CSS functions url.
1007+
* @default true
1008+
* */
1009+
url?: CssParserUrl;
9911010
};
9921011

9931012
type ExportsPresence = "error" | "warn" | "auto" | false;

0 commit comments

Comments
 (0)