Skip to content

Commit dbb5436

Browse files
committed
[compiler] Add support for commonjs (facebook#34589)
We previously always generated import statements for any modules that had to be required, notably the `import {c} from 'react/compiler-runtime'` for the memo cache function. However, this obviously doesn't work when the source is using commonjs. Now we check the sourceType of the module and generate require() statements if the source type is 'script'. I initially explored using https://babeljs.io/docs/babel-helper-module-imports, but the API design was unfortunately not flexible enough for our use-case. Specifically, our pipeline is as follows: * Compile individual functions. Generate candidate imports, pre-allocating the local names for those imports. * If the file is compiled successfully, actually add the imports to the program. Ie we need to pre-allocate identifier names for the imports before we add them to the program — but that isn't supported by babel-helper-module-imports. So instead we generate our own require() calls if the sourceType is script. DiffTrain build for [8ad773b](facebook@8ad773b)
1 parent 0cc0ff9 commit dbb5436

35 files changed

+1765
-1889
lines changed

compiled/eslint-plugin-react-hooks/index.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51991,7 +51991,7 @@ function nameAnonymousFunctions(fn) {
5199151991
const functions = nameAnonymousFunctionsImpl(fn);
5199251992
function visit(node, prefix) {
5199351993
var _a, _b;
51994-
if (node.generatedName != null) {
51994+
if (node.generatedName != null && node.fn.nameHint == null) {
5199551995
const name = `${prefix}${node.generatedName}]`;
5199651996
node.fn.nameHint = name;
5199751997
node.fn.loweredFunc.func.nameHint = name;
@@ -52024,6 +52024,10 @@ function nameAnonymousFunctionsImpl(fn) {
5202452024
if (name != null && name.kind === 'named') {
5202552025
names.set(lvalue.identifier.id, name.value);
5202652026
}
52027+
const func = functions.get(value.place.identifier.id);
52028+
if (func != null) {
52029+
functions.set(lvalue.identifier.id, func);
52030+
}
5202752031
break;
5202852032
}
5202952033
case 'PropertyLoad': {
@@ -52051,6 +52055,7 @@ function nameAnonymousFunctionsImpl(fn) {
5205152055
const node = functions.get(value.value.identifier.id);
5205252056
const variableName = value.lvalue.place.identifier.name;
5205352057
if (node != null &&
52058+
node.generatedName == null &&
5205452059
variableName != null &&
5205552060
variableName.kind === 'named') {
5205652061
node.generatedName = variableName.value;
@@ -52081,7 +52086,7 @@ function nameAnonymousFunctionsImpl(fn) {
5208152086
continue;
5208252087
}
5208352088
const node = functions.get(arg.identifier.id);
52084-
if (node != null) {
52089+
if (node != null && node.generatedName == null) {
5208552090
const generatedName = fnArgCount > 1 ? `${calleeName}(arg${i})` : `${calleeName}()`;
5208652091
node.generatedName = generatedName;
5208752092
functions.delete(arg.identifier.id);
@@ -52095,7 +52100,7 @@ function nameAnonymousFunctionsImpl(fn) {
5209552100
continue;
5209652101
}
5209752102
const node = functions.get(attr.place.identifier.id);
52098-
if (node != null) {
52103+
if (node != null && node.generatedName == null) {
5209952104
const elementName = value.tag.kind === 'BuiltinTag'
5210052105
? value.tag.name
5210152106
: ((_b = names.get(value.tag.identifier.id)) !== null && _b !== void 0 ? _b : null);
@@ -53650,7 +53655,18 @@ function addImportsToProgram(path, programContext) {
5365053655
maybeExistingImports.pushContainer('specifiers', importSpecifiers);
5365153656
}
5365253657
else {
53653-
stmts.push(libExports$1.importDeclaration(importSpecifiers, libExports$1.stringLiteral(moduleName)));
53658+
if (path.node.sourceType === 'module') {
53659+
stmts.push(libExports$1.importDeclaration(importSpecifiers, libExports$1.stringLiteral(moduleName)));
53660+
}
53661+
else {
53662+
stmts.push(libExports$1.variableDeclaration('const', [
53663+
libExports$1.variableDeclarator(libExports$1.objectPattern(sortedImport.map(specifier => {
53664+
return libExports$1.objectProperty(libExports$1.identifier(specifier.imported), libExports$1.identifier(specifier.name));
53665+
})), libExports$1.callExpression(libExports$1.identifier('require'), [
53666+
libExports$1.stringLiteral(moduleName),
53667+
])),
53668+
]));
53669+
}
5365453670
}
5365553671
}
5365653672
path.unshiftContainer('body', stmts);
@@ -57359,8 +57375,8 @@ function getNodeWithoutReactNamespace(node) {
5735957375
}
5736057376
return node;
5736157377
}
57362-
function isUseEffectIdentifier(node) {
57363-
return node.type === 'Identifier' && node.name === 'useEffect';
57378+
function isEffectIdentifier(node) {
57379+
return node.type === 'Identifier' && (node.name === 'useEffect' || node.name === 'useLayoutEffect' || node.name === 'useInsertionEffect');
5736457380
}
5736557381
function isUseEffectEventIdentifier(node) {
5736657382
{
@@ -57660,7 +57676,7 @@ const rule = {
5766057676
reactHooks.push(node.callee);
5766157677
}
5766257678
const nodeWithoutNamespace = getNodeWithoutReactNamespace(node.callee);
57663-
if ((isUseEffectIdentifier(nodeWithoutNamespace) ||
57679+
if ((isEffectIdentifier(nodeWithoutNamespace) ||
5766457680
isUseEffectEventIdentifier(nodeWithoutNamespace)) &&
5766557681
node.arguments.length > 0) {
5766657682
lastEffect = node;

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cad813ac1ed5c04c6ccb390a3bdf5fedbbacaa45
1+
8ad773b1f342d20e4773c8d086028c6927445a22
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cad813ac1ed5c04c6ccb390a3bdf5fedbbacaa45
1+
8ad773b1f342d20e4773c8d086028c6927445a22

compiled/facebook-www/React-dev.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ __DEV__ &&
14191419
exports.useTransition = function () {
14201420
return resolveDispatcher().useTransition();
14211421
};
1422-
exports.version = "19.2.0-www-classic-cad813ac-20250923";
1422+
exports.version = "19.2.0-www-classic-8ad773b1-20250924";
14231423
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
14241424
"function" ===
14251425
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-dev.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ __DEV__ &&
14191419
exports.useTransition = function () {
14201420
return resolveDispatcher().useTransition();
14211421
};
1422-
exports.version = "19.2.0-www-modern-cad813ac-20250923";
1422+
exports.version = "19.2.0-www-modern-8ad773b1-20250924";
14231423
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
14241424
"function" ===
14251425
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-prod.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,4 +602,4 @@ exports.useSyncExternalStore = function (
602602
exports.useTransition = function () {
603603
return ReactSharedInternals.H.useTransition();
604604
};
605-
exports.version = "19.2.0-www-classic-cad813ac-20250923";
605+
exports.version = "19.2.0-www-classic-8ad773b1-20250924";

compiled/facebook-www/React-prod.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,4 +602,4 @@ exports.useSyncExternalStore = function (
602602
exports.useTransition = function () {
603603
return ReactSharedInternals.H.useTransition();
604604
};
605-
exports.version = "19.2.0-www-modern-cad813ac-20250923";
605+
exports.version = "19.2.0-www-modern-8ad773b1-20250924";

compiled/facebook-www/React-profiling.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ exports.useSyncExternalStore = function (
606606
exports.useTransition = function () {
607607
return ReactSharedInternals.H.useTransition();
608608
};
609-
exports.version = "19.2.0-www-classic-cad813ac-20250923";
609+
exports.version = "19.2.0-www-classic-8ad773b1-20250924";
610610
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
611611
"function" ===
612612
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-profiling.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ exports.useSyncExternalStore = function (
606606
exports.useTransition = function () {
607607
return ReactSharedInternals.H.useTransition();
608608
};
609-
exports.version = "19.2.0-www-modern-cad813ac-20250923";
609+
exports.version = "19.2.0-www-modern-8ad773b1-20250924";
610610
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
611611
"function" ===
612612
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

0 commit comments

Comments
 (0)