Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 50 additions & 33 deletions crates/rspack_core/src/runtime_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl RuntimeTemplate {
RuntimeCodeTemplate::new(
self.compiler_options.clone(),
self.render_mode.runtime_module_render_mode(),
self.dojang.clone(),
Some(self.dojang.clone()),
)
}

Expand All @@ -371,7 +371,7 @@ impl RuntimeTemplate {
RuntimeCodeTemplate::new(
self.compiler_options.clone(),
self.render_mode.chunk_render_mode(),
self.dojang.clone(),
None,
)
}
}
Expand Down Expand Up @@ -868,11 +868,17 @@ impl ModuleCodeTemplate {
}

pub fn render_runtime_scope(&self) -> String {
let render_mode = match self.runtime_globals_render_mode {
RuntimeGlobalsRenderMode::Webpack => RuntimeGlobalsRenderMode::Webpack,
_ => RuntimeGlobalsRenderMode::RspackContext,
};
get_runtime_globals_render_map(render_mode).render(&RuntimeGlobals::REQUIRE_SCOPE)
match self.runtime_globals_render_mode {
RuntimeGlobalsRenderMode::Webpack => {
WEBPACK_RUNTIME_GLOBALS.render(&RuntimeGlobals::REQUIRE_SCOPE)
}
RuntimeGlobalsRenderMode::RspackExport => {
self.runtime_globals.render(&RuntimeGlobals::REQUIRE)
}
RuntimeGlobalsRenderMode::RspackContext | RuntimeGlobalsRenderMode::RspackLexical => {
RSPACK_CONTEXT_RUNTIME_GLOBALS.render(&RuntimeGlobals::REQUIRE_SCOPE)
}
}
}

pub fn define_es_module_flag_statement(&mut self, exports_argument: ExportsArgument) -> String {
Expand Down Expand Up @@ -1823,14 +1829,22 @@ pub struct RuntimeCodeTemplate {
compiler_options: Arc<CompilerOptions>,
render_mode: RuntimeGlobalsRenderMode,
runtime_globals: Arc<RuntimeGlobalsRenderMap>,
dojang: Arc<Dojang>,
dojang: Option<Arc<Dojang>>,
}

impl Debug for RuntimeCodeTemplate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RuntimeCodeTemplate")
.field("render_mode", &self.render_mode)
.finish_non_exhaustive()
}
}

impl RuntimeCodeTemplate {
fn new(
compiler_options: Arc<CompilerOptions>,
render_mode: RuntimeGlobalsRenderMode,
dojang: Arc<Dojang>,
dojang: Option<Arc<Dojang>>,
) -> Self {
Self {
compiler_options,
Expand Down Expand Up @@ -1897,6 +1911,26 @@ impl RuntimeCodeTemplate {
"this".to_string()
}

pub fn basic_function(&self, args: &str, body: &str) -> String {
if self
.compiler_options
.output
.environment
.supports_arrow_function()
{
format!(
r#"({args}) => {{
{body}
}}"#
)
} else {
format!(
r#"function({args}) {{
{body}
}}"#
)
}
}
pub fn render(&self, key: &str, params: Option<serde_json::Value>) -> Result<String, Error> {
let mut render_params = Value::Object(Default::default());

Expand Down Expand Up @@ -1924,12 +1958,16 @@ impl RuntimeCodeTemplate {
}
}

if let Some((executer, file_content)) = self.dojang.templates.get(key) {
let dojang = self
.dojang
.as_ref()
.expect("chunk code templates cannot render runtime module templates");
if let Some((executer, file_content)) = dojang.templates.get(key) {
executer
.render(
&mut Context::new(render_params),
&self.dojang.templates,
&self.dojang.functions,
&dojang.templates,
&dojang.functions,
file_content,
#[cfg_attr(
dylint_lib = "rspack_collection_hasher",
Expand All @@ -1946,25 +1984,4 @@ impl RuntimeCodeTemplate {
Err(error!("Runtime module: Template {key} is not found"))
}
}

pub fn basic_function(&self, args: &str, body: &str) -> String {
if self
.compiler_options
.output
.environment
.supports_arrow_function()
{
format!(
r#"({args}) => {{
{body}
}}"#
)
} else {
format!(
r#"function({args}) {{
{body}
}}"#
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::too_many_arguments)]

