|
12 | 12 |
|
13 | 13 | const {transformSync} = require('@babel/core'); |
14 | 14 | const fs = require('node:fs'); |
| 15 | +const vm = require('node:vm'); |
15 | 16 |
|
16 | 17 | function createModule( |
17 | 18 | moduleSystem, |
@@ -128,11 +129,17 @@ describe('require', () => { |
128 | 129 | expect(mockFactory.mock.calls[0][6]).toEqual([2, 3]); |
129 | 130 | }); |
130 | 131 |
|
131 | | - test('properly prefixes __d with global prefix', () => { |
132 | | - createModuleSystem(moduleSystem, false, '__metro'); |
133 | | - expect(moduleSystem.__d).toBeUndefined(); |
134 | | - expect(moduleSystem.__metro__d).not.toBeUndefined(); |
135 | | - }); |
| 132 | + test.each([['__r'], ['__d'], ['__c'], ['__registerSegment'], ['__accept']])( |
| 133 | + 'properly prefixes %s with global prefix', |
| 134 | + property => { |
| 135 | + const prefix = '__metro'; |
| 136 | + |
| 137 | + createModuleSystem(moduleSystem, true, prefix); |
| 138 | + |
| 139 | + expect(moduleSystem[property]).toBeUndefined(); |
| 140 | + expect(moduleSystem[`${prefix}${property}`]).not.toBeUndefined(); |
| 141 | + }, |
| 142 | + ); |
136 | 143 |
|
137 | 144 | test('works with Random Access Modules (RAM) bundles', () => { |
138 | 145 | const mockExports = {foo: 'bar'}; |
@@ -748,7 +755,7 @@ describe('require', () => { |
748 | 755 | const warn = console.warn; |
749 | 756 | console.warn = jest.fn(); |
750 | 757 |
|
751 | | - moduleSystem.__r(0); |
| 758 | + moduleSystem.__customPrefix__r(0); |
752 | 759 | expect(console.warn).toHaveBeenCalledTimes(0); |
753 | 760 | console.warn = warn; |
754 | 761 | }); |
@@ -1000,6 +1007,93 @@ describe('require', () => { |
1000 | 1007 | }); |
1001 | 1008 | }); |
1002 | 1009 |
|
| 1010 | + describe('several module systems in one realm', () => { |
| 1011 | + // Evaluates a prelude and the module system as a bundle does, so that |
| 1012 | + // `__METRO_GLOBAL_PREFIX__` is a global that each bundle reassigns, as |
| 1013 | + // Module Federation's host and remote bundles do. |
| 1014 | + function evaluateBundleRuntime(context, globalPrefix) { |
| 1015 | + vm.runInContext( |
| 1016 | + `var __METRO_GLOBAL_PREFIX__ = ${JSON.stringify(globalPrefix)};`, |
| 1017 | + context, |
| 1018 | + ); |
| 1019 | + vm.runInContext( |
| 1020 | + `(function (global, __DEV__) {\n${moduleSystemCode}\n})(globalThis, true);`, |
| 1021 | + context, |
| 1022 | + ); |
| 1023 | + } |
| 1024 | + |
| 1025 | + test('keeps the globals of each module system separate', () => { |
| 1026 | + const context = vm.createContext({console}); |
| 1027 | + evaluateBundleRuntime(context, 'host'); |
| 1028 | + evaluateBundleRuntime(context, 'remote'); |
| 1029 | + |
| 1030 | + createModule( |
| 1031 | + context, |
| 1032 | + 0, |
| 1033 | + 'host.js', |
| 1034 | + (g, r, i, a, module) => { |
| 1035 | + module.exports = 'host'; |
| 1036 | + }, |
| 1037 | + [], |
| 1038 | + 'host', |
| 1039 | + ); |
| 1040 | + createModule( |
| 1041 | + context, |
| 1042 | + 0, |
| 1043 | + 'remote.js', |
| 1044 | + (g, r, i, a, module) => { |
| 1045 | + module.exports = 'remote'; |
| 1046 | + }, |
| 1047 | + [], |
| 1048 | + 'remote', |
| 1049 | + ); |
| 1050 | + |
| 1051 | + expect(context.host__r(0)).toBe('host'); |
| 1052 | + expect(context.remote__r(0)).toBe('remote'); |
| 1053 | + for (const name of ['__r', '__d', '__c', '__registerSegment']) { |
| 1054 | + expect(context[`host${name}`]).not.toBe(context[`remote${name}`]); |
| 1055 | + } |
| 1056 | + }); |
| 1057 | + |
| 1058 | + test('applies a hot update to the module system that defines the module', () => { |
| 1059 | + const context = vm.createContext({console}); |
| 1060 | + const versionedFactory = version => (g, r, i, a, module) => { |
| 1061 | + module.exports = version; |
| 1062 | + module.hot.accept(); |
| 1063 | + }; |
| 1064 | + evaluateBundleRuntime(context, 'host'); |
| 1065 | + createModule( |
| 1066 | + context, |
| 1067 | + 0, |
| 1068 | + 'host.js', |
| 1069 | + versionedFactory('HostV1'), |
| 1070 | + [], |
| 1071 | + 'host', |
| 1072 | + ); |
| 1073 | + expect(context.host__r(0)).toBe('HostV1'); |
| 1074 | + |
| 1075 | + // A remote's prelude reassigns `__METRO_GLOBAL_PREFIX__`, and its module |
| 1076 | + // system defines a module with the same ID. |
| 1077 | + evaluateBundleRuntime(context, 'remote'); |
| 1078 | + createModule( |
| 1079 | + context, |
| 1080 | + 0, |
| 1081 | + 'remote.js', |
| 1082 | + versionedFactory('RemoteV1'), |
| 1083 | + [], |
| 1084 | + 'remote', |
| 1085 | + ); |
| 1086 | + expect(context.remote__r(0)).toBe('RemoteV1'); |
| 1087 | + |
| 1088 | + // An HMR update for the host redefines its module with inverse |
| 1089 | + // dependencies. |
| 1090 | + context.host__d(versionedFactory('HostV2'), 0, [], 'host.js', {0: []}); |
| 1091 | + |
| 1092 | + expect(context.host__r(0)).toBe('HostV2'); |
| 1093 | + expect(context.remote__r(0)).toBe('RemoteV1'); |
| 1094 | + }); |
| 1095 | + }); |
| 1096 | + |
1003 | 1097 | describe('hot reloading', () => { |
1004 | 1098 | test('is disabled in production', () => { |
1005 | 1099 | createModuleSystem(moduleSystem, false, ''); |
|
0 commit comments