Skip to content

Commit

Permalink
fix: channal error when using sync externals function (#8884)
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder authored Dec 30, 2024
1 parent 92e5e4f commit 3074092
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "./inject";

import foo from "foo";
import raz from "raz";
const myos = require("myos");
const bar = require("bar");
const baz = require("baz");
const fn = require("fn");
const asyncFn = require("asyncFn");
const external = require("external");
const external2 = require("external2");
const external3 = require("external3");
const external4 = require("external4");
const external5 = require("external5");

it("should work with array type of externals", function () {
expect(foo).toBe("foo");
expect(bar).toBe("bar");
expect(baz).toBe("baz");
expect(raz).toBe("raz");
expect(fn).toBe("fn");
expect(asyncFn).toBe("asyncFn");
expect(typeof myos.constants.errno.EBUSY).toBe("number");

expect(external).toBe(Array.isArray);
expect(external2).toBe(process.version);
expect(external3).toBe(globalThis);
expect(external4).toBe(global.process.version);
expect(external5).toBe("yj");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global.foo = "foo";
global.raz = "raz";
global.obj = { name: "yj" };
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
externals: [
"path",
"foo",
/^raz$/,
{
bar: "'bar'",
baz: "var 'baz'",
myos: "commonjs os",
external: ["Array", "isArray"],
external2: ["commonjs process", "version"],
external3: ["var globalThis"],
external4: ["global process", "version"],
external5: ["this obj", "name"]
},
function ({ request }) {
if (request === "fn") {
return "'fn'";
}
},
function ({ request }) {
if (request === "asyncFn") {
return "'asyncFn'";
}
}
],
externalsPresets: {
node: false
}
};
8 changes: 4 additions & 4 deletions packages/rspack/etc/core.api.md

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions packages/rspack/src/builtin-plugin/ExternalsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,24 @@ function getRawExternalItem(
externalType: type
});
}
) as Promise<ExternalItemValue>;
if (promise?.then) {
promise.then(
) as Promise<ExternalItemValue> | ExternalItemValue | undefined;
if ((promise as Promise<ExternalItemValue>)?.then) {
(promise as Promise<ExternalItemValue>).then(
result =>
resolve({
result: getRawExternalItemValueFormFnResult(result),
externalType: undefined
}),
e => reject(e)
);
} else if (item.length === 1) {
// No callback and no promise returned, regarded as a synchronous function
resolve({
result: getRawExternalItemValueFormFnResult(
promise as ExternalItemValue | undefined
),
externalType: undefined
});
}
});
};
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,7 @@ export type ExternalItem =
| string
| RegExp
| ExternalItemObjectUnknown
| ((data: ExternalItemFunctionData) => ExternalItemValue)
| ((
data: ExternalItemFunctionData,
callback: (
Expand Down
6 changes: 6 additions & 0 deletions packages/rspack/src/config/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,12 @@ const externalItem = z
.function()
.args(externalItemFunctionData as z.ZodType<t.ExternalItemFunctionData>)
.returns(z.promise(externalItemValue))
)
.or(
z
.function()
.args(externalItemFunctionData as z.ZodType<t.ExternalItemFunctionData>)
.returns(externalItemValue)
) satisfies z.ZodType<t.ExternalItem>;

const externals = externalItem
Expand Down

1 comment on commit 3074092

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 3074092 Dec 30, 2024

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-12-30 437c370) Current Change
10000_big_production-mode_disable-minimize + exec 37.5 s ± 658 ms 38.3 s ± 702 ms +2.14 %
10000_development-mode + exec 1.89 s ± 22 ms 1.82 s ± 29 ms -3.81 %
10000_development-mode_hmr + exec 685 ms ± 15 ms 684 ms ± 35 ms -0.08 %
10000_production-mode + exec 2.49 s ± 46 ms 2.48 s ± 77 ms -0.38 %
arco-pro_development-mode + exec 1.75 s ± 76 ms 1.76 s ± 92 ms +0.59 %
arco-pro_development-mode_hmr + exec 378 ms ± 3.8 ms 376 ms ± 0.76 ms -0.42 %
arco-pro_production-mode + exec 3.64 s ± 75 ms 3.54 s ± 127 ms -2.72 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.64 s ± 76 ms 3.63 s ± 73 ms -0.35 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.63 s ± 80 ms 3.58 s ± 90 ms -1.38 %
threejs_development-mode_10x + exec 1.52 s ± 18 ms 1.51 s ± 25 ms -0.62 %
threejs_development-mode_10x_hmr + exec 790 ms ± 8.7 ms 769 ms ± 12 ms -2.66 %
threejs_production-mode_10x + exec 5.34 s ± 72 ms 5.36 s ± 73 ms +0.32 %
10000_big_production-mode_disable-minimize + rss memory 9458 MiB ± 133 MiB 9520 MiB ± 73.3 MiB +0.66 %
10000_development-mode + rss memory 650 MiB ± 8.03 MiB 690 MiB ± 16 MiB +6.08 %
10000_development-mode_hmr + rss memory 1421 MiB ± 196 MiB 1502 MiB ± 331 MiB +5.74 %
10000_production-mode + rss memory 618 MiB ± 21.6 MiB 678 MiB ± 30.2 MiB +9.83 %
arco-pro_development-mode + rss memory 592 MiB ± 24.8 MiB 586 MiB ± 21.1 MiB -1.10 %
arco-pro_development-mode_hmr + rss memory 636 MiB ± 47.2 MiB 633 MiB ± 87.2 MiB -0.49 %
arco-pro_production-mode + rss memory 725 MiB ± 44.8 MiB 769 MiB ± 57 MiB +6.15 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 770 MiB ± 58.4 MiB 775 MiB ± 57.6 MiB +0.63 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 750 MiB ± 36.5 MiB 743 MiB ± 42.1 MiB -0.83 %
threejs_development-mode_10x + rss memory 624 MiB ± 24 MiB 656 MiB ± 31.1 MiB +5.10 %
threejs_development-mode_10x_hmr + rss memory 1173 MiB ± 179 MiB 1197 MiB ± 177 MiB +2.10 %
threejs_production-mode_10x + rss memory 895 MiB ± 49.3 MiB 944 MiB ± 56.2 MiB +5.45 %

Please sign in to comment.