Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inline entry modules #6554

Merged
merged 8 commits into from
May 18, 2024
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
5 changes: 5 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,10 @@ export interface RawEntryPluginOptions {
options: RawEntryOptions
}

export interface RawEnvironment {
arrowFunction?: boolean
}

export interface RawEvalDevToolModulePluginOptions {
namespace?: string
moduleFilenameTemplate?: string | ((info: RawModuleFilenameTemplateFnCtx) => string)
Expand Down Expand Up @@ -1150,6 +1154,7 @@ export interface RawOutputOptions {
workerWasmLoading: string
workerPublicPath: string
scriptType: "module" | "text/javascript" | "false"
environment: RawEnvironment
}

export interface RawParserOptions {
Expand Down
20 changes: 18 additions & 2 deletions crates/rspack_binding_options/src/options/raw_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use napi::Either;
use napi_derive::napi;
use rspack_binding_values::JsFilename;
use rspack_core::{
CrossOriginLoading, LibraryCustomUmdObject, LibraryName, LibraryNonUmdObject, LibraryOptions,
PathInfo,
CrossOriginLoading, Environment, LibraryCustomUmdObject, LibraryName, LibraryNonUmdObject,
LibraryOptions, PathInfo,
};
use rspack_core::{LibraryAuxiliaryComment, OutputOptions, TrustedTypes};

Expand Down Expand Up @@ -141,6 +141,20 @@ impl From<RawCrossOriginLoading> for CrossOriginLoading {
}
}

#[derive(Debug, Clone)]
#[napi(object)]
pub struct RawEnvironment {
pub arrow_function: Option<bool>,
}

impl From<RawEnvironment> for Environment {
fn from(value: RawEnvironment) -> Self {
Self {
arrow_function: value.arrow_function,
}
}
}

#[derive(Debug)]
#[napi(object, object_to_js = false)]
pub struct RawOutputOptions {
Expand Down Expand Up @@ -184,6 +198,7 @@ pub struct RawOutputOptions {
pub worker_public_path: String,
#[napi(ts_type = r#""module" | "text/javascript" | "false""#)]
pub script_type: String,
pub environment: RawEnvironment,
}

impl TryFrom<RawOutputOptions> for OutputOptions {
Expand Down Expand Up @@ -232,6 +247,7 @@ impl TryFrom<RawOutputOptions> for OutputOptions {
worker_wasm_loading: value.worker_wasm_loading.as_str().into(),
worker_public_path: value.worker_public_path,
script_type: value.script_type,
environment: value.environment.into(),
})
}
}
17 changes: 16 additions & 1 deletion crates/rspack_core/src/code_generation_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anymap::CloneAny;
use rspack_hash::{HashDigest, HashFunction, HashSalt, RspackHash, RspackHashDigest};
use rspack_identifier::IdentifierMap;
use rspack_sources::BoxSource;
use rustc_hash::FxHashMap as HashMap;
use rustc_hash::{FxHashMap as HashMap, FxHashSet};
use serde::Serialize;

use crate::{
Expand Down Expand Up @@ -68,6 +68,21 @@ impl CodeGenerationDataAssetInfo {
}
}

#[derive(Clone, Debug)]
pub struct CodeGenerationDataTopLevelDeclarations {
inner: FxHashSet<String>,
}

impl CodeGenerationDataTopLevelDeclarations {
pub fn new(inner: FxHashSet<String>) -> Self {
Self { inner }
}

pub fn inner(&self) -> &FxHashSet<String> {
&self.inner
}
}

