Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with the new fork plugin #11723

Merged
merged 1 commit into from
Nov 30, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions scripts/rollup/plugins/use-forks-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,63 @@
'use strict';

const path = require('path');
const semver = require('semver');

function resolveRelatively(importee, importer) {
if (semver.gte(process.version, '8.9.0')) {
return require.resolve(importee, {
paths: [path.dirname(importer)],
});
} else {
// `paths` argument is not available in older Node.
// This works though.
// https://github.com/nodejs/node/issues/5963
const Module = require('module');
return Module._findPath(importee, [
path.dirname(importer),
...module.paths,
]);
}
}

let resolveCache = new Map();
function useForks(forks) {
let resolvedForks = {};
let resolvedForks = new Map();
Object.keys(forks).forEach(srcModule => {
const targetModule = forks[srcModule];
resolvedForks[require.resolve(srcModule)] = require.resolve(targetModule);
resolvedForks.set(
require.resolve(srcModule),
require.resolve(targetModule)
);
});
return {
resolveId(importee, importer) {
if (!importer || !importee) {
return null;
}
if (importee.startsWith('\u0000')) {
// Internal Rollup reference, ignore.
// Passing that to Node file functions can fatal.
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)],
});
resolvedImportee = resolveRelatively(importee, importer);
} catch (err) {
// Not our fault, let Rollup fail later.
}
if (resolvedImportee) {
resolveCache.set(cacheKey, resolvedImportee);
}
}
if (resolvedImportee && resolvedForks.hasOwnProperty(resolvedImportee)) {
if (resolvedImportee && resolvedForks.has(resolvedImportee)) {
// We found a fork!
return resolvedForks[resolvedImportee];
return resolvedForks.get(resolvedImportee);
}
return null;
},
Expand Down