Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Sep 24, 2024
1 parent c1f8f8e commit b496dc3
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 85 deletions.
4 changes: 0 additions & 4 deletions crates/rspack_plugin_context_replacement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,11 @@ async fn cmf_before_resolve(&self, mut result: BeforeResolveResult) -> Result<Be
if let Some(new_content_reg_exp) = &self.new_content_reg_exp {
data.reg_exp = Some(new_content_reg_exp.clone());
}
// if let Some(new_content_callback) = &self.new_content_after_resolve_callback {
// new_content_callback(&mut result).await?;
// } else {
for d in &mut data.dependencies {
if let Some(d) = d.as_context_dependency_mut() {
*d.critical_mut() = None;
}
}
// }
}

Ok(result)
Expand Down
34 changes: 23 additions & 11 deletions packages/rspack/src/ChunkGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,53 @@ import {
import { Chunk } from "./Chunk";
import type { Compilation } from "./Compilation";
import { Module } from "./Module";
import type { Lifecycle } from "./util/lifecycle";

export class ChunkGraph {
constructor(private compilation: Compilation) {}
#compilation: Compilation;
#lifecycle: Lifecycle;

constructor(compilation: Compilation, lifecycle: Lifecycle) {
this.#compilation = compilation;
this.#lifecycle = lifecycle;
}

getChunkModules(chunk: Chunk): Readonly<Module[]> {
this.#lifecycle.ensureActive();
return __chunk_graph_inner_get_chunk_modules(
chunk.__internal__innerUkey(),
this.compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.compilation));
this.#compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.#lifecycle, this.#compilation));
}

getChunkModulesIterable(chunk: Chunk): Iterable<Module> {
this.#lifecycle.ensureActive();
return new Set(
__chunk_graph_inner_get_chunk_modules(
chunk.__internal__innerUkey(),
this.compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.compilation))
this.#compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.#lifecycle, this.#compilation))
);
}

getChunkEntryModulesIterable(chunk: Chunk): Iterable<Module> {
this.#lifecycle.ensureActive();
return new Set(
__chunk_graph_inner_get_chunk_entry_modules(
chunk.__internal__innerUkey(),
this.compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.compilation))
this.#compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.#lifecycle, this.#compilation))
);
}

getChunkEntryDependentChunksIterable(chunk: Chunk): Iterable<Chunk> {
this.#lifecycle.ensureActive();
return new Set(
__chunk_graph_inner_get_chunk_entry_dependent_chunks_iterable(
chunk.__internal__innerUkey(),
this.compilation.__internal_getInner()
this.#compilation.__internal_getInner()
).map(c =>
Chunk.__from_binding(c, this.compilation.__internal_getInner())
Chunk.__from_binding(c, this.#compilation.__internal_getInner())
)
);
}
Expand All @@ -52,12 +63,13 @@ export class ChunkGraph {
chunk: Chunk,
sourceType: string
): Iterable<Module> {
this.#lifecycle.ensureActive();
return new Set(
__chunk_graph_inner_get_chunk_modules_iterable_by_source_type(
chunk.__internal__innerUkey(),
sourceType,
this.compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.compilation))
this.#compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.#lifecycle, this.#compilation))
);
}
}
6 changes: 5 additions & 1 deletion packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { createReadonlyMap } from "./util/createReadonlyMap";
import { createFakeCompilationDependencies } from "./util/fake";
import type { InputFileSystem } from "./util/fs";
import type Hash from "./util/hash";
import type { Lifecycle } from "./util/lifecycle";
import { memoizeValue } from "./util/memoize";
import { JsSource } from "./util/source";
export { type AssetInfo } from "./util/AssetInfo";
Expand Down Expand Up @@ -252,7 +253,9 @@ export class Compilation {
}
>;