#[derive(Debug, Default, Clone)]
pub struct CodeGenerationData {
inner: anymap::Map<dyn CloneAny + Send + Sync>,
Expand Down
41 changes: 33 additions & 8 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ use crate::{
reserved_names::RESERVED_NAMES, returning_function, runtime_condition_expression,
subtract_runtime_condition, AsyncDependenciesBlockIdentifier, BoxDependency, BuildContext,
BuildInfo, BuildMeta, BuildMetaDefaultObject, BuildMetaExportsType, BuildResult,
ChunkInitFragments, CodeGenerationResult, Compilation, ConcatenatedModuleIdent,
ConcatenationScope, ConnectionId, ConnectionState, Context, DependenciesBlock, DependencyId,
DependencyTemplate, ErrorSpan, ExportInfoId, ExportInfoProvided, ExportsArgument, ExportsType,
FactoryMeta, IdentCollector, LibIdentOptions, Module, ModuleDependency, ModuleGraph,
ModuleGraphConnection, ModuleIdentifier, ModuleType, Resolve, RuntimeCondition, RuntimeGlobals,
RuntimeSpec, SourceType, SpanExt, Template, UsageState, UsedName, DEFAULT_EXPORT,
NAMESPACE_OBJECT_EXPORT,
ChunkInitFragments, CodeGenerationDataTopLevelDeclarations, CodeGenerationResult, Compilation,
ConcatenatedModuleIdent, ConcatenationScope, ConnectionId, ConnectionState, Context,
DependenciesBlock, DependencyId, DependencyTemplate, ErrorSpan, ExportInfoId, ExportInfoProvided,
ExportsArgument, ExportsType, FactoryMeta, IdentCollector, LibIdentOptions, Module,
ModuleDependency, ModuleGraph, ModuleGraphConnection, ModuleIdentifier, ModuleType, Resolve,
RuntimeCondition, RuntimeGlobals, RuntimeSpec, SourceType, SpanExt, Template, UsageState,
UsedName, DEFAULT_EXPORT, NAMESPACE_OBJECT_EXPORT,
};

#[derive(Debug)]
Expand Down Expand Up @@ -550,6 +550,7 @@ impl Module for ConcatenatedModule {
all_star_exports: Default::default(),
need_create_require: Default::default(),
json_data: Default::default(),
top_level_declarations: Some(Default::default()),
module_concatenation_bailout: Default::default(),
};
self.clear_diagnostics();
Expand Down Expand Up @@ -599,6 +600,17 @@ impl Module for ConcatenatedModule {
}
// release guard ASAP
drop(diagnostics_guard);

// populate topLevelDeclarations
if let Some(module_build_info) = module.build_info() {
if let Some(decls) = &module_build_info.top_level_declarations
&& let Some(top_level_declarations) = &mut build_info.top_level_declarations
{
top_level_declarations.extend(decls.iter().cloned());
} else {
build_info.top_level_declarations = None;
}
}
}
self.set_build_info(build_info);
// return a dummy result is enough, since we don't build the ConcatenatedModule in make phase
Expand Down Expand Up @@ -650,6 +662,7 @@ impl Module for ConcatenatedModule {
}

let mut all_used_names = HashSet::from_iter(RESERVED_NAMES.iter().map(|item| item.to_string()));
let mut top_level_declarations: HashSet<String> = HashSet::default();

for module in modules_with_info.iter() {
let ModuleInfoOrReference::Concatenated(m) = module else {
Expand Down Expand Up @@ -727,6 +740,7 @@ impl Module for ConcatenatedModule {
// dbg!(&name, &new_name);
all_used_names.insert(new_name.clone());
info.internal_names.insert(name.clone(), new_name.clone());
top_level_declarations.insert(new_name.as_str().into());

// Update source
let source = info.source.as_mut().expect("should have source");
Expand All @@ -746,6 +760,7 @@ impl Module for ConcatenatedModule {
// Handle the case when the name is not already used
all_used_names.insert(name.to_string());
info.internal_names.insert(name.clone(), name.to_string());
top_level_declarations.insert(name.to_string());
}
}

Expand All @@ -764,7 +779,8 @@ impl Module for ConcatenatedModule {

if let Some(namespace_object_name) = namespace_object_name {
all_used_names.insert(namespace_object_name.clone());
info.namespace_object_name = Some(namespace_object_name);
info.namespace_object_name = Some(namespace_object_name.clone());
top_level_declarations.insert(namespace_object_name);
}
// dbg!(info.module, &info.internal_names);
}
Expand All @@ -774,6 +790,7 @@ impl Module for ConcatenatedModule {
let external_name = Self::find_new_name("", &all_used_names, None, &readable_identifier);
all_used_names.insert(external_name.clone());
info.name = Some(external_name.as_str().into());
top_level_declarations.insert(external_name.as_str().into());
}
}
// Handle additional logic based on module build meta
Expand All @@ -786,6 +803,7 @@ impl Module for ConcatenatedModule {
);
all_used_names.insert(external_name_interop.as_str().into());
info.set_interop_namespace_object_name(Some(external_name_interop.as_str().into()));
top_level_declarations.insert(external_name_interop.as_str().into());
}

