Skip to content

Commit 397baeb

Browse files
committed
chore: rename to import_mode
1 parent fd59d53 commit 397baeb

File tree

9 files changed

+122
-67
lines changed

9 files changed

+122
-67
lines changed

crates/node_binding/binding.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ export interface RawAssetGeneratorOptions {
11431143
outputPath?: JsFilename
11441144
publicPath?: "auto" | JsFilename
11451145
dataUrl?: RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)
1146-
experimentalPreserveImport?: boolean
1146+
importMode?: "url" | "preserve"
11471147
}
11481148

11491149
export interface RawAssetInlineGeneratorOptions {
@@ -1168,7 +1168,7 @@ export interface RawAssetResourceGeneratorOptions {
11681168
filename?: JsFilename
11691169
outputPath?: JsFilename
11701170
publicPath?: "auto" | JsFilename
1171-
experimentalPreserveImport?: boolean
1171+
importMode?: "url" | "preserve"
11721172
}
11731173

11741174
export interface RawBannerPluginOptions {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,8 @@ pub struct RawAssetGeneratorOptions {
542542
)]
543543
pub data_url: Option<RawAssetGeneratorDataUrl>,
544544

545-
pub experimental_preserve_import: Option<bool>,
545+
#[napi(ts_type = r#""url" | "preserve""#)]
546+
pub import_mode: Option<String>,
546547
}
547548

548549
impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
@@ -555,7 +556,7 @@ impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
555556
data_url: value
556557
.data_url
557558
.map(|i| RawAssetGeneratorDataUrlWrapper(i).into()),
558-
experimental_preserve_import: value.experimental_preserve_import,
559+
import_mode: value.import_mode.map(|n| n.into()),
559560
}
560561
}
561562
}
@@ -588,8 +589,8 @@ pub struct RawAssetResourceGeneratorOptions {
588589
pub output_path: Option<JsFilename>,
589590
#[napi(ts_type = "\"auto\" | JsFilename")]
590591
pub public_path: Option<JsFilename>,
591-
592-
pub experimental_preserve_import: Option<bool>,
592+
#[napi(ts_type = r#""url" | "preserve""#)]
593+
pub import_mode: Option<String>,
593594
}
594595

595596
impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
@@ -599,7 +600,7 @@ impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
599600
filename: value.filename.map(|i| i.into()),
600601
output_path: value.output_path.map(|i| i.into()),
601602
public_path: value.public_path.map(|i| i.into()),
602-
experimental_preserve_import: value.experimental_preserve_import,
603+
import_mode: value.import_mode.map(|i| i.into()),
603604
}
604605
}
605606
}

crates/rspack_core/src/options/module.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,14 +472,53 @@ impl From<AssetGeneratorOptions> for AssetInlineGeneratorOptions {
472472
}
473473
}
474474

475+
#[cacheable]
476+
#[derive(Debug, Clone, Copy, MergeFrom)]
477+
struct AssetGeneratorImportModeFlags(u8);
478+
bitflags! {
479+
impl AssetGeneratorImportModeFlags: u8 {
480+
const URL = 1 << 0;
481+
const PRESERVE = 1 << 1;
482+
}
483+
}
484+
485+
#[cacheable]
486+
#[derive(Debug, Clone, Copy, MergeFrom)]
487+
pub struct AssetGeneratorImportMode(AssetGeneratorImportModeFlags);
488+
489+
impl AssetGeneratorImportMode {
490+
pub fn is_url(&self) -> bool {
491+
self.0.contains(AssetGeneratorImportModeFlags::URL)
492+
}
493+
pub fn is_preserve(&self) -> bool {
494+
self.0.contains(AssetGeneratorImportModeFlags::PRESERVE)
495+
}
496+
}
497+
498+
impl From<String> for AssetGeneratorImportMode {
499+
fn from(s: String) -> Self {
500+
match s.as_str() {
501+
"url" => Self(AssetGeneratorImportModeFlags::URL),
502+
"preserve" => Self(AssetGeneratorImportModeFlags::PRESERVE),
503+
_ => unreachable!("AssetGeneratorImportMode error"),
504+
}
505+
}
506+
}
507+
508+
impl Default for AssetGeneratorImportMode {
509+
fn default() -> Self {
510+
Self(AssetGeneratorImportModeFlags::URL)
511+
}
512+
}
513+
475514
#[cacheable]
476515
#[derive(Debug, Clone, MergeFrom)]
477516
pub struct AssetResourceGeneratorOptions {
478517
pub emit: Option<bool>,
479518
pub filename: Option<Filename>,
480519
pub output_path: Option<Filename>,
481520
pub public_path: Option<PublicPath>,
482-
pub experimental_preserve_import: Option<bool>,
521+
pub import_mode: Option<AssetGeneratorImportMode>,
483522
}
484523

485524
impl From<AssetGeneratorOptions> for AssetResourceGeneratorOptions {
@@ -489,7 +528,7 @@ impl From<AssetGeneratorOptions> for AssetResourceGeneratorOptions {
489528
filename: value.filename,
490529
output_path: value.output_path,
491530
public_path: value.public_path,
492-
experimental_preserve_import: value.experimental_preserve_import,
531+
import_mode: value.import_mode,
493532
}
494533
}
495534
}
@@ -502,7 +541,7 @@ pub struct AssetGeneratorOptions {
502541
pub output_path: Option<Filename>,
503542
pub public_path: Option<PublicPath>,
504543
pub data_url: Option<AssetGeneratorDataUrl>,
505-
pub experimental_preserve_import: Option<bool>,
544+
pub import_mode: Option<AssetGeneratorImportMode>,
506545
}
507546

