Skip to content

Commit

Permalink
feat: add build unique id for detecting (#6865)
Browse files Browse the repository at this point in the history
* feat: add build unique id for detecting

* feat: add build unique id for detecting
  • Loading branch information
LingyuCoder authored Jun 24, 2024
1 parent fc2e737 commit 7215fa8
Show file tree
Hide file tree
Showing 42 changed files with 455 additions and 55 deletions.
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ export interface RawBuiltins {

export interface RawBundlerInfoPluginOptions {
version: string
bundler: string
force: boolean | string[]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,9 @@ impl BuiltinPlugin {
let plugin_options = downcast_into::<RawBundlerInfoPluginOptions>(self.options)?;
plugins.push(
BundlerInfoPlugin::new(
RawBundlerInfoModeWrapper(plugin_options.force).into(),
plugin_options.version,
plugin_options.bundler,
RawBundlerInfoModeWrapper(plugin_options.force).into(),
)
.boxed(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct RawBundlerInfoModeWrapper(pub RawBundlerInfoMode);
#[napi(object)]
pub struct RawBundlerInfoPluginOptions {
pub version: String,
pub bundler: String,
#[napi(ts_type = "boolean | string[]")]
pub force: RawBundlerInfoMode,
}
Expand Down
9 changes: 7 additions & 2 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,13 +1217,18 @@ impl Compilation {
) -> Result<()> {
let mut runtime_requirements_mut = *requirements;
let mut runtime_requirements;
while !runtime_requirements_mut.is_empty() {
requirements.insert(runtime_requirements_mut);

loop {
runtime_requirements = runtime_requirements_mut;
runtime_requirements_mut = RuntimeGlobals::default();
call_hook(&runtime_requirements, &mut runtime_requirements_mut)?;
runtime_requirements_mut =
runtime_requirements_mut.difference(requirements.intersection(runtime_requirements_mut));
if runtime_requirements_mut.is_empty() {
break;
} else {
requirements.insert(runtime_requirements_mut);
}
}
Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion crates/rspack_core/src/runtime_globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use swc_core::ecma::atoms::Atom;

bitflags! {
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct RuntimeGlobals: u64 {
pub struct RuntimeGlobals: u128 {
const REQUIRE_SCOPE = 1 << 0;

/**
Expand Down Expand Up @@ -243,6 +243,8 @@ bitflags! {
const RSPACK_VERSION = 1 << 62;

const HAS_CSS_MODULES = 1 << 63;

const RSPACK_UNIQUE_ID = 1 << 64;
}
}

Expand Down Expand Up @@ -325,6 +327,7 @@ impl RuntimeGlobals {
R::PRELOAD_CHUNK_HANDLERS => "__webpack_require__.H",
// rspack only
R::RSPACK_VERSION => "__webpack_require__.rv",
R::RSPACK_UNIQUE_ID => "__webpack_require__.ruid",
R::HAS_CSS_MODULES => "has css modules",
_ => unreachable!(),
}
Expand Down
19 changes: 16 additions & 3 deletions crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const WEBPACK_CHUNK_NAME: &str = "__webpack_chunkname__";
const WEBPACK_RUNTIME_ID: &str = "__webpack_runtime_id__";
const WEBPACK_REQUIRE: &str = RuntimeGlobals::REQUIRE.name();
const RSPACK_VERSION: &str = "__rspack_version__";
const RSPACK_UNIQUE_ID: &str = "__rspack_unique_id__";

pub struct APIPluginOptions {
module: bool,
Expand Down Expand Up @@ -226,6 +227,18 @@ impl JavascriptParserPlugin for APIPlugin {
)));
Some(true)
}
WEBPACK_RUNTIME_ID => {
parser
.presentational_dependencies
.push(Box::new(ConstDependency::new(
ident.span.real_lo(),
ident.span.real_hi(),
RuntimeGlobals::RUNTIME_ID.name().into(),
Some(RuntimeGlobals::RUNTIME_ID),
)));
Some(true)
}
// rspack specific
RSPACK_VERSION => {
parser
.presentational_dependencies
Expand All @@ -237,14 +250,14 @@ impl JavascriptParserPlugin for APIPlugin {
)));
Some(true)
}
WEBPACK_RUNTIME_ID => {
RSPACK_UNIQUE_ID => {
parser
.presentational_dependencies
.push(Box::new(ConstDependency::new(
ident.span.real_lo(),
ident.span.real_hi(),
RuntimeGlobals::RUNTIME_ID.name().into(),
Some(RuntimeGlobals::RUNTIME_ID),
format!("{}", RuntimeGlobals::RSPACK_UNIQUE_ID).into(),
Some(RuntimeGlobals::RSPACK_UNIQUE_ID),
)));
Some(true)
}
Expand Down
59 changes: 49 additions & 10 deletions crates/rspack_plugin_runtime/src/bundler_info.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use rspack_core::{
ChunkUkey, Compilation, CompilationRuntimeRequirementInTree, Plugin, PluginContext,
RuntimeGlobals,
ChunkUkey, Compilation, CompilationAdditionalTreeRuntimeRequirements,
CompilationRuntimeRequirementInTree, Plugin, PluginContext, RuntimeGlobals,
};
use rspack_error::Result;
use rspack_hook::{plugin, plugin_hook};
use rustc_hash::FxHashSet;

use crate::runtime_module::RspackVersionRuntimeModule;
use crate::runtime_module::{RspackUniqueIdRuntimeModule, RspackVersionRuntimeModule};

#[derive(Debug)]
pub enum BundlerInfoForceMode {
Expand All @@ -19,15 +19,43 @@ pub enum BundlerInfoForceMode {
#[derive(Debug)]
pub struct BundlerInfoPlugin {
version: String,
bundler_name: String,
force: BundlerInfoForceMode,
}

impl BundlerInfoPlugin {
pub fn new(force: BundlerInfoForceMode, version: String) -> Self {
Self::new_inner(version, force)
pub fn new(version: String, bundler_name: String, force: BundlerInfoForceMode) -> Self {
Self::new_inner(version, bundler_name, force)
}
}

#[plugin_hook(CompilationAdditionalTreeRuntimeRequirements for BundlerInfoPlugin)]
async fn additional_tree_runtime_requirements(
&self,
_compilation: &mut Compilation,
_chunk_ukey: &ChunkUkey,
runtime_requirements: &mut RuntimeGlobals,
) -> Result<()> {
if match &self.force {
BundlerInfoForceMode::All => true,
BundlerInfoForceMode::Partial(s) => s.get("version").is_some(),
BundlerInfoForceMode::Auto => runtime_requirements.contains(RuntimeGlobals::RSPACK_VERSION),
} {
runtime_requirements.insert(RuntimeGlobals::REQUIRE);
runtime_requirements.insert(RuntimeGlobals::RSPACK_VERSION);
}

if match &self.force {
BundlerInfoForceMode::All => true,
BundlerInfoForceMode::Partial(s) => s.get("uniqueId").is_some(),
BundlerInfoForceMode::Auto => runtime_requirements.contains(RuntimeGlobals::RSPACK_UNIQUE_ID),
} {
runtime_requirements.insert(RuntimeGlobals::REQUIRE);
runtime_requirements.insert(RuntimeGlobals::RSPACK_UNIQUE_ID);
}
Ok(())
}

#[plugin_hook(CompilationRuntimeRequirementInTree for BundlerInfoPlugin)]
fn runtime_requirements_in_tree(
&self,
Expand All @@ -36,16 +64,22 @@ fn runtime_requirements_in_tree(
runtime_requirements: &RuntimeGlobals,
_runtime_requirements_mut: &mut RuntimeGlobals,
) -> Result<Option<()>> {
if match &self.force {
BundlerInfoForceMode::All => true,
BundlerInfoForceMode::Partial(s) => s.get("version").is_some(),
BundlerInfoForceMode::Auto => runtime_requirements.contains(RuntimeGlobals::RSPACK_VERSION),
} {
if runtime_requirements.contains(RuntimeGlobals::RSPACK_VERSION) {
compilation.add_runtime_module(
chunk_ukey,
Box::new(RspackVersionRuntimeModule::new(self.version.clone())),
)?;
}

if runtime_requirements.contains(RuntimeGlobals::RSPACK_UNIQUE_ID) {
compilation.add_runtime_module(
chunk_ukey,
Box::new(RspackUniqueIdRuntimeModule::new(
self.bundler_name.clone(),
self.version.clone(),
)),
)?;
}
Ok(None)
}

Expand All @@ -59,6 +93,11 @@ impl Plugin for BundlerInfoPlugin {
ctx: PluginContext<&mut rspack_core::ApplyContext>,
_options: &mut rspack_core::CompilerOptions,
) -> Result<()> {
ctx
.context
.compilation_hooks
.additional_tree_runtime_requirements
.tap(additional_tree_runtime_requirements::new(self));
ctx
.context
.compilation_hooks
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_plugin_runtime/src/runtime_module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod public_path;
mod readfile_chunk_loading;
mod relative_url;
mod require_js_chunk_loading;
mod rspack_unique_id;
mod rspack_version;
mod runtime_id;
mod startup_chunk_dependencies;
Expand Down Expand Up @@ -72,6 +73,7 @@ pub use public_path::PublicPathRuntimeModule;
pub use readfile_chunk_loading::ReadFileChunkLoadingRuntimeModule;
pub use relative_url::RelativeUrlRuntimeModule;
pub use require_js_chunk_loading::RequireChunkLoadingRuntimeModule;
pub use rspack_unique_id::RspackUniqueIdRuntimeModule;
pub use rspack_version::RspackVersionRuntimeModule;
pub use runtime_id::RuntimeIdRuntimeModule;
pub use startup_chunk_dependencies::StartupChunkDependenciesRuntimeModule;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use rspack_core::{
impl_runtime_module,
rspack_sources::{BoxSource, RawSource, SourceExt},
Compilation, RuntimeModule, RuntimeModuleStage,
};
use rspack_identifier::Identifier;

#[impl_runtime_module]
#[derive(Debug)]
pub struct RspackUniqueIdRuntimeModule {
id: Identifier,
bundler_name: String,
bundler_version: String,
}

impl RspackUniqueIdRuntimeModule {
pub fn new(bundler_name: String, bundler_version: String) -> Self {
Self::with_default(
Identifier::from("webpack/runtime/rspack_unique_id"),
bundler_name,
bundler_version,
)
}
}

impl RuntimeModule for RspackUniqueIdRuntimeModule {
fn stage(&self) -> RuntimeModuleStage {
RuntimeModuleStage::Attach
}
fn name(&self) -> Identifier {
self.id
}

fn generate(&self, _: &Compilation) -> rspack_error::Result<BoxSource> {
Ok(
RawSource::from(
include_str!("runtime/get_unique_id.js")
.replace("$BUNDLER_NAME$", &self.bundler_name)
.replace("$BUNDLER_VERSION$", &self.bundler_version),
)
.boxed(),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__webpack_require__.ruid = "bundler=$BUNDLER_NAME$@$BUNDLER_VERSION$";
7 changes: 6 additions & 1 deletion packages/rspack-test-tools/src/processor/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ export class BuiltinProcessor<
extensions: [".js"]
},
experiments: {
futureDefaults: true
futureDefaults: true,
rspackFuture: {
bundlerInfo: {
force: false
}
}
},
devtool: false,
context: context.getSource(),
Expand Down
7 changes: 7 additions & 0 deletions packages/rspack-test-tools/src/processor/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ export class ConfigProcessor<
},
optimization: {
minimize: false
},
experiments: {
rspackFuture: {
bundlerInfo: {
force: false
}
}
}
} as TCompilerOptions<T>;
}
Expand Down
9 changes: 8 additions & 1 deletion packages/rspack-test-tools/src/processor/diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ export class DiagnosticProcessor<
},
output: {
path: context.getDist()
},
experiments: {
rspackFuture: {
bundlerInfo: {
force: false
}
}
}
};
} as TCompilerOptions<T>;
}
}
12 changes: 11 additions & 1 deletion packages/rspack-test-tools/src/processor/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,17 @@ export class DiffProcessor implements ITestProcessor {
plugins: [
type === ECompilerType.Webpack && new WebpackDiffConfigPlugin(),
type === ECompilerType.Rspack && new RspackDiffConfigPlugin()
].filter(Boolean)
].filter(Boolean),
experiments:
type === ECompilerType.Rspack
? {
rspackFuture: {
bundlerInfo: {
force: false
}
}
}
: {}
} as TCompilerOptions<T>;
}

Expand Down
7 changes: 7 additions & 0 deletions packages/rspack-test-tools/src/processor/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ export class ErrorProcessor<
devtool: false,
optimization: {
minimize: false
},
experiments: {
rspackFuture: {
bundlerInfo: {
force: false
}
}
}
} as TCompilerOptions<T>;
if (typeof _errorOptions.options === "function") {
Expand Down
9 changes: 8 additions & 1 deletion packages/rspack-test-tools/src/processor/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@ export class HashProcessor<
context: context.getSource(),
output: {
path: context.getDist()
},
experiments: {
rspackFuture: {
bundlerInfo: {
force: false
}
}
}
};
} as TCompilerOptions<T>;
}
static overrideOptions<T extends ECompilerType>(
index: number,
Expand Down
7 changes: 7 additions & 0 deletions packages/rspack-test-tools/src/processor/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ export class HookTaskProcessor<
},
optimization: {
minimize: false
},
experiments: {
rspackFuture: {
bundlerInfo: {
force: false
}
}
}
} as TCompilerOptions<T>;
}
Expand Down
Loading

2 comments on commit 7215fa8

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
nx ✅ success
rspress ❌ failure
rsbuild ❌ failure
compat ✅ success
examples ❌ failure

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-06-20 5839629) Current Change
10000_development-mode + exec 2.21 s ± 12 ms 2.19 s ± 22 ms -1.03 %
10000_development-mode_hmr + exec 714 ms ± 9.7 ms 722 ms ± 7.5 ms +1.08 %
10000_production-mode + exec 2.77 s ± 28 ms 2.74 s ± 24 ms -0.96 %
arco-pro_development-mode + exec 1.9 s ± 78 ms 1.89 s ± 60 ms -0.42 %
arco-pro_development-mode_hmr + exec 439 ms ± 0.92 ms 437 ms ± 1.8 ms -0.48 %
arco-pro_production-mode + exec 3.47 s ± 86 ms 3.49 s ± 41 ms +0.64 %
threejs_development-mode_10x + exec 2.06 s ± 21 ms 1.99 s ± 13 ms -3.56 %
threejs_development-mode_10x_hmr + exec 812 ms ± 4.1 ms 820 ms ± 6.6 ms +0.99 %
threejs_production-mode_10x + exec 5.33 s ± 24 ms 5.21 s ± 27 ms -2.13 %

Please sign in to comment.