if exports_type == Some(BuildMetaExportsType::Default)
Expand All @@ -799,6 +817,7 @@ impl Module for ConcatenatedModule {
);
all_used_names.insert(external_name_interop.as_str().into());
info.set_interop_namespace_object2_name(Some(external_name_interop.as_str().into()));
top_level_declarations.insert(external_name_interop.as_str().into());
}

if matches!(
Expand All @@ -809,6 +828,7 @@ impl Module for ConcatenatedModule {
Self::find_new_name("default", &all_used_names, None, &readable_identifier);
all_used_names.insert(external_name_interop.clone());
info.set_interop_default_access_name(Some(external_name_interop.as_str().into()));
top_level_declarations.insert(external_name_interop.as_str().into());
}
}

Expand Down Expand Up @@ -1253,6 +1273,11 @@ impl Module for ConcatenatedModule {
code_generation_result.add(SourceType::JavaScript, CachedSource::new(result).boxed());
code_generation_result.chunk_init_fragments = chunk_init_fragments;
code_generation_result.runtime_requirements = runtime_requirements;
code_generation_result
.data
.insert(CodeGenerationDataTopLevelDeclarations::new(
top_level_declarations,
));
Ok(code_generation_result)
}

Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rspack_macros::impl_source_map_config;
use rspack_util::source_map::SourceMapKind;
use rustc_hash::FxHashMap as HashMap;
use rustc_hash::{FxHashMap as HashMap, FxHashSet};
use serde::Serialize;

use crate::{
Expand Down Expand Up @@ -389,6 +389,7 @@ impl Module for ExternalModule {

let build_info = BuildInfo {
hash: Some(hasher.digest(&build_context.compiler_options.output.hash_digest)),
top_level_declarations: Some(FxHashSet::default()),
..Default::default()
};

Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct BuildInfo {
pub all_star_exports: Vec<DependencyId>,
pub need_create_require: bool,
pub json_data: Option<JsonValue>,
pub top_level_declarations: Option<HashSet<String>>,
pub module_concatenation_bailout: Option<String>,
}

Expand All @@ -69,6 +70,7 @@ impl Default for BuildInfo {
all_star_exports: Vec::default(),
need_create_require: false,
json_data: None,
top_level_declarations: None,
module_concatenation_bailout: None,
}
}
Expand Down
12 changes: 12 additions & 0 deletions crates/rspack_core/src/options/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct OutputOptions {
pub worker_wasm_loading: WasmLoading,
pub worker_public_path: String,
pub script_type: String,
pub environment: Environment,
}

impl From<&OutputOptions> for RspackHash {
Expand Down Expand Up @@ -380,3 +381,14 @@ pub struct LibraryCustomUmdObject {
pub commonjs: Option<String>,
pub root: Option<Vec<String>>,
}

#[derive(Debug)]
pub struct Environment {
pub arrow_function: Option<bool>,
}

impl Environment {
pub fn supports_arrow_function(&self) -> bool {
self.arrow_function.unwrap_or_default()
}
}
2 changes: 1 addition & 1 deletion crates/rspack_core/src/runtime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait RuntimeModule: Module + CustomSourceRuntimeModule {
}
// if wrap iife
fn should_isolate(&self) -> bool {
false
true
}

fn generate_with_custom(
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_plugin_devtool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,10 @@ impl JavascriptModulesPluginPlugin for EvalSourceMapDevToolJavascriptModulesPlug
EVAL_SOURCE_MAP_DEV_TOOL_PLUGIN_NAME.hash(&mut args.hasher);
Ok(())
}

fn inline_in_runtime_bailout(&self) -> Option<String> {
Some("the eval-source-map devtool is used.".to_string())
}
}

const EVAL_SOURCE_MAP_DEV_TOOL_PLUGIN_NAME: &str = "rspack.EvalSourceMapDevToolPlugin";
Expand Down Expand Up @@ -1106,6 +1110,10 @@ impl JavascriptModulesPluginPlugin for EvalDevToolModuleJavascriptModulesPluginP
EVAL_DEV_TOOL_MODULE_PLUGIN_NAME.hash(&mut args.hasher);
Ok(())
}

