Skip to content

Commit fefaefc

Browse files
committed
feat(asset): add importMode: "newURL"
1 parent e598f28 commit fefaefc

File tree

6 files changed

+55
-26
lines changed

6 files changed

+55
-26
lines changed

crates/node_binding/napi-binding.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,7 @@ export interface RawAssetGeneratorOptions {
17321732
outputPath?: JsFilename
17331733
publicPath?: "auto" | JsFilename
17341734
dataUrl?: RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)
1735-
importMode?: "url" | "preserve"
1735+
importMode?: "url" | "preserve" | "newURL"
17361736
binary?: boolean
17371737
}
17381738

@@ -1759,7 +1759,7 @@ export interface RawAssetResourceGeneratorOptions {
17591759
filename?: JsFilename
17601760
outputPath?: JsFilename
17611761
publicPath?: "auto" | JsFilename
1762-
importMode?: "url" | "preserve"
1762+
importMode?: "url" | "preserve" | "newURL"
17631763
binary?: boolean
17641764
}
17651765

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ pub struct RawAssetGeneratorOptions {
575575
)]
576576
pub data_url: Option<RawAssetGeneratorDataUrl>,
577577

578-
#[napi(ts_type = r#""url" | "preserve""#)]
578+
#[napi(ts_type = r#""url" | "preserve" | "newURL""#)]
579579
pub import_mode: Option<String>,
580580
pub binary: Option<bool>,
581581
}
@@ -626,7 +626,7 @@ pub struct RawAssetResourceGeneratorOptions {
626626
pub output_path: Option<JsFilename>,
627627
#[napi(ts_type = "\"auto\" | JsFilename")]
628628
pub public_path: Option<JsFilename>,
629-
#[napi(ts_type = r#""url" | "preserve""#)]
629+
#[napi(ts_type = r#""url" | "preserve" | "newURL""#)]
630630
pub import_mode: Option<String>,
631631
pub binary: Option<bool>,
632632
}

crates/rspack_core/src/options/module.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ bitflags! {
504504
impl AssetGeneratorImportModeFlags: u8 {
505505
const URL = 1 << 0;
506506
const PRESERVE = 1 << 1;
507+
const NEW_URL = 1 << 2;
507508
}
508509
}
509510

@@ -518,14 +519,18 @@ impl AssetGeneratorImportMode {
518519
pub fn is_preserve(&self) -> bool {
519520
self.0.contains(AssetGeneratorImportModeFlags::PRESERVE)
520521
}
522+
pub fn is_new_url(&self) -> bool {
523+
self.0.contains(AssetGeneratorImportModeFlags::NEW_URL)
524+
}
521525
}
522526

523527
impl From<String> for AssetGeneratorImportMode {
524528
fn from(s: String) -> Self {
525529
match s.as_str() {
526530
"url" => Self(AssetGeneratorImportModeFlags::URL),
527531
"preserve" => Self(AssetGeneratorImportModeFlags::PRESERVE),
528-
_ => unreachable!("AssetGeneratorImportMode error"),
532+
"newURL" => Self(AssetGeneratorImportModeFlags::NEW_URL),
533+
_ => unreachable!("asset generator import mode should be url, preserve or newURL"),
529534
}
530535
}
531536
}

crates/rspack_plugin_asset/src/lib.rs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {
562562
)
563563
.await?;
564564

