[6a3a178] | 1 | // @flow
|
---|
| 2 |
|
---|
| 3 | import path from "path";
|
---|
| 4 | import debounce from "lodash.debounce";
|
---|
| 5 | import requireResolve from "resolve";
|
---|
| 6 |
|
---|
| 7 | const nativeRequireResolve = parseFloat(process.versions.node) >= 8.9;
|
---|
| 8 |
|
---|
| 9 | export function resolve(
|
---|
| 10 | dirname: string,
|
---|
| 11 | moduleName: string,
|
---|
| 12 | absoluteImports: boolean | string,
|
---|
| 13 | ): string {
|
---|
| 14 | if (absoluteImports === false) return moduleName;
|
---|
| 15 |
|
---|
| 16 | let basedir = dirname;
|
---|
| 17 | if (typeof absoluteImports === "string") {
|
---|
| 18 | basedir = path.resolve(basedir, absoluteImports);
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | let modulePackage, moduleNestedPath;
|
---|
| 22 |
|
---|
| 23 | let slash = moduleName.indexOf("/");
|
---|
| 24 | if (moduleName[0] === "@") {
|
---|
| 25 | slash = moduleName.indexOf("/", slash + 1);
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | if (slash === -1) {
|
---|
| 29 | modulePackage = moduleName;
|
---|
| 30 | moduleNestedPath = "";
|
---|
| 31 | } else {
|
---|
| 32 | modulePackage = moduleName.slice(0, slash);
|
---|
| 33 | moduleNestedPath = moduleName.slice(slash);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | try {
|
---|
| 37 | let pkg;
|
---|
| 38 | if (nativeRequireResolve) {
|
---|
| 39 | // $FlowIgnore
|
---|
| 40 | pkg = require.resolve(`${modulePackage}/package.json`, {
|
---|
| 41 | paths: [basedir],
|
---|
| 42 | });
|
---|
| 43 | } else {
|
---|
| 44 | pkg = requireResolve.sync(`${modulePackage}/package.json`, { basedir });
|
---|
| 45 | }
|
---|
| 46 | return path.dirname(pkg) + moduleNestedPath;
|
---|
| 47 | } catch (err) {
|
---|
| 48 | if (err.code !== "MODULE_NOT_FOUND") throw err;
|
---|
| 49 |
|
---|
| 50 | // $FlowIgnore
|
---|
| 51 | throw Object.assign(
|
---|
| 52 | new Error(`Failed to resolve "${moduleName}" relative to "${dirname}"`),
|
---|
| 53 | {
|
---|
| 54 | code: "BABEL_POLYFILL_NOT_FOUND",
|
---|
| 55 | polyfill: moduleName,
|
---|
| 56 | dirname,
|
---|
| 57 | },
|
---|
| 58 | );
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | export function has(basedir: string, name: string) {
|
---|
| 63 | try {
|
---|
| 64 | if (nativeRequireResolve) {
|
---|
| 65 | // $FlowIgnore
|
---|
| 66 | require.resolve(name, { paths: [basedir] });
|
---|
| 67 | } else {
|
---|
| 68 | requireResolve.sync(name, { basedir });
|
---|
| 69 | }
|
---|
| 70 | return true;
|
---|
| 71 | } catch {
|
---|
| 72 | return false;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | export function logMissing(missingDeps: Set<string>) {
|
---|
| 77 | if (missingDeps.size === 0) return;
|
---|
| 78 |
|
---|
| 79 | const deps = Array.from(missingDeps)
|
---|
| 80 | .sort()
|
---|
| 81 | .join(" ");
|
---|
| 82 |
|
---|
| 83 | console.warn(
|
---|
| 84 | "\nSome polyfills have been added but are not present in your dependencies.\n" +
|
---|
| 85 | "Please run one of the following commands:\n" +
|
---|
| 86 | `\tnpm install --save ${deps}\n` +
|
---|
| 87 | `\tyarn add ${deps}\n`,
|
---|
| 88 | );
|
---|
| 89 |
|
---|
| 90 | process.exitCode = 1;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | let allMissingDeps = new Set();
|
---|
| 94 |
|
---|
| 95 | const laterLogMissingDependencies = debounce(() => {
|
---|
| 96 | logMissing(allMissingDeps);
|
---|
| 97 | allMissingDeps = new Set();
|
---|
| 98 | }, 100);
|
---|
| 99 |
|
---|
| 100 | export function laterLogMissing(missingDeps: Set<string>) {
|
---|
| 101 | if (missingDeps.size === 0) return;
|
---|
| 102 |
|
---|
| 103 | missingDeps.forEach(name => allMissingDeps.add(name));
|
---|
| 104 | laterLogMissingDependencies();
|
---|
| 105 | }
|
---|