Skip to content

Commit

Permalink
Merge branch 'main' into release_1_0_0_beta_4
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Aug 10, 2024
2 parents 4ef7736 + 3867fe0 commit 9102876
Show file tree
Hide file tree
Showing 28 changed files with 692 additions and 214 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ swc_html = { version = "=0.145.0" }
swc_html_minifier = { version = "=0.142.2" }
swc_node_comments = { version = "=0.23.0" }


[workspace.metadata.release]
rate-limit = { existing-packages = 70, new-packages = 70 }
[profile.dev]
codegen-units = 16
debug = 2 # debug build will cause runtime panic if codegen-unints is default
Expand Down
38 changes: 26 additions & 12 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ export class DependenciesBlockDto {
}
export type DependenciesBlockDTO = DependenciesBlockDto

export class DependenciesDto {
get fileDependencies(): Array<string>
get addedFileDependencies(): Array<string>
get removedFileDependencies(): Array<string>
get contextDependencies(): Array<string>
get addedContextDependencies(): Array<string>
get removedContextDependencies(): Array<string>
get missingDependencies(): Array<string>
get addedMissingDependencies(): Array<string>
get removedMissingDependencies(): Array<string>
get buildDependencies(): Array<string>
get addedBuildDependencies(): Array<string>
get removedBuildDependencies(): Array<string>
}
export type DependenciesDTO = DependenciesDto

