|
| 1 | +// reuse `@babel/types` from `metro` |
| 2 | +const metroPath = require.resolve('metro'); |
| 3 | +const babelTypesPath = require.resolve('@babel/types', { paths: [metroPath] }); |
| 4 | +const t = require(babelTypesPath); |
| 5 | + |
| 6 | +const METRO_PREFIX = '__METRO_GLOBAL_PREFIX__'; |
| 7 | +const GLOBAL_NAMES_TO_PREFIX = ['__r', '__c', '__registerSegment', '__accept']; |
| 8 | +const REFRESH_SYMBOLS = ['$RefreshReg$', '$RefreshSig$']; |
| 9 | + |
| 10 | +/** |
| 11 | + * - global.__r = metroRequire; |
| 12 | + * + global[`${__METRO_GLOBAL_PREFIX__}__r`] = metroRequire; |
| 13 | + */ |
| 14 | +function prefixGlobalNames(path) { |
| 15 | + const { object, property, computed } = path.node; |
| 16 | + |
| 17 | + if ( |
| 18 | + t.isIdentifier(object, { name: 'global' }) && |
| 19 | + t.isIdentifier(property) && |
| 20 | + !computed && |
| 21 | + GLOBAL_NAMES_TO_PREFIX.includes(property.name) |
| 22 | + ) { |
| 23 | + path.replaceWith( |
| 24 | + t.memberExpression( |
| 25 | + t.identifier('global'), |
| 26 | + t.templateLiteral( |
| 27 | + [ |
| 28 | + t.templateElement({ raw: '', cooked: '' }), |
| 29 | + t.templateElement( |
| 30 | + { raw: property.name, cooked: property.name }, |
| 31 | + true |
| 32 | + ), |
| 33 | + ], |
| 34 | + [t.identifier(METRO_PREFIX)] |
| 35 | + ), |
| 36 | + true |
| 37 | + ) |
| 38 | + ); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * - global.$RefreshReg$ = () => {}; |
| 44 | + * - global.$RefreshSig$ = () => (type) => type; |
| 45 | + * + global.$RefreshReg$ = global.$RefreshReg$ ?? (() => {}); |
| 46 | + * + global.$RefreshSig$ = global.$RefreshSig$ ?? (() => type => type); |
| 47 | + */ |
| 48 | +function defaultRefreshSymbols(path, state) { |
| 49 | + const { node } = path; |
| 50 | + |
| 51 | + if ( |
| 52 | + t.isMemberExpression(node.left) && |
| 53 | + t.isIdentifier(node.left.object, { name: 'global' }) && |
| 54 | + t.isIdentifier(node.left.property) && |
| 55 | + REFRESH_SYMBOLS.includes(node.left.property.name) |
| 56 | + ) { |
| 57 | + const propName = node.left.property.name; |
| 58 | + |
| 59 | + if (state.hasTransformed[propName]) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const globalProp = t.memberExpression( |
| 64 | + t.identifier('global'), |
| 65 | + t.identifier(node.left.property.name) |
| 66 | + ); |
| 67 | + |
| 68 | + path.replaceWith( |
| 69 | + t.assignmentExpression( |
| 70 | + '=', |
| 71 | + globalProp, |
| 72 | + t.logicalExpression('??', globalProp, node.right) |
| 73 | + ) |
| 74 | + ); |
| 75 | + state.hasTransformed[propName] = true; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * - RefreshRuntime.register(type, moduleId + " " + id); |
| 81 | + * + RefreshRuntime.register(type, __METRO_GLOBAL_PREFIX__ + ' ' + moduleId + ' ' + id); |
| 82 | + */ |
| 83 | +function prefixModuleIDs(path) { |
| 84 | + const { callee, arguments: args } = path.node; |
| 85 | + const isRegisterExports = t.isIdentifier(callee, { |
| 86 | + name: 'registerExportsForReactRefresh', |
| 87 | + }); |
| 88 | + const isRefreshRuntimeRegister = |
| 89 | + t.isMemberExpression(callee) && |
| 90 | + t.isIdentifier(callee.object, { name: 'RefreshRuntime' }) && |
| 91 | + t.isIdentifier(callee.property, { name: 'register' }); |
| 92 | + |
| 93 | + if (!isRegisterExports && !isRefreshRuntimeRegister) return; |
| 94 | + |
| 95 | + const lastArgIndex = args.length - 1; |
| 96 | + const lastArg = args[lastArgIndex]; |
| 97 | + |
| 98 | + args[lastArgIndex] = t.binaryExpression( |
| 99 | + '+', |
| 100 | + t.identifier(METRO_PREFIX), |
| 101 | + t.binaryExpression('+', t.stringLiteral(' '), lastArg) |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * - global[__METRO_GLOBAL_PREFIX__ + '__ReactRefresh'] || metroRequire.Refresh |
| 107 | + * + global[global.__METRO_GLOBAL_PREFIX__ + '__ReactRefresh'] || |
| 108 | + * + metroRequire.Refresh |
| 109 | + */ |
| 110 | +function patchRequireRefresh(path) { |
| 111 | + const funcPath = path.findParent( |
| 112 | + (p) => |
| 113 | + (p.isFunctionDeclaration() || p.isFunctionExpression()) && |
| 114 | + p.node.id?.name === 'requireRefresh' |
| 115 | + ); |
| 116 | + |
| 117 | + if (!funcPath) return; |
| 118 | + |
| 119 | + const newReturn = t.logicalExpression( |
| 120 | + '||', |
| 121 | + t.memberExpression( |
| 122 | + t.identifier('global'), |
| 123 | + t.binaryExpression( |
| 124 | + '+', |
| 125 | + t.memberExpression(t.identifier('global'), t.identifier(METRO_PREFIX)), |
| 126 | + t.stringLiteral('__ReactRefresh') |
| 127 | + ), |
| 128 | + true |
| 129 | + ), |
| 130 | + t.memberExpression(t.identifier('metroRequire'), t.identifier('Refresh')) |
| 131 | + ); |
| 132 | + |
| 133 | + path.get('argument').replaceWith(newReturn); |
| 134 | +} |
| 135 | + |
| 136 | +function metroPatchRequireBabelPlugin() { |
| 137 | + return { |
| 138 | + name: 'metro-patch-require', |
| 139 | + visitor: { |
| 140 | + Program: { |
| 141 | + enter(_, state) { |
| 142 | + // Transform only require.js from metro-runtime |
| 143 | + state.shouldTransform = state.file.opts.filename.includes( |
| 144 | + 'metro-runtime/src/polyfills/require.js' |
| 145 | + ); |
| 146 | + // Perform refreshSymbols transformation only once |
| 147 | + // Because it is referenced in multiple places |
| 148 | + state.hasTransformed = { |
| 149 | + $RefreshReg$: false, |
| 150 | + $RefreshSig$: false, |
| 151 | + }; |
| 152 | + }, |
| 153 | + }, |
| 154 | + MemberExpression(path, state) { |
| 155 | + if (!state.shouldTransform) return; |
| 156 | + prefixGlobalNames(path); |
| 157 | + }, |
| 158 | + AssignmentExpression(path, state) { |
| 159 | + if (!state.shouldTransform) return; |
| 160 | + defaultRefreshSymbols(path, state); |
| 161 | + }, |
| 162 | + CallExpression(path, state) { |
| 163 | + if (!state.shouldTransform) return; |
| 164 | + prefixModuleIDs(path); |
| 165 | + }, |
| 166 | + ReturnStatement(path, state) { |
| 167 | + if (!state.shouldTransform) return; |
| 168 | + patchRequireRefresh(path); |
| 169 | + }, |
| 170 | + }, |
| 171 | + }; |
| 172 | +} |
| 173 | + |
| 174 | +module.exports = metroPatchRequireBabelPlugin; |
0 commit comments