mirror of
https://github.com/facebook/react.git
synced 2026-02-23 20:23:02 +00:00
* Unify the way we fork modules * Replace rollup-plugin-alias with our own plugin This does exactly what we need and doesn't suffer from https://github.com/rollup/rollup-plugin-alias/issues/34. * Move the new plugin to its own file * Rename variable for consistency I settled on calling them "forks" since we already have a different concept of "shims". * Move fork config into its own file
50 lines
1.4 KiB
JavaScript
50 lines
1.4 KiB
JavaScript
/**
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
'use strict';
|
|
|
|
const path = require('path');
|
|
|
|
let resolveCache = new Map();
|
|
function useForks(forks) {
|
|
let resolvedForks = {};
|
|
Object.keys(forks).forEach(srcModule => {
|
|
const targetModule = forks[srcModule];
|
|
resolvedForks[require.resolve(srcModule)] = require.resolve(targetModule);
|
|
});
|
|
return {
|
|
resolveId(importee, importer) {
|
|
if (!importer || !importee) {
|
|
return null;
|
|
}
|
|
let resolvedImportee = null;
|
|
let cacheKey = `${importer}:::${importee}`;
|
|
if (resolveCache.has(cacheKey)) {
|
|
// Avoid hitting file system if possible.
|
|
resolvedImportee = resolveCache.get(cacheKey);
|
|
} else {
|
|
try {
|
|
resolvedImportee = require.resolve(importee, {
|
|
paths: [path.dirname(importer)],
|
|
});
|
|
} catch (err) {
|
|
// Not our fault, let Rollup fail later.
|
|
}
|
|
if (resolvedImportee) {
|
|
resolveCache.set(cacheKey, resolvedImportee);
|
|
}
|
|
}
|
|
if (resolvedImportee && resolvedForks.hasOwnProperty(resolvedImportee)) {
|
|
// We found a fork!
|
|
return resolvedForks[resolvedImportee];
|
|
}
|
|
return null;
|
|
},
|
|
};
|
|
}
|
|
|
|
module.exports = useForks;
|