fn inline_in_runtime_bailout(&self) -> Option<String> {
Some("the eval devtool is used.".to_string())
}
}

const EVAL_DEV_TOOL_MODULE_PLUGIN_NAME: &str = "rspack.EvalDevToolModulePlugin";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ impl Dependency for CommonJsSelfReferenceDependency {
fn dependency_type(&self) -> &DependencyType {
&DependencyType::CjsSelfReference
}

fn resource_identifier(&self) -> Option<&str> {
Some("self")
}
}

impl ModuleDependency for CommonJsSelfReferenceDependency {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,7 @@ impl JavascriptParserPlugin for CommonJsExportsParserPlugin {
}

if remaining.is_empty() {
// exports = {};
// module.exports = {};
// this = {};
parser.bailout();
parser.walk_expression(&assign_expr.right);
return Some(true);
return None;
}

parser.enable();
Expand Down
11 changes: 11 additions & 0 deletions crates/rspack_plugin_javascript/src/parser_plugin/drive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
None
}

fn finish(&self, parser: &mut JavascriptParser) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.finish(parser);
// `SyncBailHook`
if res.is_some() {
return res;
}
}
None
}

fn pre_module_declaration(
&self,
parser: &mut JavascriptParser,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use rustc_hash::FxHashSet;

use super::JavascriptParserPlugin;
use crate::visitors::JavascriptParser;

pub struct JavascriptMetaInfoPlugin;

impl JavascriptParserPlugin for JavascriptMetaInfoPlugin {
fn finish(&self, parser: &mut JavascriptParser) -> Option<bool> {
if parser.build_info.top_level_declarations.is_none() {
parser.build_info.top_level_declarations = Some(FxHashSet::default());
}
let variables: Vec<_> = parser
.get_all_variables_from_current_scope()
.map(|(name, _)| name.to_string())
.collect();
for name in variables {
if parser.get_free_info_from_variable(&name).is_none() {
parser
.build_info
.top_level_declarations
.as_mut()
.expect("must have value")
.insert(name);
}
}
None
}
}
2 changes: 2 additions & 0 deletions crates/rspack_plugin_javascript/src/parser_plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod import_meta_context_dependency_parser_plugin;
mod import_meta_plugin;
mod import_parser_plugin;
mod initialize_evaluating;
mod javascript_meta_info_plugin;
mod node_stuff_plugin;
mod provide;
mod require_context_dependency_parser_plugin;
Expand Down Expand Up @@ -43,6 +44,7 @@ pub(crate) use self::import_meta_context_dependency_parser_plugin::ImportMetaCon
pub(crate) use self::import_meta_plugin::ImportMetaPlugin;
pub(crate) use self::import_parser_plugin::ImportParserPlugin;
pub(crate) use self::initialize_evaluating::InitializeEvaluating;
pub(crate) use self::javascript_meta_info_plugin::JavascriptMetaInfoPlugin;
pub(crate) use self::node_stuff_plugin::NodeStuffPlugin;
pub(crate) use self::provide::ProviderPlugin;
pub(crate) use self::r#const::{is_logic_op, ConstPlugin};
Expand Down
Loading
Loading