Skip to content

Commit

Permalink
fix treeshaking
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Nov 21, 2023
1 parent 777d18d commit 7bc9c0a
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 23 deletions.
13 changes: 5 additions & 8 deletions crates/rspack_core/src/build_chunk_graph/code_splitter.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::sync::Arc;

use anyhow::anyhow;
use rspack_error::{internal_error, Result};
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};

use super::remove_parent_modules::RemoveParentModulesContext;
use crate::{
AsyncDependenciesBlockIdentifier, BoxDependency, ChunkGroup, ChunkGroupInfo, ChunkGroupKind,
ChunkGroupOptions, ChunkGroupUkey, ChunkLoading, ChunkUkey, Compilation, DependenciesBlock,
Logger, ModuleGraphConnection, ModuleIdentifier, RuntimeSpec, IS_NEW_TREESHAKING,
get_entry_runtime, AsyncDependenciesBlockIdentifier, BoxDependency, ChunkGroup, ChunkGroupInfo,
ChunkGroupKind, ChunkGroupOptions, ChunkGroupUkey, ChunkLoading, ChunkUkey, Compilation,
DependenciesBlock, Logger, ModuleGraphConnection, ModuleIdentifier, RuntimeSpec,
IS_NEW_TREESHAKING,
};

pub(super) struct CodeSplitter<'me> {
Expand Down Expand Up @@ -83,9 +82,7 @@ impl<'me> CodeSplitter<'me> {
let mut entrypoint = ChunkGroup::new(
ChunkGroupKind::new_entrypoint(true, Box::new(options.clone())),
ChunkGroupInfo {
runtime: HashSet::from_iter([Arc::from(
options.runtime.clone().unwrap_or_else(|| name.to_string()),
)]),
runtime: get_entry_runtime(name, options),
chunk_loading: !matches!(
options
.chunk_loading
Expand Down
23 changes: 15 additions & 8 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ use crate::{
ChunkGraph, ChunkGroupByUkey, ChunkGroupUkey, ChunkHashArgs, ChunkKind, ChunkUkey, CleanQueue,
CleanTask, CleanTaskResult, CodeGenerationResult, CodeGenerationResults, CompilationLogger,
CompilationLogging, CompilerOptions, ContentHashArgs, ContextDependency, DependencyId,
DependencyParents, Entry, EntryData, EntryOptions, Entrypoint, FactorizeQueue, FactorizeTask,
FactorizeTaskResult, Filename, Logger, Module, ModuleGraph, ModuleIdentifier, ModuleProfile,
ModuleType, PathData, ProcessAssetsArgs, ProcessDependenciesQueue, ProcessDependenciesResult,
ProcessDependenciesTask, RenderManifestArgs, Resolve, ResolverFactory, RuntimeGlobals,
RuntimeModule, RuntimeSpec, SharedPluginDriver, SourceType, Stats, TaskResult, WorkerTask,
DependencyParents, DependencyType, Entry, EntryData, EntryOptions, Entrypoint, FactorizeQueue,
FactorizeTask, FactorizeTaskResult, Filename, Logger, Module, ModuleGraph, ModuleIdentifier,
ModuleProfile, ModuleType, PathData, ProcessAssetsArgs, ProcessDependenciesQueue,
ProcessDependenciesResult, ProcessDependenciesTask, RenderManifestArgs, Resolve, ResolverFactory,
RuntimeGlobals, RuntimeModule, RuntimeSpec, SharedPluginDriver, SourceType, Stats, TaskResult,
WorkerTask,
};
use crate::{tree_shaking::visitor::OptimizeAnalyzeResult, Context};

Expand Down Expand Up @@ -941,11 +942,13 @@ impl Compilation {
if self.options.builtins.tree_shaking.enable() {
self.bailout_module_identifiers = self
.module_graph
.modules()
.dependencies()
.values()
.par_bridge()
.filter_map(|module| {
if module.as_context_module().is_some() {
.filter_map(|dep| {
if dep.as_context_dependency().is_some()
&& let Some(module) = self.module_graph.get_module(dep.id())
{
let mut values = vec![(module.identifier(), BailoutFlag::CONTEXT_MODULE)];
if let Some(dependencies) = self
.module_graph
Expand All @@ -962,6 +965,10 @@ impl Compilation {
}

Some(values)
} else if matches!(dep.dependency_type(), DependencyType::ContainerExposed)
&& let Some(module) = self.module_graph.get_module(dep.id())
{
Some(vec![(module.identifier(), BailoutFlag::CONTAINER_EXPOSED)])
} else {
None
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/module_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ impl ModuleGraph {
self.blocks.get(block_id)
}

pub fn dependencies(&self) -> &HashMap<DependencyId, BoxDependency> {
&self.dependencies
}

pub fn add_dependency(&mut self, dependency: BoxDependency) {
self.dependencies.insert(*dependency.id(), dependency);
}
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/tree_shaking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ bitflags::bitflags! {
const COMMONJS_EXPORTS = 1 << 2;
const DYNAMIC_IMPORT = 1 << 3;
const CONTEXT_MODULE = 1 << 4;
const CONTAINER_EXPOSED = 1 << 5;
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use rustc_hash::FxHashMap as HashMap;
mod identifier;
pub use identifier::*;

mod runtime;
pub use runtime::*;

mod property_name;
pub use property_name::*;

Expand Down
9 changes: 9 additions & 0 deletions crates/rspack_core/src/utils/runtime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::sync::Arc;

use crate::{EntryOptions, RuntimeSpec};

pub fn get_entry_runtime(name: &str, options: &EntryOptions) -> RuntimeSpec {
RuntimeSpec::from_iter([Arc::from(
options.runtime.clone().unwrap_or_else(|| name.to_string()),
)])
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ struct ExportPropertyLibraryPluginParsed<'a> {
#[derive(Debug, Default)]
pub struct ExportPropertyLibraryPlugin {
library_type: LibraryType,
_ns_object_used: bool,
}

impl ExportPropertyLibraryPlugin {
pub fn new(library_type: LibraryType) -> Self {
Self { library_type }
pub fn new(library_type: LibraryType, ns_object_used: bool) -> Self {
Self {
library_type,
_ns_object_used: ns_object_used,
}
}

fn parse_options<'a>(
Expand Down
11 changes: 7 additions & 4 deletions crates/rspack_plugin_library/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub use system_library_plugin::SystemLibraryPlugin;
pub use umd_library_plugin::UmdLibraryPlugin;

pub fn enable_library_plugin(library_type: String, plugins: &mut Vec<BoxPlugin>) {
let ns_object_used = library_type != "module";
match library_type.as_str() {
"var" => plugins.push(
AssignLibraryPlugin::new(AssignLibraryPluginOptions {
Expand Down Expand Up @@ -99,19 +100,21 @@ pub fn enable_library_plugin(library_type: String, plugins: &mut Vec<BoxPlugin>)
.boxed(),
),
"umd" | "umd2" => {
plugins.push(ExportPropertyLibraryPlugin::default().boxed());
plugins.push(ExportPropertyLibraryPlugin::new(library_type.clone(), ns_object_used).boxed());
plugins.push(UmdLibraryPlugin::new("umd2" == library_type, library_type).boxed());
}
"amd" | "amd-require" => {
plugins.push(ExportPropertyLibraryPlugin::default().boxed());
plugins.push(ExportPropertyLibraryPlugin::new(library_type.clone(), ns_object_used).boxed());
plugins.push(AmdLibraryPlugin::new("amd-require" == library_type, library_type).boxed());
}
"module" => {
plugins.push(ExportPropertyLibraryPlugin::default().boxed());
plugins.push(ExportPropertyLibraryPlugin::new(library_type.clone(), ns_object_used).boxed());
plugins.push(ModuleLibraryPlugin::default().boxed());
}
"system" => {
plugins.push(ExportPropertyLibraryPlugin::default().boxed());
plugins.push(
ExportPropertyLibraryPlugin::new(library_type.clone(), library_type != "module").boxed(),
);
plugins.push(SystemLibraryPlugin::default().boxed());
}
_ => {}
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-dev-server/tests/normalizeOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ async function getAdditionEntries(
await server.start();
const entries = compiler.builtinPlugins
.filter(p => p.name === "EntryPlugin")
.map(p => p.raw().options)
.map(p => p.options)
.reduce<Object>((acc, cur: any) => {
const name = cur.options.name;
const request = cur.entry;
Expand Down

0 comments on commit 7bc9c0a

Please sign in to comment.