constructor(compiler: Compiler, inner: JsCompilation) {
#lifecycle: Lifecycle;

constructor(compiler: Compiler, inner: JsCompilation, lifecycle: Lifecycle) {
this.#inner = inner;
this.#customModules = {};

Expand Down Expand Up @@ -365,6 +368,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
this.childrenCounters = {};
this.children = [];
this.chunkGraph = new ChunkGraph(this);
this.#lifecycle = lifecycle;
}

get hash(): Readonly<string | null> {
Expand Down
24 changes: 19 additions & 5 deletions packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import type {
OutputFileSystem,
WatchFileSystem
} from "./util/fs";
import { Lifecycle } from "./util/lifecycle";

export interface AssetEmittedInfo {
content: Buffer;
Expand All @@ -85,6 +86,7 @@ class Compiler {
#instance?: binding.Rspack;
#initial: boolean;

#compilationLifecycle?: Lifecycle;
#compilation?: Compilation;
#compilationParams?: CompilationParams;

Expand Down Expand Up @@ -706,13 +708,23 @@ class Compiler {
}

#createCompilation(native: binding.JsCompilation): Compilation {
const compilation = new Compilation(this, native);
this.#compilationLifecycle = new Lifecycle(
"The associated Compilation has exceeded the lifetime of its internal Rust instance, making this object inaccessible"
);
const compilation = new Compilation(
this,
native,
this.#compilationLifecycle
);
compilation.name = this.name;
this.#compilation = compilation;
return compilation;
}

#resetThisCompilation() {
if (this.#compilationLifecycle) {
this.#compilationLifecycle.drop();
}
// reassign new compilation in thisCompilation
this.#compilation = undefined;
// ensure thisCompilation must call
Expand Down Expand Up @@ -1185,15 +1197,17 @@ class Compiler {
| false
| binding.JsContextModuleFactoryAfterResolveData
) => {
const lifecycle = new Lifecycle(
"The current object can only be used during the ContextModuleFactory afterResolve phase and is inaccessible beyond this lifecycle"
);
const data = bindingData
? ContextModuleFactoryAfterResolveData.__from_binding(
bindingData
bindingData,
lifecycle
)
: false;
const result = await queried.promise(data);
if (data) {
ContextModuleFactoryAfterResolveData.__drop(data);
}
lifecycle.drop();
return result
? ContextModuleFactoryAfterResolveData.__to_binding(result)
: false;
Expand Down
15 changes: 12 additions & 3 deletions packages/rspack/src/DependenciesBlock.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import type { DependenciesBlockDTO } from "@rspack/binding";
import { Dependency } from "./Dependency";
import type { Lifecycle } from "./util/lifecycle";

export class DependenciesBlock {
#binding: DependenciesBlockDTO;
#lifecycle: Lifecycle;

constructor(binding: DependenciesBlockDTO) {
constructor(binding: DependenciesBlockDTO, lifecycle: Lifecycle) {
this.#binding = binding;
this.#lifecycle = lifecycle;
}

get dependencies(): Dependency[] {
return this.#binding.dependencies.map(d => Dependency.__from_binding(d));
this.#lifecycle.ensureActive();
return this.#binding.dependencies.map(d =>
Dependency.__from_binding(d, this.#lifecycle)
);
}

get blocks(): DependenciesBlock[] {
return this.#binding.blocks.map(b => new DependenciesBlock(b));
this.#lifecycle.ensureActive();
return this.#binding.blocks.map(
b => new DependenciesBlock(b, this.#lifecycle)
);
}
}
38 changes: 17 additions & 21 deletions packages/rspack/src/Dependency.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,47 @@
import { type JsDependency, JsDependencyMut } from "@rspack/binding";
import type { Lifecycle } from "./util/lifecycle";

export class Dependency {
#binding: JsDependencyMut | JsDependency;
#dropped = false;
#lifecycle: Lifecycle;

static __from_binding(binding: JsDependencyMut | JsDependency): Dependency {
return new Dependency(binding);
static __from_binding(
binding: JsDependencyMut | JsDependency,
lifecycle: Lifecycle
): Dependency {
return new Dependency(binding, lifecycle);
}

static __drop(dependency: Dependency) {
dependency.#dropped = true;
}

private ensureValidLifecycle() {
if (this.#dropped) {
throw new Error(
"The Dependency has exceeded its lifecycle and has been dropped by Rust."
);
}
}

private constructor(binding: JsDependencyMut | JsDependency) {
private constructor(
binding: JsDependencyMut | JsDependency,
lifecycle: Lifecycle
) {
this.#binding = binding;
this.#lifecycle = lifecycle;
}

get type(): string {
this.ensureValidLifecycle();
this.#lifecycle.ensureActive();
return this.#binding.type;
}

get category(): string {
this.ensureValidLifecycle();
this.#lifecycle.ensureActive();
return this.#binding.category;
}

get request(): string | undefined {
this.ensureValidLifecycle();
this.#lifecycle.ensureActive();
return this.#binding.request;
}

get critital(): boolean {
this.ensureValidLifecycle();
this.#lifecycle.ensureActive();
return this.#binding.critical;
}

set critital(critital: boolean) {
this.ensureValidLifecycle();
this.#lifecycle.ensureActive();
if (
typeof critital === "boolean" &&
this.#binding instanceof JsDependencyMut
Expand Down
Loading

0 comments on commit b496dc3

Please sign in to comment.