Skip to content
Open
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
21 changes: 21 additions & 0 deletions packages/metro-runtime/src/polyfills/__tests__/require-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ describe('require', () => {
createModuleSystem(moduleSystem, false, '');
expect(moduleSystem.__r).not.toBeUndefined();
expect(moduleSystem.__d).not.toBeUndefined();
expect(moduleSystem.__e).toBeUndefined();

const mockExports = {foo: 'bar'};
const mockFactory = jest
Expand Down Expand Up @@ -128,6 +129,26 @@ describe('require', () => {
expect(mockFactory.mock.calls[0][6]).toEqual([2, 3]);
});

test('exposes metroRequire as __e when runtime owner has already installed __r', () => {
const runtimeOwnerRequire = jest.fn();
moduleSystem.__r = runtimeOwnerRequire;

createModuleSystem(moduleSystem, false, '');

expect(moduleSystem.__r).toBe(runtimeOwnerRequire);
expect(moduleSystem.__e).not.toBeUndefined();
expect(moduleSystem.__e).not.toBe(runtimeOwnerRequire);
});

test('does not overwrite __r if already provided by the runtime owner', () => {
const runtimeOwnerRequire = jest.fn();
moduleSystem.__r = runtimeOwnerRequire;

createModuleSystem(moduleSystem, false, '');

expect(moduleSystem.__r).toBe(runtimeOwnerRequire);
});

test('properly prefixes __d with global prefix', () => {
createModuleSystem(moduleSystem, false, '__metro');
expect(moduleSystem.__d).toBeUndefined();
Expand Down
10 changes: 9 additions & 1 deletion packages/metro-runtime/src/polyfills/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ export type DefineFn = (
type VerboseModuleNameForDev = string;
type ModuleDefiner = (moduleId: ModuleID) => void;

global.__r = metroRequire as RequireFn;
if (!global.__r) {
// Entry point require function.
// Install only if the runtime owner hasn't already provided one.
global.__r = metroRequire as RequireFn;
} else {
// Install it as a separate binding.
// The runtime owner might e.g. use it to implement his own __r wrapper.
global.__e = metroRequire as RequireFn;
}
global[`${__METRO_GLOBAL_PREFIX__}__d`] = define as DefineFn;
global.__c = clear;
global.__registerSegment = registerSegment;
Expand Down