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 context info issuer layer for external function #8832

Merged
merged 1 commit into from
Dec 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 @@ -371,6 +371,7 @@ export declare function cleanupGlobalTrace(): void

export interface ContextInfo {
issuer: string
issuerLayer?: string
}

export declare function formatDiagnostic(diagnostic: JsDiagnostic): ExternalObject<'Diagnostic'>
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_values/src/raw_options/raw_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl From<RawExternalItemFnResult> for ExternalItemFnResult {
#[napi(object)]
pub struct ContextInfo {
pub issuer: String,
pub issuer_layer: Option<String>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -120,6 +121,7 @@ impl From<ExternalItemFnCtx> for RawExternalItemFnCtx {
context: value.context,
context_info: ContextInfo {
issuer: value.context_info.issuer,
issuer_layer: value.context_info.issuer_layer,
},
resolve_options_with_dependency_type: value.resolve_options_with_dependency_type,
resolver_factory: value.resolver_factory,
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/options/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub type ExternalItemObject = HashMap<String, ExternalItemValue>;

pub struct ContextInfo {
pub issuer: String,
pub issuer_layer: Option<String>,
}

pub struct ExternalItemFnCtx {
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_plugin_externals/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ async fn factorize(&self, data: &mut ModuleFactoryCreateData) -> Result<Option<B
issuer: data
.issuer
.clone()
.map_or("".to_string(), |i| i.to_string()),
.map(|i| i.to_string())
.unwrap_or_default(),
issuer_layer: data.issuer_layer.clone(),
},
resolve_options_with_dependency_type: ResolveOptionsWithDependencyType {
resolve_options: data
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import e1 from "external-pkg";
import e2 from "./other-layer";

it("should have the correct value", () => {
expect(e1).toBe(1);
expect(e2).toBe(2);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import e2 from "external-pkg";

export default e2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
entry: "./index.js",
module: {
rules: [
{
test: /other-layer\.js$/,
layer: "other-layer",
}
]
},
externals: [
function ({ context, request, contextInfo }, callback) {
if (request === "external-pkg") {
if (contextInfo.issuerLayer === "other-layer") {
return callback(null, "var 2");
}
return callback(null, "var 1");
}
return callback()
}
],
experiments: {
layers: true,
}
};
1 change: 1 addition & 0 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ export type Context = string;
// @public (undocumented)
type ContextInfo = {
issuer: string;
issuerLayer?: string;
};

// @public (undocumented)
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type ResourceDataWithData = ResourceData & {
export type CreateData = Partial<JsCreateData>;
export type ContextInfo = {
issuer: string;
issuerLayer?: string;
};
export type ResolveData = {
contextInfo: ContextInfo;
Expand Down
3 changes: 2 additions & 1 deletion packages/rspack/src/builtin-plugin/ExternalsPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
type BuiltinPlugin,
BuiltinPluginName,
type RawExternalItemFnCtx,
type RawExternalsPluginOptions
} from "@rspack/binding";

Expand Down Expand Up @@ -44,7 +45,7 @@ function getRawExternalItem(
}

if (typeof item === "function") {
return async ctx => {
return async (ctx: RawExternalItemFnCtx) => {
return await new Promise((resolve, reject) => {
const data = ctx.data();
const promise = item(
Expand Down
Loading