export class DependencyDto {
get type(): string
get category(): string
Expand All @@ -33,8 +49,8 @@ export class DependencyDto {
export type DependencyDTO = DependencyDto

export class EntryDataDto {
get dependencies(): Array<DependencyDto>
get includeDependencies(): Array<DependencyDto>
get dependencies(): Array<DependencyDTO>
get includeDependencies(): Array<DependencyDTO>
get options(): EntryOptionsDto
}
export type EntryDataDTO = EntryDataDto
Expand Down Expand Up @@ -82,10 +98,7 @@ export class JsCompilation {
get entrypoints(): Record<string, JsChunkGroup>
get chunkGroups(): Array<JsChunkGroup>
get hash(): string | null
getFileDependencies(): Array<string>
getContextDependencies(): Array<string>
getMissingDependencies(): Array<string>
getBuildDependencies(): Array<string>
dependencies(): DependenciesDto
pushDiagnostic(diagnostic: JsDiagnostic): void
spliceDiagnostic(start: number, end: number, replaceWith: Array<JsDiagnostic>): void
pushNativeDiagnostics(diagnostics: ExternalObject<'Diagnostic[]'>): void
Expand Down Expand Up @@ -146,6 +159,7 @@ export class ModuleDto {
get type(): string
get layer(): string | undefined
get blocks(): Array<DependenciesBlockDto>
size(ty?: string | undefined | null): number
}
export type ModuleDTO = ModuleDto

Expand Down Expand Up @@ -351,6 +365,10 @@ export interface JsBuildTimeExecutionOption {
baseUri?: string
}

export interface JsCacheGroupTestCtx {
module: ModuleDTO
}

export interface JsChunk {
__inner_ukey: number
__inner_groups: Array<number>
Expand Down Expand Up @@ -431,8 +449,8 @@ export interface JsDiagnostic {
}

export interface JsEntryData {
dependencies: Array<DependencyDto>
includeDependencies: Array<DependencyDto>
dependencies: Array<DependencyDTO>
includeDependencies: Array<DependencyDTO>
options: JsEntryOptions
}

Expand Down Expand Up @@ -993,10 +1011,6 @@ export interface RawCacheGroupOptions {
usedExports?: boolean
}

export interface RawCacheGroupTestCtx {
module: JsModule
}

export interface RawCacheOptions {
type: string
maxGenerations: number
Expand Down
9 changes: 8 additions & 1 deletion crates/node_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::sync::Mutex;
use compiler::{Compiler, CompilerState, CompilerStateGuard};
use napi::bindgen_prelude::*;
use rspack_binding_options::BuiltinPlugin;
use rspack_core::PluginExt;
use rspack_core::{Compilation, PluginExt};
use rspack_error::Diagnostic;
use rspack_fs_node::{AsyncNodeWritableFileSystem, ThreadsafeNodeFS};

Expand Down Expand Up @@ -164,6 +164,9 @@ impl Rspack {
// SAFETY: The mutable reference to `Compiler` is exclusive. It's guaranteed by the running state guard.
Ok(unsafe { s.compiler.as_mut().get_unchecked_mut() })
})?;

self.cleanup_last_compilation(&compiler.compilation);

// SAFETY:
// 1. `Compiler` is pinned and stored on the heap.
// 2. `JsReference` (NAPI internal mechanism) keeps `Compiler` alive until its instance getting garbage collected.
Expand All @@ -172,6 +175,10 @@ impl Rspack {
_guard,
)
}

fn cleanup_last_compilation(&self, compilation: &Compilation) {
JsCompilationWrapper::cleanup(compilation.id());
}
}

fn concurrent_compiler_error() -> Error {
Expand Down
88 changes: 25 additions & 63 deletions crates/node_binding/src/plugins/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use napi::{
use rspack_binding_values::{
CompatSource, JsAdditionalTreeRuntimeRequirementsArg, JsAdditionalTreeRuntimeRequirementsResult,
JsAfterResolveData, JsAfterResolveOutput, JsAssetEmittedArgs, JsBeforeResolveArgs,
JsBeforeResolveOutput, JsChunk, JsChunkAssetArgs, JsCompilation,
JsBeforeResolveOutput, JsChunk, JsChunkAssetArgs, JsCompilationWrapper,
JsContextModuleFactoryAfterResolveData, JsContextModuleFactoryAfterResolveResult,
JsContextModuleFactoryBeforeResolveData, JsContextModuleFactoryBeforeResolveResult, JsCreateData,
JsExecuteModuleArg, JsFactorizeArgs, JsFactorizeOutput, JsModule,
Expand Down Expand Up @@ -350,23 +350,23 @@ pub struct RegisterJsTaps {
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsCompilation) => void); stage: number; }>"
)]
pub register_compiler_this_compilation_taps: RegisterFunction<JsCompilation, ()>,
pub register_compiler_this_compilation_taps: RegisterFunction<JsCompilationWrapper, ()>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsCompilation) => void); stage: number; }>"
)]
pub register_compiler_compilation_taps: RegisterFunction<JsCompilation, ()>,
pub register_compiler_compilation_taps: RegisterFunction<JsCompilationWrapper, ()>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsCompilation) => Promise<void>); stage: number; }>"
)]
pub register_compiler_make_taps: RegisterFunction<JsCompilation, Promise<()>>,
pub register_compiler_make_taps: RegisterFunction<JsCompilationWrapper, Promise<()>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsCompilation) => void); stage: number; }>"
)]
pub register_compiler_finish_make_taps: RegisterFunction<JsCompilation, Promise<()>>,
pub register_compiler_finish_make_taps: RegisterFunction<JsCompilationWrapper, Promise<()>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsCompilation) => boolean | undefined); stage: number; }>"
)]
pub register_compiler_should_emit_taps: RegisterFunction<JsCompilation, Option<bool>>,
pub register_compiler_should_emit_taps: RegisterFunction<JsCompilationWrapper, Option<bool>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: (() => Promise<void>); stage: number; }>"
)]
Expand Down Expand Up @@ -410,7 +410,7 @@ pub struct RegisterJsTaps {
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsCompilation) => Promise<void>); stage: number; }>"
)]
pub register_compilation_finish_modules_taps: RegisterFunction<JsCompilation, Promise<()>>,
pub register_compilation_finish_modules_taps: RegisterFunction<JsCompilationWrapper, Promise<()>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: (() => boolean | undefined); stage: number; }>"
)]
Expand All @@ -436,11 +436,11 @@ pub struct RegisterJsTaps {
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsCompilation) => Promise<void>); stage: number; }>"
)]
pub register_compilation_process_assets_taps: RegisterFunction<JsCompilation, Promise<()>>,
pub register_compilation_process_assets_taps: RegisterFunction<JsCompilationWrapper, Promise<()>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsCompilation) => void); stage: number; }>"
)]
pub register_compilation_after_process_assets_taps: RegisterFunction<JsCompilation, ()>,
pub register_compilation_after_process_assets_taps: RegisterFunction<JsCompilationWrapper, ()>,
#[napi(ts_type = "(stages: Array<number>) => Array<{ function: (() => void); stage: number; }>")]
pub register_compilation_seal_taps: RegisterFunction<(), ()>,
#[napi(
Expand Down Expand Up @@ -500,39 +500,39 @@ pub struct RegisterJsTaps {
/* Compiler Hooks */
define_register!(
RegisterCompilerThisCompilationTaps,
tap = CompilerThisCompilationTap<JsCompilation, ()> @ CompilerThisCompilationHook,
tap = CompilerThisCompilationTap<JsCompilationWrapper, ()> @ CompilerThisCompilationHook,
cache = false,
sync = false,
kind = RegisterJsTapKind::CompilerThisCompilation,
skip = false,
);
define_register!(
RegisterCompilerCompilationTaps,
tap = CompilerCompilationTap<JsCompilation, ()> @ CompilerCompilationHook,
tap = CompilerCompilationTap<JsCompilationWrapper, ()> @ CompilerCompilationHook,
cache = false,
sync = false,
kind = RegisterJsTapKind::CompilerCompilation,
skip = true,
);
define_register!(
RegisterCompilerMakeTaps,
tap = CompilerMakeTap<JsCompilation, Promise<()>> @ CompilerMakeHook,
tap = CompilerMakeTap<JsCompilationWrapper, Promise<()>> @ CompilerMakeHook,
cache = false,
sync = false,
kind = RegisterJsTapKind::CompilerMake,
skip = true,
);
define_register!(
RegisterCompilerFinishMakeTaps,
tap = CompilerFinishMakeTap<JsCompilation, Promise<()>> @ CompilerFinishMakeHook,
tap = CompilerFinishMakeTap<JsCompilationWrapper, Promise<()>> @ CompilerFinishMakeHook,
cache = false,
sync = false,
kind = RegisterJsTapKind::CompilerFinishMake,
skip = true,
);
define_register!(
RegisterCompilerShouldEmitTaps,
tap = CompilerShouldEmitTap<JsCompilation, Option<bool>> @ CompilerShouldEmitHook,
tap = CompilerShouldEmitTap<JsCompilationWrapper, Option<bool>> @ CompilerShouldEmitHook,
cache = false,
sync = false,
kind = RegisterJsTapKind::CompilerShouldEmit,
Expand Down Expand Up @@ -598,7 +598,7 @@ define_register!(
);
define_register!(
RegisterCompilationFinishModulesTaps,
tap = CompilationFinishModulesTap<JsCompilation, Promise<()>> @ CompilationFinishModulesHook,
tap = CompilationFinishModulesTap<JsCompilationWrapper, Promise<()>> @ CompilationFinishModulesHook,
cache = false,
sync = false,
kind = RegisterJsTapKind::CompilationFinishModules,
Expand Down Expand Up @@ -670,15 +670,15 @@ define_register!(
);
define_register!(
RegisterCompilationProcessAssetsTaps,
tap = CompilationProcessAssetsTap<JsCompilation, Promise<()>> @ CompilationProcessAssetsHook,
tap = CompilationProcessAssetsTap<JsCompilationWrapper, Promise<()>> @ CompilationProcessAssetsHook,
cache = false,
sync = false,
kind = RegisterJsTapKind::CompilationProcessAssets,
skip = true,
);
define_register!(
RegisterCompilationAfterProcessAssetsTaps,
tap = CompilationAfterProcessAssetsTap<JsCompilation, ()> @ CompilationAfterProcessAssetsHook,
tap = CompilationAfterProcessAssetsTap<JsCompilationWrapper, ()> @ CompilationAfterProcessAssetsHook,
cache = false,
sync = false,
kind = RegisterJsTapKind::CompilationAfterProcessAssets,
Expand Down Expand Up @@ -786,11 +786,7 @@ impl CompilerThisCompilation for CompilerThisCompilationTap {
compilation: &mut Compilation,
_: &mut CompilationParams,
) -> rspack_error::Result<()> {
// SAFETY:
// 1. `Compiler` is stored on the heap and pinned in binding crate.
// 2. `Compilation` outlives `JsCompilation` and `Compiler` outlives `Compilation`.
// 3. `JsCompilation` was replaced everytime a new `Compilation` was created before getting accessed.
let compilation = unsafe { JsCompilation::from_compilation(compilation) };
let compilation = JsCompilationWrapper::new(compilation);
self.function.call_with_sync(compilation).await
}

Expand All @@ -806,11 +802,7 @@ impl CompilerCompilation for CompilerCompilationTap {
compilation: &mut Compilation,
_: &mut CompilationParams,
) -> rspack_error::Result<()> {
// SAFETY:
// 1. `Compiler` is stored on the heap and pinned in binding crate.
// 2. `Compilation` outlives `JsCompilation` and `Compiler` outlives `Compilation`.
// 3. `JsCompilation` was replaced everytime a new `Compilation` was created before getting accessed.
let compilation = unsafe { JsCompilation::from_compilation(compilation) };
let compilation = JsCompilationWrapper::new(compilation);
self.function.call_with_sync(compilation).await
}

Expand All @@ -822,12 +814,7 @@ impl CompilerCompilation for CompilerCompilationTap {
#[async_trait]
impl CompilerMake for CompilerMakeTap {
async fn run(&self, compilation: &mut Compilation) -> rspack_error::Result<()> {
// SAFETY:
// 1. `Compiler` is stored on the heap and pinned in binding crate.
// 2. `Compilation` outlives `JsCompilation` and `Compiler` outlives `Compilation`.
// 3. `JsCompilation` was replaced everytime a new `Compilation` was created before getting accessed.
let compilation = unsafe { JsCompilation::from_compilation(compilation) };

let compilation = JsCompilationWrapper::new(compilation);
self.function.call_with_promise(compilation).await
}

Expand All @@ -839,12 +826,7 @@ impl CompilerMake for CompilerMakeTap {
#[async_trait]
impl CompilerFinishMake for CompilerFinishMakeTap {
async fn run(&self, compilation: &mut Compilation) -> rspack_error::Result<()> {
// SAFETY:
// 1. `Compiler` is stored on the heap and pinned in binding crate.
// 2. `Compilation` outlives `JsCompilation` and `Compiler` outlives `Compilation`.
// 3. `JsCompilation` was replaced everytime a new `Compilation` was created before getting accessed.
let compilation = unsafe { JsCompilation::from_compilation(compilation) };

let compilation = JsCompilationWrapper::new(compilation);
self.function.call_with_promise(compilation).await
}

Expand All @@ -856,12 +838,7 @@ impl CompilerFinishMake for CompilerFinishMakeTap {
#[async_trait]
impl CompilerShouldEmit for CompilerShouldEmitTap {
async fn run(&self, compilation: &mut Compilation) -> rspack_error::Result<Option<bool>> {
// SAFETY:
// 1. `Compiler` is stored on the heap and pinned in binding crate.
// 2. `Compilation` outlives `JsCompilation` and `Compiler` outlives `Compilation`.
// 3. `JsCompilation` was replaced everytime a new `Compilation` was created before getting accessed.
let compilation = unsafe { JsCompilation::from_compilation(compilation) };

let compilation = JsCompilationWrapper::new(compilation);
self.function.call_with_sync(compilation).await
}

Expand Down Expand Up @@ -982,12 +959,7 @@ impl CompilationExecuteModule for CompilationExecuteModuleTap {
#[async_trait]
impl CompilationFinishModules for CompilationFinishModulesTap {
async fn run(&self, compilation: &mut Compilation) -> rspack_error::Result<()> {
// SAFETY:
// 1. `Compiler` is stored on the heap and pinned in binding crate.
// 2. `Compilation` outlives `JsCompilation` and `Compiler` outlives `Compilation`.
// 3. `JsCompilation` was replaced everytime a new `Compilation` was created before getting accessed.
let compilation = unsafe { JsCompilation::from_compilation(compilation) };

let compilation = JsCompilationWrapper::new(compilation);
self.function.call_with_promise(compilation).await
}

Expand Down Expand Up @@ -1149,12 +1121,7 @@ impl CompilationChunkAsset for CompilationChunkAssetTap {
#[async_trait]
impl CompilationProcessAssets for CompilationProcessAssetsTap {
async fn run(&self, compilation: &mut Compilation) -> rspack_error::Result<()> {
// SAFETY:
// 1. `Compiler` is stored on the heap and pinned in binding crate.
// 2. `Compilation` outlives `JsCompilation` and `Compiler` outlives `Compilation`.
// 3. `JsCompilation` was replaced everytime a new `Compilation` was created before getting accessed.
let compilation = unsafe { JsCompilation::from_compilation(compilation) };

let compilation = JsCompilationWrapper::new(compilation);
self.function.call_with_promise(compilation).await
}

Expand All @@ -1166,12 +1133,7 @@ impl CompilationProcessAssets for CompilationProcessAssetsTap {
#[async_trait]
impl CompilationAfterProcessAssets for CompilationAfterProcessAssetsTap {
async fn run(&self, compilation: &mut Compilation) -> rspack_error::Result<()> {
// SAFETY:
// 1. `Compiler` is stored on the heap and pinned in binding crate.
// 2. `Compilation` outlives `JsCompilation` and `Compiler` outlives `Compilation`.
// 3. `JsCompilation` was replaced everytime a new `Compilation` was created before getting accessed.
let compilation = unsafe { JsCompilation::from_compilation(compilation) };

let compilation = JsCompilationWrapper::new(compilation);
self.function.call_with_sync(compilation).await
}

Expand Down
Loading

0 comments on commit 9102876

Please sign in to comment.