Skip to content

Commit ad92af5

Browse files
committed
chore: update
1 parent 15b1a19 commit ad92af5

File tree

8 files changed

+84
-69
lines changed

8 files changed

+84
-69
lines changed

crates/rspack_core/src/artifacts/code_generation_results.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ impl CodeGenerationDataUrl {
3131
}
3232
}
3333

34+
#[derive(Clone, Debug)]
35+
pub struct CodeGenerationPublicPathAutoMark(pub bool);
36+
3437
#[derive(Clone, Debug)]
3538
pub struct CodeGenerationDataFilename {
3639
filename: String,

crates/rspack_core/src/concatenated_module.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,6 @@ impl Module for ConcatenatedModule {
639639
};
640640
let runtime = runtime.as_deref();
641641
let context = compilation.options.context.clone();
642-
dbg!(&context);
643642

644643
let (modules_with_info, module_to_info_map) =
645644
self.get_modules_with_info(&compilation.get_module_graph(), runtime);
@@ -956,7 +955,6 @@ impl Module for ConcatenatedModule {
956955
continue;
957956
}
958957
let used_name = export_info.get_used_name(&module_graph, None, runtime);
959-
dbg!(&used_name);
960958

961959
let Some(used_name) = used_name else {
962960
unused_exports.insert(name);

crates/rspack_core/src/init_fragment.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ pub fn render_init_fragments<C: InitFragmentRenderContext>(
235235
for (key, fragments) in keyed_fragments {
236236
let f = key.merge_fragments(fragments);
237237
let contents = f.contents(context)?;
238+
dbg!(&contents.start, &contents.end);
238239
concat_source.add(RawStringSource::from(contents.start));
239240
if let Some(end_content) = contents.end {
240241
end_contents.push(RawStringSource::from(end_content))

crates/rspack_plugin_asset/src/lib.rs

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use rspack_core::{
1111
rspack_sources::{BoxSource, CachedSource, RawBufferSource, RawStringSource, SourceExt},
1212
AssetGeneratorDataUrl, AssetGeneratorDataUrlFnCtx, AssetInfo, AssetParserDataUrl,
1313
BuildMetaDefaultObject, BuildMetaExportsType, ChunkGraph, ChunkUkey, CodeGenerationDataAssetInfo,
14-
CodeGenerationDataFilename, CodeGenerationDataUrl, Compilation, CompilationRenderManifest,
15-
CompilerOptions, Filename, GenerateContext, GeneratorOptions, LocalFilenameFn, Module,
16-
ModuleGraph, NormalModule, ParseContext, ParserAndGenerator, PathData, Plugin, PublicPath,
17-
RenderManifestEntry, ResourceData, RuntimeGlobals, RuntimeSpec, SourceType,
14+
CodeGenerationDataFilename, CodeGenerationDataUrl, CodeGenerationPublicPathAutoMark, Compilation,
15+
CompilationRenderManifest, CompilerOptions, Filename, GenerateContext, GeneratorOptions,
16+
LocalFilenameFn, Module, ModuleGraph, NormalModule, ParseContext, ParserAndGenerator, PathData,
17+
Plugin, PublicPath, RenderManifestEntry, ResourceData, RuntimeGlobals, RuntimeSpec, SourceType,
1818
NAMESPACE_OBJECT_EXPORT,
1919
};
2020
use rspack_error::{error, Diagnostic, IntoTWithDiagnosticArray, Result};
@@ -459,6 +459,9 @@ impl ParserAndGenerator for AssetParserAndGenerator {
459459
.expect("module should be a NormalModule in AssetParserAndGenerator");
460460
let module_generator_options = normal_module.get_generator_options();
461461

462+
let (experimental_lib_preserve_import, experimental_lib_re_export) =
463+
self.get_experimental_lib_options(module_generator_options)?;
464+
462465
let result = match generate_context.requested_source_type {
463466
SourceType::JavaScript => {
464467
let exported_content = if parsed_asset_config.is_inline() {
@@ -520,6 +523,16 @@ impl ParserAndGenerator for AssetParserAndGenerator {
520523
}
521524
PublicPath::Auto => public_path.render(compilation, &filename),
522525
};
526+
527+
if experimental_lib_preserve_import || experimental_lib_re_export {
528+
let place_holder = "__webpack_auto_public_path__";
529+
dbg!(place_holder);
530+
serde_json::to_string(&format!("{place_holder}{original_filename}"));
531+
generate_context
532+
.data
533+
.insert(CodeGenerationPublicPathAutoMark(true));
534+
}
535+
523536
serde_json::to_string(&format!("{public_path}{original_filename}"))
524537
.map_err(|e| error!(e.to_string()))?
525538
} else {
@@ -554,65 +567,50 @@ impl ParserAndGenerator for AssetParserAndGenerator {
554567
unreachable!()
555568
};
556569

557-
let (experimental_lib_preserve_import, experimental_lib_re_export) =
558-
self.get_experimental_lib_options(module_generator_options)?;
559-
560570
if experimental_lib_preserve_import || experimental_lib_re_export {
561-
if let Some(ref mut scope) = generate_context.concatenation_scope {
562-
let is_module = compilation.options.output.module;
563-
if parsed_asset_config.is_resource() {
571+
let is_module = compilation.options.output.module;
572+
if is_module {
573+
if let Some(ref mut scope) = generate_context.concatenation_scope {
564574
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
565575
if experimental_lib_re_export {
566-
if is_module {
567-
return Ok(
568-
RawStringSource::from(format!(
569-
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};
570-
export default {NAMESPACE_OBJECT_EXPORT};"#
571-
))
572-
.boxed(),
573-
);
574-
} else {
575-
return Ok(
576-
RawStringSource::from(format!(
577-
r#"module.exports = require({NAMESPACE_OBJECT_EXPORT});"#
578-
))
579-
.boxed(),
580-
);
581-
}
576+
return Ok(
577+
RawStringSource::from(format!(
578+
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};
579+
export default {NAMESPACE_OBJECT_EXPORT}"#
580+
))
581+
.boxed(),
582+
);
582583
} else if experimental_lib_preserve_import {
583-
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
584-
585-
if is_module {
586-
return Ok(
587-
RawStringSource::from(format!(
588-
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};"#
589-
))
590-
.boxed(),
591-
);
592-
} else {
593-
let supports_const = compilation.options.output.environment.supports_const();
594-
let declaration_kind = if supports_const { "const" } else { "var" };
595-
596-
return Ok(
597-
RawStringSource::from(format!(
598-
r#"{declaration_kind} {NAMESPACE_OBJECT_EXPORT} = require({NAMESPACE_OBJECT_EXPORT});"#
599-
))
600-
.boxed(),
601-
);
602-
}
584+
return Ok(
585+
RawStringSource::from(format!(
586+
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};"#
587+
))
588+
.boxed(),
589+
);
603590
}
591+
}
592+
} else {
593+
if let Some(ref mut scope) = generate_context.concatenation_scope {
594+
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
595+
let supports_const = compilation.options.output.environment.supports_const();
596+
let declaration_kind = if supports_const { "const" } else { "var" };
597+
return Ok(
598+
RawStringSource::from(format!(
599+
r#"{declaration_kind} {NAMESPACE_OBJECT_EXPORT} = require({exported_content});"#
600+
))
601+
.boxed(),
602+
);
604603
} else {
605604
if experimental_lib_re_export {
606-
if is_module {
607-
return Ok(
608-
RawStringSource::from(format!(r#"export default {exported_content};"#)).boxed(),
609-
);
610-
} else {
611-
return Ok(
612-
RawStringSource::from(format!(r#"module.exports = {exported_content};"#))
613-
.boxed(),
614-
);
615-
}
605+
generate_context
606+
.runtime_requirements
607+
.insert(RuntimeGlobals::MODULE);
608+
return Ok(
609+
RawStringSource::from(format!(
610+
r#"module.exports = require({exported_content});"#
611+
))
612+
.boxed(),
613+
);
616614
}
617615
}
618616
}

crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use rspack_core::diagnostics::map_box_diagnostics_to_module_parse_diagnostics;
88
use rspack_core::rspack_sources::{BoxSource, ReplaceSource, Source, SourceExt};
99
use rspack_core::{
1010
render_init_fragments, AsyncDependenciesBlockIdentifier, BuildMetaExportsType, ChunkGraph,
11-
Compilation, DependenciesBlock, DependencyId, DependencyRange, GenerateContext, Module,
12-
ModuleGraph, ModuleType, ParseContext, ParseResult, ParserAndGenerator, SideEffectsBailoutItem,
13-
SourceType, TemplateContext, TemplateReplaceSource,
11+
CodeGenerationPublicPathAutoMark, Compilation, DependenciesBlock, DependencyId, DependencyRange,
12+
GenerateContext, Module, ModuleGraph, ModuleType, ParseContext, ParseResult, ParserAndGenerator,
13+
SideEffectsBailoutItem, SourceType, TemplateContext, TemplateReplaceSource,
1414
};
1515
use rspack_error::miette::Diagnostic;
1616
use rspack_error::{DiagnosticExt, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
@@ -271,6 +271,7 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {
271271
module: &dyn Module,
272272
generate_context: &mut GenerateContext,
273273
) -> Result<BoxSource> {
274+
dbg!(&generate_context.data);
274275
if matches!(
275276
generate_context.requested_source_type,
276277
SourceType::JavaScript

crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -676,10 +676,12 @@ impl ModuleConcatenationPlugin {
676676
}
677677
}
678678
}
679-
// module_graph
680-
// .module_identifier_to_module
681-
// .remove(&root_module_id);
682-
// compilation.chunk_graph.clear
679+
680+
// different from webpack
681+
// Rspack: if entry is an asset module, outputs a js chunk and a asset chunk
682+
// Webpack: if entry is an asset module, outputs an asset chunk
683+
// these lines of codes fix a bug: when asset module (NormalModule) is concatenated into ConcatenatedModule, the asset will be lost
684+
// because `chunk_graph.replace_module(&root_module_id, &new_module.id());` will remove the asset module from chunk, and I add this module back to fix this bug
683685
if is_root_module_asset_module {
684686
chunk_graph.replace_module(&root_module_id, &new_module.id());
685687
chunk_graph.add_module(root_module_id);

crates/rspack_plugin_javascript/src/runtime.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use rayon::prelude::*;
22
use rspack_core::chunk_graph_chunk::ChunkId;
3-
use rspack_core::rspack_sources::{BoxSource, ConcatSource, RawStringSource, SourceExt};
3+
use rspack_core::rspack_sources::{
4+
BoxSource, ConcatSource, RawStringSource, ReplaceSource, SourceExt,
5+
};
46
use rspack_core::{
5-
to_normal_comment, BoxModule, ChunkGraph, ChunkInitFragments, ChunkUkey, Compilation,
6-
RuntimeGlobals, SourceType,
7+
to_normal_comment, BoxModule, ChunkGraph, ChunkInitFragments, ChunkUkey,
8+
CodeGenerationPublicPathAutoMark, Compilation, RuntimeGlobals, SourceType,
79
};
810
use rspack_error::{error, Result};
911
use rspack_util::diff_mode::is_diff_mode;
@@ -75,6 +77,16 @@ pub fn render_module(
7577
let Some(origin_source) = code_gen_result.get(&SourceType::JavaScript) else {
7678
return Ok(None);
7779
};
80+
81+
let mut origin_source = origin_source;
82+
83+
if code_gen_result
84+
.data
85+
.get::<CodeGenerationPublicPathAutoMark>()
86+
.is_some()
87+
{
88+
let source = ReplaceSource::new(origin_source.clone());
89+
}
7890
let hooks = JsPlugin::get_compilation_hooks(compilation);
7991
let mut module_chunk_init_fragments = match code_gen_result.data.get::<ChunkInitFragments>() {
8092
Some(fragments) => fragments.clone(),

crates/rspack_plugin_library/src/modern_module_library_plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rspack_core::{
77
CodeGenerationExportsFinalNames, Compilation, CompilationFinishModules,
88
CompilationOptimizeChunkModules, CompilationParams, CompilerCompilation, CompilerOptions,
99
ConcatenatedModule, ConcatenatedModuleExportsDefinitions, DependenciesBlock, Dependency,
10-
DependencyId, LibraryOptions, ModuleGraph, ModuleIdentifier, Plugin, PluginContext, SourceType,
10+
DependencyId, LibraryOptions, ModuleGraph, ModuleIdentifier, Plugin, PluginContext,
1111
};
1212
use rspack_error::{error_bail, Result};
1313
use rspack_hash::RspackHash;

0 commit comments

Comments
 (0)