use std::borrow::Cow;

use cow_utils::CowUtils;
Expand Down Expand Up @@ -85,9 +87,13 @@ async fn render_module_content(
chunk_ukey: &ChunkUkey,
module: &dyn Module,
render_source: &mut RenderSource,
runtime_requirements: &mut RuntimeGlobals,
_init_fragments: &mut ChunkInitFragments,
runtime_template: &RuntimeCodeTemplate,
) -> Result<()> {
if compilation.options.output.trusted_types.is_some() {
runtime_requirements.insert(RuntimeGlobals::CREATE_SCRIPT);
}
let origin_source = render_source.source.clone();
if let Some(cached_source) = self.cache.get(&origin_source) {
render_source.source = cached_source.value().clone();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::too_many_arguments)]

use std::{borrow::Cow, sync::Arc};

use derive_more::Debug;
Expand Down Expand Up @@ -95,9 +97,13 @@ async fn render_module_content(
chunk: &ChunkUkey,
module: &dyn Module,
render_source: &mut RenderSource,
runtime_requirements: &mut RuntimeGlobals,
_init_fragments: &mut ChunkInitFragments,
runtime_template: &RuntimeCodeTemplate,
) -> Result<()> {
if compilation.options.output.trusted_types.is_some() {
runtime_requirements.insert(RuntimeGlobals::CREATE_SCRIPT);
}
let output_options = &compilation.options.output;
let chunk = compilation
.build_chunk_graph_artifact
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_plugin_esm_library/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,7 @@ var {} = {{}};
let mut render_source = RenderSource {
source: js_source.clone(),
};
let mut runtime_requirements = codegen_res.runtime_requirements;

let mut chunk_init_fragments = vec![];
hooks
Expand All @@ -1510,6 +1511,7 @@ var {} = {{}};
.expect("should have module")
.as_ref(),
&mut render_source,
&mut runtime_requirements,
&mut chunk_init_fragments,
runtime_template,
)
Expand Down Expand Up @@ -1612,7 +1614,7 @@ var {} = {{}};
concate_info.has_ast = true;
concate_info.source = Some(ReplaceSource::new(render_source.source.clone()));
concate_info.internal_source = Some(render_source.source.clone());
concate_info.runtime_requirements = codegen_res.runtime_requirements;
concate_info.runtime_requirements = runtime_requirements;
concate_info.chunk_init_fragments = codegen_res
.data
.get::<ChunkInitFragments>()
Expand Down
1 change: 0 additions & 1 deletion crates/rspack_plugin_esm_library/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,6 @@ var {} = {{}};
} else {
final_source
};

Ok(Some(RenderSource {
source: final_source,
}))
Expand Down
5 changes: 4 additions & 1 deletion crates/rspack_plugin_javascript/src/plugin/api_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![allow(clippy::too_many_arguments)]

