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: add build unique id for detecting #6865

Merged
merged 5 commits into from
Jun 24, 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
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,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
Loading