Skip to content

Commit 8669b9e

Browse files
committed
[compiler] Add support for commonjs (#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](8ad773b)
1 parent 2b6b1b4 commit 8669b9e

35 files changed

+98
-87
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53655,7 +53655,18 @@ function addImportsToProgram(path, programContext) {
5365553655
maybeExistingImports.pushContainer('specifiers', importSpecifiers);
5365653656
}
5365753657
else {
53658-
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+
}
5365953670
}
5366053671
}
5366153672
path.unshiftContainer('body', stmts);

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2c6d92fd80ec6917cb7387dbb771e35e82b0126d
1+
8ad773b1f342d20e4773c8d086028c6927445a22
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2c6d92fd80ec6917cb7387dbb771e35e82b0126d
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-2c6d92fd-20250924";
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-2c6d92fd-20250924";
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-2c6d92fd-20250924";
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-2c6d92fd-20250924";
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-2c6d92fd-20250924";
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-2c6d92fd-20250924";
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 &&

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20224,10 +20224,10 @@ __DEV__ &&
2022420224
(function () {
2022520225
var internals = {
2022620226
bundleType: 1,
20227-
version: "19.2.0-www-classic-2c6d92fd-20250924",
20227+
version: "19.2.0-www-classic-8ad773b1-20250924",
2022820228
rendererPackageName: "react-art",
2022920229
currentDispatcherRef: ReactSharedInternals,
20230-
reconcilerVersion: "19.2.0-www-classic-2c6d92fd-20250924"
20230+
reconcilerVersion: "19.2.0-www-classic-8ad773b1-20250924"
2023120231
};
2023220232
internals.overrideHookState = overrideHookState;
2023320233
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -20261,7 +20261,7 @@ __DEV__ &&
2026120261
exports.Shape = Shape;
2026220262
exports.Surface = Surface;
2026320263
exports.Text = Text;
20264-
exports.version = "19.2.0-www-classic-2c6d92fd-20250924";
20264+
exports.version = "19.2.0-www-classic-8ad773b1-20250924";
2026520265
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
2026620266
"function" ===
2026720267
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

0 commit comments

Comments
 (0)