


























@@ -15,21 +15,19 @@ async function loadCachedPluginModuleLoader(scope: string) {
1515 options,
1616}),
1717);
18-vi.doMock("jiti", () => ({
19- createJiti,
20-}));
211822-const { getCachedPluginModuleLoader } = await importFreshModule<
19+const pluginModuleLoaderCache = await importFreshModule<
2320typeof import("./plugin-module-loader-cache.js")
2421>(import.meta.url, `./plugin-module-loader-cache.js?scope=${scope}`);
25-26-const getCachedPluginModuleLoaderWithMock: typeof getCachedPluginModuleLoader = (params) =>
27-getCachedPluginModuleLoader({
22+const getCachedPluginModuleLoader: typeof pluginModuleLoaderCache.getCachedPluginModuleLoader = (
23+params,
24+) =>
25+pluginModuleLoaderCache.getCachedPluginModuleLoader({
2826 ...params,
2927createLoader: params.createLoader ?? asPluginModuleLoaderFactory(createJiti),
3028});
312932-return { createJiti, getCachedPluginModuleLoader: getCachedPluginModuleLoaderWithMock };
30+return { createJiti, getCachedPluginModuleLoader };
3331}
34323533function asPluginModuleLoaderFactory(factory: unknown): PluginModuleLoaderFactory {
@@ -372,8 +370,6 @@ describe("getCachedPluginModuleLoader", () => {
372370it("serves compiled .js targets from native require without invoking the module loader", async () => {
373371const fromSourceTransformer = vi.fn();
374372const createJiti = vi.fn(() => fromSourceTransformer);
375-const jitiModuleFactory = vi.fn(() => ({ createJiti }));
376-vi.doMock("jiti", jitiModuleFactory);
377373const nativeStub = vi.fn((target: string) => ({
378374ok: true as const,
379375moduleExport: { loadedFrom: target },
@@ -400,13 +396,17 @@ describe("getCachedPluginModuleLoader", () => {
400396expect(result.loadedFrom).toBe("/repo/dist/extensions/demo/api.js");
401397// Jiti should not be constructed or invoked for .js targets that
402398// `tryNativeRequireJavaScriptModule` resolves.
403-expect(jitiModuleFactory).not.toHaveBeenCalled();
404399expect(createJiti).not.toHaveBeenCalled();
405400expect(fromSourceTransformer).not.toHaveBeenCalled();
406401// allowWindows must be passed so the native fast path works on Windows too.
407-expect(nativeStub).toHaveBeenCalledWith("/repo/dist/extensions/demo/api.js", {
408-allowWindows: true,
409-});
402+expect(nativeStub).toHaveBeenCalledWith(
403+"/repo/dist/extensions/demo/api.js",
404+expect.objectContaining({
405+allowWindows: true,
406+fallbackOnMissingDependency: true,
407+fallbackOnNativeError: true,
408+}),
409+);
410410expect(getPluginModuleLoaderStats()).toMatchObject({
411411calls: 1,
412412nativeHits: 1,
@@ -446,9 +446,14 @@ describe("getCachedPluginModuleLoader", () => {
446446expect(() => loader("/repo/dist/extensions/demo/api.js")).toThrow("missing-dep");
447447expect(createJiti).not.toHaveBeenCalled();
448448expect(fromSourceTransformer).not.toHaveBeenCalled();
449-expect(nativeStub).toHaveBeenCalledWith("/repo/dist/extensions/demo/api.js", {
450-allowWindows: true,
451-});
449+expect(nativeStub).toHaveBeenCalledWith(
450+"/repo/dist/extensions/demo/api.js",
451+expect.objectContaining({
452+allowWindows: true,
453+fallbackOnMissingDependency: true,
454+fallbackOnNativeError: true,
455+}),
456+);
452457expect(getPluginModuleLoaderStats()).toMatchObject({
453458calls: 1,
454459nativeHits: 0,
@@ -461,7 +466,6 @@ describe("getCachedPluginModuleLoader", () => {
461466it("falls back to source transform when the native-require helper declines", async () => {
462467const fromSourceTransformer = vi.fn(() => ({ fromSourceTransform: true }));
463468const createJiti = vi.fn(() => fromSourceTransformer);
464-vi.doMock("jiti", () => ({ createJiti }));
465469vi.doMock("./native-module-require.js", () => ({
466470isJavaScriptModulePath: () => true,
467471tryNativeRequireJavaScriptModule: () => ({ ok: false }),
@@ -481,6 +485,10 @@ describe("getCachedPluginModuleLoader", () => {
481485482486const result = loader("/repo/dist/extensions/demo/api.js") as { fromSourceTransform: boolean };
483487expect(result.fromSourceTransform).toBe(true);
488+expect(createJiti).toHaveBeenCalledWith(
489+"file:///repo/src/plugins/public-surface-loader.ts",
490+expect.objectContaining({ tryNative: true }),
491+);
484492expect(fromSourceTransformer).toHaveBeenCalledWith("/repo/dist/extensions/demo/api.js");
485493expect(getPluginModuleLoaderStats()).toMatchObject({
486494calls: 1,
@@ -496,7 +504,6 @@ describe("getCachedPluginModuleLoader", () => {
496504vi.spyOn(process, "platform", "get").mockReturnValue("win32");
497505const fromSourceTransformer = vi.fn(() => ({ fromSourceTransform: true }));
498506const createJiti = vi.fn(() => fromSourceTransformer);
499-vi.doMock("jiti", () => ({ createJiti }));
500507vi.doMock("./native-module-require.js", () => ({
501508isJavaScriptModulePath: () => true,
502509tryNativeRequireJavaScriptModule: () => ({ ok: false }),
@@ -529,7 +536,6 @@ describe("getCachedPluginModuleLoader", () => {
529536it("skips the native-require fast path when tryNative is explicitly false", async () => {
530537const fromSourceTransformer = vi.fn(() => ({ fromSourceTransform: true }));
531538const createJiti = vi.fn(() => fromSourceTransformer);
532-vi.doMock("jiti", () => ({ createJiti }));
533539const nativeStub = vi.fn(() => ({ ok: true, moduleExport: { fromNative: true } }));
534540vi.doMock("./native-module-require.js", () => ({
535541isJavaScriptModulePath: () => true,
@@ -570,7 +576,6 @@ describe("getCachedPluginModuleLoader", () => {
570576vi.spyOn(process, "platform", "get").mockReturnValue("win32");
571577const fromSourceTransformer = vi.fn(() => ({ fromSourceTransform: true }));
572578const createJiti = vi.fn(() => fromSourceTransformer);
573-vi.doMock("jiti", () => ({ createJiti }));
574579const nativeStub = vi.fn(() => ({ ok: true, moduleExport: { fromNative: true } }));
575580vi.doMock("./native-module-require.js", () => ({
576581isJavaScriptModulePath: () => true,
@@ -605,7 +610,6 @@ describe("getCachedPluginModuleLoader", () => {
605610it("forwards extra loader arguments through to the source-transform fallback", async () => {
606611const fromSourceTransformer = vi.fn(() => ({ fromSourceTransform: true }));
607612const createJiti = vi.fn(() => fromSourceTransformer);
608-vi.doMock("jiti", () => ({ createJiti }));
609613vi.doMock("./native-module-require.js", () => ({
610614isJavaScriptModulePath: () => true,
611615tryNativeRequireJavaScriptModule: () => ({ ok: false }),
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。