565-
let asset_path = if import_mode.is_preserve() {
565+
let asset_path = if import_mode.is_preserve() || import_mode.is_new_url() {
566566
generate_context
567567
.data
568568
.insert(CodeGenerationPublicPathAutoReplace(true));
@@ -631,37 +631,60 @@ impl ParserAndGenerator for AssetParserAndGenerator {
631631
return Ok(RawStringSource::from("").boxed());
632632
}
633633

634-
if import_mode.is_preserve() && parsed_asset_config.is_resource() {
635-
let is_module = compilation.options.output.module;
636-
if let Some(ref mut scope) = generate_context.concatenation_scope {
637-
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
638-
if is_module {
634+
if parsed_asset_config.is_resource() {
635+
if import_mode.is_preserve() {
636+
let is_module = compilation.options.output.module;
637+
if let Some(ref mut scope) = generate_context.concatenation_scope {
638+
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
639+
if is_module {
640+
return Ok(
641+
RawStringSource::from(format!(
642+
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};"#
643+
))
644+
.boxed(),
645+
);
646+
} else {
647+
let supports_const = compilation.options.output.environment.supports_const();
648+
let declaration_kind = if supports_const { "const" } else { "var" };
649+
return Ok(
650+
RawStringSource::from(format!(
651+
r#"{declaration_kind} {NAMESPACE_OBJECT_EXPORT} = require({exported_content});"#
652+
))
653+
.boxed(),
654+
);
655+
}
656+
} else {
657+
generate_context
658+
.runtime_requirements
659+
.insert(RuntimeGlobals::MODULE);
660+
return Ok(
661+
RawStringSource::from(format!(r#"module.exports = require({exported_content});"#))
662+
.boxed(),
663+
);
664+
}
665+
};
666+
667+
if import_mode.is_new_url() {
668+
if let Some(ref mut scope) = generate_context.concatenation_scope {
669+
let supports_const = compilation.options.output.environment.supports_const();
670+
let declaration_kind = if supports_const { "const" } else { "var" };
671+
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
639672
return Ok(
640673
RawStringSource::from(format!(
641-
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};"#
674+
r#"{declaration_kind} {NAMESPACE_OBJECT_EXPORT} = /*#__PURE__*/ new URL({exported_content}, import.meta.url).href;"#
642675
))
643676
.boxed(),
644677
);
645678
} else {
646-
let supports_const = compilation.options.output.environment.supports_const();
647-
let declaration_kind = if supports_const { "const" } else { "var" };
648679
return Ok(
649680
RawStringSource::from(format!(
650-
r#"{declaration_kind} {NAMESPACE_OBJECT_EXPORT} = require({exported_content});"#
681+
r#"module.exports = /*#__PURE__*/ new URL({exported_content}, import.meta.url).href;"#
651682
))
652683
.boxed(),
653684
);
654685
}
655-
} else {
656-
generate_context
657-
.runtime_requirements
658-
.insert(RuntimeGlobals::MODULE);
659-
return Ok(
660-
RawStringSource::from(format!(r#"module.exports = require({exported_content});"#))
661-
.boxed(),
662-
);
663686
}
664-
};
687+
}
665688

666689
if let Some(ref mut scope) = generate_context.concatenation_scope {
667690
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);

packages/rspack/etc/core.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export type AssetInlineGeneratorOptions = {
237237
export type AssetModuleFilename = Filename;
238238

239239
// @public
240-
export type AssetModuleImportMode = "url" | "preserve";
240+
export type AssetModuleImportMode = "url" | "preserve" | "newURL";
241241

242242
// @public
243243
export type AssetModuleOutputPath = Filename;

packages/rspack/src/config/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ export type AssetModuleOutputPath = Filename;
12021202
* Only for modules with module type 'asset' or 'asset/resource'.
12031203
* @default "url"
12041204
*/
1205-
export type AssetModuleImportMode = "url" | "preserve";
1205+
export type AssetModuleImportMode = "url" | "preserve" | "newURL";
12061206

12071207
/** Options for asset modules. */
12081208
export type AssetResourceGeneratorOptions = {
@@ -1224,6 +1224,7 @@ export type AssetResourceGeneratorOptions = {
12241224
/**
12251225
* If "url", a URL pointing to the asset will be generated based on publicPath.
12261226
* If "preserve", preserve import/require statement from generated asset.
1227+
* If "newURL", a new URL object will be created for the asset.
12271228
* Only for modules with module type 'asset' or 'asset/resource'.
12281229
* @default "url"
12291230
*/

0 commit comments

Comments
 (0)