use rspack_core::{
ChunkInitFragments, ChunkUkey, Compilation, CompilationParams, CompilerCompilation,
InitFragmentExt, InitFragmentKey, InitFragmentStage, Module, NormalInitFragment, Plugin,
RuntimeCodeTemplate,
RuntimeCodeTemplate, RuntimeGlobals,
};
use rspack_error::Result;
use rspack_hook::{plugin, plugin_hook};
Expand Down Expand Up @@ -34,6 +36,7 @@ async fn render_module_content(
_chunk_ukey: &ChunkUkey,
module: &dyn Module,
_source: &mut RenderSource,
_runtime_requirements: &mut RuntimeGlobals,
init_fragments: &mut ChunkInitFragments,
_runtime_template: &RuntimeCodeTemplate,
) -> Result<()> {
Expand Down
6 changes: 4 additions & 2 deletions crates/rspack_plugin_javascript/src/plugin/drive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![allow(clippy::too_many_arguments)]

use rspack_core::{
AssetInfo, BoxModule, Chunk, ChunkInitFragments, ChunkUkey, Compilation, Module,
ModuleIdentifier, RuntimeCodeTemplate, rspack_sources::BoxSource,
ModuleIdentifier, RuntimeCodeTemplate, RuntimeGlobals, rspack_sources::BoxSource,
};
use rspack_hash::RspackHasher;
use rspack_hook::define_hook;
Expand All @@ -11,7 +13,7 @@ define_hook!(JavascriptModulesRenderChunk: Series(compilation: &Compilation, chu
define_hook!(JavascriptModulesRenderChunkContent: SeriesBail(compilation: &Compilation, chunk_ukey: &ChunkUkey, asset_info: &mut AssetInfo, runtime_template: &RuntimeCodeTemplate) -> RenderSource);
define_hook!(JavascriptModulesRender: Series(compilation: &Compilation, chunk_ukey: &ChunkUkey, source: &mut RenderSource, runtime_template: &RuntimeCodeTemplate));
define_hook!(JavascriptModulesRenderStartup: Series(compilation: &Compilation, chunk_ukey: &ChunkUkey, module: &ModuleIdentifier, source: &mut RenderSource, runtime_template: &RuntimeCodeTemplate));
define_hook!(JavascriptModulesRenderModuleContent: Series(compilation: &Compilation, chunk_ukey: &ChunkUkey,module: &dyn Module, source: &mut RenderSource, init_fragments: &mut ChunkInitFragments, runtime_template: &RuntimeCodeTemplate),tracing=false);
define_hook!(JavascriptModulesRenderModuleContent: Series(compilation: &Compilation, chunk_ukey: &ChunkUkey,module: &dyn Module, source: &mut RenderSource, runtime_requirements: &mut RuntimeGlobals, init_fragments: &mut ChunkInitFragments, runtime_template: &RuntimeCodeTemplate),tracing=false);
define_hook!(JavascriptModulesRenderModuleContainer: Series(compilation: &Compilation, chunk_ukey: &ChunkUkey,module: &dyn Module, source: &mut RenderSource, init_fragments: &mut ChunkInitFragments, runtime_template: &RuntimeCodeTemplate),tracing=false);
define_hook!(JavascriptModulesRenderModulePackage: Series(compilation: &Compilation, chunk_ukey: &ChunkUkey, module: &dyn Module, source: &mut RenderSource, init_fragments: &mut ChunkInitFragments, runtime_template: &RuntimeCodeTemplate),tracing=false);
define_hook!(JavascriptModulesChunkHash: Series(compilation: &Compilation, chunk_ukey: &ChunkUkey, hasher: &mut RspackHasher));
Expand Down
1 change: 0 additions & 1 deletion crates/rspack_plugin_javascript/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,6 @@ var {} = {{}};
{
rendered_module = source.clone();
};

chunk_init_fragments.extend(fragments);
chunk_init_fragments.extend(additional_fragments);
let inner_strict = !all_strict && m.build_info().strict;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,6 @@ impl JsPlugin {
{
rendered_module = source.clone();
};

chunk_init_fragments.extend(fragments);
chunk_init_fragments.extend(additional_fragments);
let inner_strict = !all_strict && m.build_info().strict;
Expand Down
7 changes: 5 additions & 2 deletions crates/rspack_plugin_javascript/src/plugin/url_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#![allow(clippy::too_many_arguments)]

use concat_string::concat_string;
use rspack_core::{
ChunkInitFragments, ChunkUkey, CodeGenerationDataFilename, Compilation, CompilationParams,
CompilerCompilation, DependencyId, JavascriptParserUrl, Module, ModuleType,
NormalModuleFactoryParser, ParserAndGenerator, ParserOptions, PathData, Plugin, PublicPath,
RuntimeCodeTemplate, RuntimeSpec, SourceType, URLStaticMode, get_js_chunk_filename_template,
get_undo_path,
RuntimeCodeTemplate, RuntimeGlobals, RuntimeSpec, SourceType, URLStaticMode,
get_js_chunk_filename_template, get_undo_path,
rspack_sources::{BoxSource, ReplaceSource, SourceExt},
};
use rspack_error::Result;
Expand Down Expand Up @@ -196,6 +198,7 @@ async fn render_module_content(
chunk_ukey: &ChunkUkey,
module: &dyn Module,
render_source: &mut RenderSource,
_runtime_requirements: &mut RuntimeGlobals,
_init_fragments: &mut ChunkInitFragments,
_runtime_template: &RuntimeCodeTemplate,
) -> Result<()> {
Expand Down
33 changes: 15 additions & 18 deletions crates/rspack_plugin_javascript/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rayon::prelude::*;
use rspack_core::{
ChunkGraph, ChunkInitFragments, ChunkKind, ChunkUkey, CodeGenerationPublicPathAutoReplace,
Compilation, Module, RuntimeCodeTemplate, RuntimeGlobals, RuntimeModuleGenerateContext,
SourceType,
Compilation, Module, RuntimeCodeTemplate, RuntimeGlobals, RuntimeGlobalsRenderMode,
RuntimeModuleGenerateContext, SourceType,
chunk_graph_chunk::ChunkIdSet,
get_undo_path, render_runtime_module_source,
rspack_sources::{
Expand Down Expand Up @@ -136,6 +136,7 @@ pub async fn render_module(
Some(fragments) => fragments.clone(),
None => ChunkInitFragments::default(),
};
let mut render_runtime_requirements = code_gen_result.runtime_requirements;

let mut render_source = if code_gen_result
.data
Expand Down Expand Up @@ -191,6 +192,7 @@ pub async fn render_module(
chunk_ukey,
module,
&mut render_source,
&mut render_runtime_requirements,
&mut module_chunk_init_fragments,
runtime_template,
)
Expand All @@ -214,21 +216,16 @@ pub async fn render_module(

let need_module = runtime_requirements.is_some_and(|r| r.contains(RuntimeGlobals::MODULE));
let need_exports = runtime_requirements.is_some_and(|r| r.contains(RuntimeGlobals::EXPORTS));
let need_require = runtime_requirements.is_some_and(|r| {
r.contains(RuntimeGlobals::REQUIRE)
|| r.contains(RuntimeGlobals::REQUIRE_SCOPE)
|| (compilation.options.experiments.runtime_mode == RuntimeMode::Rspack
&& !r.renderable_require_scope().is_empty())
});
let need_require = if need_require {
render_source
.source
.source()
.into_string_lossy()
.contains(&runtime_template.render_runtime_argument())
} else {
need_require
};
let need_require = runtime_template.render_mode() != RuntimeGlobalsRenderMode::RspackExport
&& (render_runtime_requirements.contains(RuntimeGlobals::REQUIRE)
|| render_runtime_requirements.contains(RuntimeGlobals::REQUIRE_SCOPE)
|| !render_runtime_requirements
.renderable_require_scope()
.is_empty());
let module_runtime_scope = compilation
.runtime_template
.create_module_code_template()
.render_runtime_scope();

let mut args = Vec::new();
if need_module || need_exports || need_require {
Expand All @@ -250,7 +247,7 @@ pub async fn render_module(
});
}
if need_require {
args.push(runtime_template.render_runtime_argument());
args.push(module_runtime_scope);
}

let mut container_sources = ConcatSource::default();
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_sri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use derive_more::Debug;
use futures::future::BoxFuture;
use rspack_core::{CrossOriginLoading, ModuleCodeTemplate};
use rspack_core::CrossOriginLoading;
use rspack_error::Result;
use rspack_fs::WritableFileSystem;
use rspack_paths::Utf8PathBuf;
Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct SRICompilationContext {
pub fs: ArcFs,
pub output_path: Utf8PathBuf,
pub cross_origin_loading: CrossOriginLoading,
pub runtime_template: ModuleCodeTemplate,
pub runtime_require_name: String,
}

pub struct IntegrityCallbackData {
Expand Down
Loading
Loading