508547
pub struct AssetGeneratorDataUrlFnCtx<'a> {

crates/rspack_plugin_asset/src/lib.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use rayon::prelude::*;
88
use rspack_cacheable::{cacheable, cacheable_dyn};
99
use rspack_core::{
1010
rspack_sources::{BoxSource, RawBufferSource, RawStringSource, SourceExt},
11-
AssetGeneratorDataUrl, AssetGeneratorDataUrlFnCtx, AssetInfo, AssetParserDataUrl,
12-
BuildMetaDefaultObject, BuildMetaExportsType, ChunkGraph, ChunkUkey, CodeGenerationDataAssetInfo,
13-
CodeGenerationDataFilename, CodeGenerationDataUrl, CodeGenerationPublicPathAutoReplace,
14-
Compilation, CompilationRenderManifest, CompilerOptions, Filename, GenerateContext,
15-
GeneratorOptions, LocalFilenameFn, Module, ModuleGraph, NormalModule, ParseContext,
16-
ParserAndGenerator, PathData, Plugin, PublicPath, RenderManifestEntry, ResourceData,
17-
RuntimeGlobals, RuntimeSpec, SourceType, NAMESPACE_OBJECT_EXPORT,
11+
AssetGeneratorDataUrl, AssetGeneratorDataUrlFnCtx, AssetGeneratorImportMode, AssetInfo,
12+
AssetParserDataUrl, BuildMetaDefaultObject, BuildMetaExportsType, ChunkGraph, ChunkUkey,
13+
CodeGenerationDataAssetInfo, CodeGenerationDataFilename, CodeGenerationDataUrl,
14+
CodeGenerationPublicPathAutoReplace, Compilation, CompilationRenderManifest, CompilerOptions,
15+
Filename, GenerateContext, GeneratorOptions, LocalFilenameFn, Module, ModuleGraph, NormalModule,
16+
ParseContext, ParserAndGenerator, PathData, Plugin, PublicPath, RenderManifestEntry,
17+
ResourceData, RuntimeGlobals, RuntimeSpec, SourceType, NAMESPACE_OBJECT_EXPORT,
1818
};
1919
use rspack_error::{error, Diagnostic, IntoTWithDiagnosticArray, Result};
2020
use rspack_hash::{RspackHash, RspackHashDigest};
@@ -305,21 +305,21 @@ impl AssetParserAndGenerator {
305305
Ok((public_path, info))
306306
}
307307

308-
fn get_experimental_lib_options(
308+
fn get_import_mode(
309309
&self,
310310
module_generator_options: Option<&GeneratorOptions>,
311-
) -> Result<bool> {
312-
let experimental_preserve_import = module_generator_options
311+
) -> Result<AssetGeneratorImportMode> {
312+
let import_mode = module_generator_options
313313
.and_then(|x| x.get_asset())
314-
.and_then(|x| x.experimental_preserve_import)
314+
.and_then(|x| x.import_mode)
315315
.or_else(|| {
316316
module_generator_options
317317
.and_then(|x| x.get_asset_resource())
318-
.and_then(|x| x.experimental_preserve_import)
318+
.and_then(|x| x.import_mode)
319319
})
320-
.unwrap_or(false);
320+
.unwrap_or_default();
321321

322-
Ok(experimental_preserve_import)
322+
Ok(import_mode)
323323
}
324324
}
325325

@@ -453,8 +453,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {
453453
.expect("module should be a NormalModule in AssetParserAndGenerator");
454454
let module_generator_options = normal_module.get_generator_options();
455455

456-
let experimental_preserve_import =
457-
self.get_experimental_lib_options(module_generator_options)?;
456+
let import_mode = self.get_import_mode(module_generator_options)?;
458457

459458
let result = match generate_context.requested_source_type {
460459
SourceType::JavaScript => {
@@ -500,7 +499,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {
500499
true,
501500
)?;
502501

503-
let asset_path = if experimental_preserve_import {
502+
let asset_path = if import_mode.is_preserve() {
504503
generate_context
505504
.data
506505
.insert(CodeGenerationPublicPathAutoReplace(true));
@@ -560,7 +559,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {
560559
unreachable!()
561560
};
562561

563-
if experimental_preserve_import && parsed_asset_config.is_resource() {
562+
if import_mode.is_preserve() && parsed_asset_config.is_resource() {
564563
let is_module = compilation.options.output.module;
565564
if let Some(ref mut scope) = generate_context.concatenation_scope {
566565
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);

packages/rspack-test-tools/tests/configCases/asset/preserve-import/rspack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
test: /\.png$/,
1313
type: "asset/resource",
1414
generator: {
15-
experimentalPreserveImport: true
15+
importMode: "preserve"
1616
}
1717
}
1818
]

packages/rspack-test-tools/tests/configCases/library/modern-module-asset-entry/rspack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = {
2424
type: "asset/resource",
2525
generator: {
2626
filename: 'static/img/[name].png',
27-
experimentalPreserveImport: true
27+
importMode: "preserve"
2828
}
2929
}
3030
]
@@ -46,7 +46,7 @@ module.exports = {
4646
const js = list.find(item => item.endsWith("js"));
4747
const jsContent = assets[js].source().toString();
4848

49-
const preseveImport = /import img_namespaceObject from ['"]\.\/static\/img\/img\.png['"]/.test(jsContent);
49+
const preseveImport = /import\simg_namespaceObject\sfrom ['"]\.\/static\/img\/img\.png['"]/.test(jsContent);
5050
assert(preseveImport);
5151
const hasExports = /export\s{\simg_namespaceObject\sas\sdefault\s}/.test(jsContent);
5252
assert(hasExports);

0 commit comments

Comments
 (0)