react/scripts/rollup/modules.js
Sebastian Markbåge dd9117e313
[Flight] Source Map Actions in Reference Node Loader Transforms (#30755)
Follow up to #30741.

This is just for the reference Webpack implementation.

If there is a source map associated with a Node ESM loader, we generate
new source map entries for every `registerServerReference` call.

To avoid messing too much with it, this doesn't rewrite the original
mappings. It just reads them while finding each of the exports in the
original mappings. We need to read all since whatever we append at the
end is relative. Then we just generate new appended entries at the end.

For the location I picked the location of the local name identifier.
Since that's the name of the function and that gives us a source map
name index. It means it jumps to the name rather than the beginning of
the function declaration. It could be made more clever like finding a
local function definition if it is reexported. We could also point to
the line/column of the function declaration rather than the identifier
but point to the name index of the identifier name.

Now jumping to definition works in the fixture.

<img width="574" alt="Screenshot 2024-08-20 at 2 49 07 PM"
src="https://github.com/user-attachments/assets/7710f0e6-2cee-4aad-8d4c-ae985f8289eb">

Unfortunately this technique doesn't seem to work in Firefox nor Safari.
They don't apply the source map for jumping to the definition.
2024-08-21 09:52:17 -04:00

93 lines
3.0 KiB
JavaScript

'use strict';
const forks = require('./forks');
// For any external that is used in a DEV-only condition, explicitly
// specify whether it has side effects during import or not. This lets
// us know whether we can safely omit them when they are unused.
const HAS_NO_SIDE_EFFECTS_ON_IMPORT = false;
// const HAS_SIDE_EFFECTS_ON_IMPORT = true;
const importSideEffects = Object.freeze({
fs: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
'fs/promises': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
path: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
stream: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
'prop-types/checkPropTypes': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface':
HAS_NO_SIDE_EFFECTS_ON_IMPORT,
scheduler: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
react: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
'react-dom/server': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
'react/jsx-dev-runtime': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
'react-dom': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
url: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
ReactNativeInternalFeatureFlags: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
'webpack-sources/lib/helpers/createMappingsSerializer.js':
HAS_NO_SIDE_EFFECTS_ON_IMPORT,
'webpack-sources/lib/helpers/readMappings.js': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
});
// Bundles exporting globals that other modules rely on.
const knownGlobals = Object.freeze({
react: 'React',
'react-dom': 'ReactDOM',
'react-dom/server': 'ReactDOMServer',
scheduler: 'Scheduler',
'scheduler/unstable_mock': 'SchedulerMock',
ReactNativeInternalFeatureFlags: 'ReactNativeInternalFeatureFlags',
});
// Given ['react'] in bundle externals, returns { 'react': 'React' }.
function getPeerGlobals(externals, bundleType) {
const peerGlobals = {};
externals.forEach(name => {
peerGlobals[name] = knownGlobals[name];
});
return peerGlobals;
}
// Determines node_modules packages that are safe to assume will exist.
function getDependencies(bundleType, entry) {
// Replaces any part of the entry that follow the package name (like
// "/server" in "react-dom/server") by the path to the package settings
const packageJson = require(entry.replace(/(\/.*)?$/, '/package.json'));
// Both deps and peerDeps are assumed as accessible.
return Array.from(
new Set([
...Object.keys(packageJson.dependencies || {}),
...Object.keys(packageJson.peerDependencies || {}),
])
);
}
// Hijacks some modules for optimization and integration reasons.
function getForks(bundleType, entry, moduleType, bundle) {
const forksForBundle = {};
Object.keys(forks).forEach(srcModule => {
const dependencies = getDependencies(bundleType, entry);
const targetModule = forks[srcModule](
bundleType,
entry,
dependencies,
moduleType,
bundle
);
if (targetModule === null) {
return;
}
forksForBundle[srcModule] = targetModule;
});
return forksForBundle;
}
function getImportSideEffects() {
return importSideEffects;
}
module.exports = {
getImportSideEffects,
getPeerGlobals,
getDependencies,
getForks,
};