Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter committed Jan 13, 2025
1 parent 15b1a19 commit ad92af5
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 69 deletions.
3 changes: 3 additions & 0 deletions crates/rspack_core/src/artifacts/code_generation_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ impl CodeGenerationDataUrl {
}
}

#[derive(Clone, Debug)]
pub struct CodeGenerationPublicPathAutoMark(pub bool);

#[derive(Clone, Debug)]
pub struct CodeGenerationDataFilename {
filename: String,
Expand Down
2 changes: 0 additions & 2 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,6 @@ impl Module for ConcatenatedModule {
};
let runtime = runtime.as_deref();
let context = compilation.options.context.clone();
dbg!(&context);

let (modules_with_info, module_to_info_map) =
self.get_modules_with_info(&compilation.get_module_graph(), runtime);
Expand Down Expand Up @@ -956,7 +955,6 @@ impl Module for ConcatenatedModule {
continue;
}
let used_name = export_info.get_used_name(&module_graph, None, runtime);
dbg!(&used_name);

let Some(used_name) = used_name else {
unused_exports.insert(name);
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/init_fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ pub fn render_init_fragments<C: InitFragmentRenderContext>(
for (key, fragments) in keyed_fragments {
let f = key.merge_fragments(fragments);
let contents = f.contents(context)?;
dbg!(&contents.start, &contents.end);
concat_source.add(RawStringSource::from(contents.start));
if let Some(end_content) = contents.end {
end_contents.push(RawStringSource::from(end_content))
Expand Down
110 changes: 54 additions & 56 deletions crates/rspack_plugin_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use rspack_core::{
rspack_sources::{BoxSource, CachedSource, RawBufferSource, RawStringSource, SourceExt},
AssetGeneratorDataUrl, AssetGeneratorDataUrlFnCtx, AssetInfo, AssetParserDataUrl,
BuildMetaDefaultObject, BuildMetaExportsType, ChunkGraph, ChunkUkey, CodeGenerationDataAssetInfo,
CodeGenerationDataFilename, CodeGenerationDataUrl, Compilation, CompilationRenderManifest,
CompilerOptions, Filename, GenerateContext, GeneratorOptions, LocalFilenameFn, Module,
ModuleGraph, NormalModule, ParseContext, ParserAndGenerator, PathData, Plugin, PublicPath,
RenderManifestEntry, ResourceData, RuntimeGlobals, RuntimeSpec, SourceType,
CodeGenerationDataFilename, CodeGenerationDataUrl, CodeGenerationPublicPathAutoMark, Compilation,
CompilationRenderManifest, CompilerOptions, Filename, GenerateContext, GeneratorOptions,
LocalFilenameFn, Module, ModuleGraph, NormalModule, ParseContext, ParserAndGenerator, PathData,
Plugin, PublicPath, RenderManifestEntry, ResourceData, RuntimeGlobals, RuntimeSpec, SourceType,
NAMESPACE_OBJECT_EXPORT,
};
use rspack_error::{error, Diagnostic, IntoTWithDiagnosticArray, Result};
Expand Down Expand Up @@ -459,6 +459,9 @@ impl ParserAndGenerator for AssetParserAndGenerator {
.expect("module should be a NormalModule in AssetParserAndGenerator");
let module_generator_options = normal_module.get_generator_options();

let (experimental_lib_preserve_import, experimental_lib_re_export) =
self.get_experimental_lib_options(module_generator_options)?;

let result = match generate_context.requested_source_type {
SourceType::JavaScript => {
let exported_content = if parsed_asset_config.is_inline() {
Expand Down Expand Up @@ -520,6 +523,16 @@ impl ParserAndGenerator for AssetParserAndGenerator {
}
PublicPath::Auto => public_path.render(compilation, &filename),
};

if experimental_lib_preserve_import || experimental_lib_re_export {
let place_holder = "__webpack_auto_public_path__";
dbg!(place_holder);
serde_json::to_string(&format!("{place_holder}{original_filename}"));
generate_context
.data
.insert(CodeGenerationPublicPathAutoMark(true));
}

serde_json::to_string(&format!("{public_path}{original_filename}"))
.map_err(|e| error!(e.to_string()))?
} else {
Expand Down Expand Up @@ -554,65 +567,50 @@ impl ParserAndGenerator for AssetParserAndGenerator {
unreachable!()
};

let (experimental_lib_preserve_import, experimental_lib_re_export) =
self.get_experimental_lib_options(module_generator_options)?;

if experimental_lib_preserve_import || experimental_lib_re_export {
if let Some(ref mut scope) = generate_context.concatenation_scope {
let is_module = compilation.options.output.module;
if parsed_asset_config.is_resource() {
let is_module = compilation.options.output.module;
if is_module {
if let Some(ref mut scope) = generate_context.concatenation_scope {
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
if experimental_lib_re_export {
if is_module {
return Ok(
RawStringSource::from(format!(
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};
export default {NAMESPACE_OBJECT_EXPORT};"#
))
.boxed(),
);
} else {
return Ok(
RawStringSource::from(format!(
r#"module.exports = require({NAMESPACE_OBJECT_EXPORT});"#
))
.boxed(),
);
}
return Ok(
RawStringSource::from(format!(
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};
export default {NAMESPACE_OBJECT_EXPORT}"#
))
.boxed(),
);
} else if experimental_lib_preserve_import {
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);

if is_module {
return Ok(
RawStringSource::from(format!(
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};"#
))
.boxed(),
);
} else {
let supports_const = compilation.options.output.environment.supports_const();
let declaration_kind = if supports_const { "const" } else { "var" };

return Ok(
RawStringSource::from(format!(
r#"{declaration_kind} {NAMESPACE_OBJECT_EXPORT} = require({NAMESPACE_OBJECT_EXPORT});"#
))
.boxed(),
);
}
return Ok(
RawStringSource::from(format!(
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};"#
))
.boxed(),
);
}
}
} else {
if let Some(ref mut scope) = generate_context.concatenation_scope {
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
let supports_const = compilation.options.output.environment.supports_const();
let declaration_kind = if supports_const { "const" } else { "var" };
return Ok(
RawStringSource::from(format!(
r#"{declaration_kind} {NAMESPACE_OBJECT_EXPORT} = require({exported_content});"#
))
.boxed(),
);
} else {
if experimental_lib_re_export {
if is_module {
return Ok(
RawStringSource::from(format!(r#"export default {exported_content};"#)).boxed(),
);
} else {
return Ok(
RawStringSource::from(format!(r#"module.exports = {exported_content};"#))
.boxed(),
);
}
generate_context
.runtime_requirements
.insert(RuntimeGlobals::MODULE);
return Ok(
RawStringSource::from(format!(
r#"module.exports = require({exported_content});"#
))
.boxed(),
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use rspack_core::diagnostics::map_box_diagnostics_to_module_parse_diagnostics;
use rspack_core::rspack_sources::{BoxSource, ReplaceSource, Source, SourceExt};
use rspack_core::{
render_init_fragments, AsyncDependenciesBlockIdentifier, BuildMetaExportsType, ChunkGraph,
Compilation, DependenciesBlock, DependencyId, DependencyRange, GenerateContext, Module,
ModuleGraph, ModuleType, ParseContext, ParseResult, ParserAndGenerator, SideEffectsBailoutItem,
SourceType, TemplateContext, TemplateReplaceSource,
CodeGenerationPublicPathAutoMark, Compilation, DependenciesBlock, DependencyId, DependencyRange,
GenerateContext, Module, ModuleGraph, ModuleType, ParseContext, ParseResult, ParserAndGenerator,
SideEffectsBailoutItem, SourceType, TemplateContext, TemplateReplaceSource,
};
use rspack_error::miette::Diagnostic;
use rspack_error::{DiagnosticExt, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
Expand Down Expand Up @@ -271,6 +271,7 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {
module: &dyn Module,
generate_context: &mut GenerateContext,
) -> Result<BoxSource> {
dbg!(&generate_context.data);
if matches!(
generate_context.requested_source_type,
SourceType::JavaScript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,12 @@ impl ModuleConcatenationPlugin {
}
}
}
// module_graph
// .module_identifier_to_module
// .remove(&root_module_id);
// compilation.chunk_graph.clear

// different from webpack
// Rspack: if entry is an asset module, outputs a js chunk and a asset chunk
// Webpack: if entry is an asset module, outputs an asset chunk
// these lines of codes fix a bug: when asset module (NormalModule) is concatenated into ConcatenatedModule, the asset will be lost
// 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
if is_root_module_asset_module {
chunk_graph.replace_module(&root_module_id, &new_module.id());
chunk_graph.add_module(root_module_id);
Expand Down
18 changes: 15 additions & 3 deletions crates/rspack_plugin_javascript/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use rayon::prelude::*;
use rspack_core::chunk_graph_chunk::ChunkId;
use rspack_core::rspack_sources::{BoxSource, ConcatSource, RawStringSource, SourceExt};
use rspack_core::rspack_sources::{
BoxSource, ConcatSource, RawStringSource, ReplaceSource, SourceExt,
};
use rspack_core::{
to_normal_comment, BoxModule, ChunkGraph, ChunkInitFragments, ChunkUkey, Compilation,
RuntimeGlobals, SourceType,
to_normal_comment, BoxModule, ChunkGraph, ChunkInitFragments, ChunkUkey,
CodeGenerationPublicPathAutoMark, Compilation, RuntimeGlobals, SourceType,
};
use rspack_error::{error, Result};
use rspack_util::diff_mode::is_diff_mode;
Expand Down Expand Up @@ -75,6 +77,16 @@ pub fn render_module(
let Some(origin_source) = code_gen_result.get(&SourceType::JavaScript) else {
return Ok(None);
};

let mut origin_source = origin_source;

if code_gen_result
.data
.get::<CodeGenerationPublicPathAutoMark>()
.is_some()
{
let source = ReplaceSource::new(origin_source.clone());
}
let hooks = JsPlugin::get_compilation_hooks(compilation);
let mut module_chunk_init_fragments = match code_gen_result.data.get::<ChunkInitFragments>() {
Some(fragments) => fragments.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rspack_core::{
CodeGenerationExportsFinalNames, Compilation, CompilationFinishModules,
CompilationOptimizeChunkModules, CompilationParams, CompilerCompilation, CompilerOptions,
ConcatenatedModule, ConcatenatedModuleExportsDefinitions, DependenciesBlock, Dependency,
DependencyId, LibraryOptions, ModuleGraph, ModuleIdentifier, Plugin, PluginContext, SourceType,
DependencyId, LibraryOptions, ModuleGraph, ModuleIdentifier, Plugin, PluginContext,
};
use rspack_error::{error_bail, Result};
use rspack_hash::RspackHash;
Expand Down

0 comments on commit ad92af5

Please sign in to comment.