|
| 1 | +import path from 'path'; |
| 2 | +import { resolvePath } from 'babel-plugin-module-resolver'; |
| 3 | +import { cloneDeep } from 'lodash'; |
| 4 | + |
| 5 | +import { |
| 6 | + isFromCurrentRepo, |
| 7 | + findRepoRootPath, |
| 8 | + getLinkedBabelrc, |
| 9 | + getLinkedPackageJSON, |
| 10 | + getAliases, |
| 11 | + getFilesRepo, |
| 12 | +} from './utils'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Get the new babel config with optimized settings for linked aliases |
| 16 | + */ |
| 17 | +export default function packagebind (babelConfig) { |
| 18 | + /** We are cloning babelrc here so we can mutate it directly to keep logic simpler */ |
| 19 | + const cloneBabelrc = cloneDeep(babelConfig); |
| 20 | + const { plugins = [] } = cloneBabelrc; |
| 21 | + |
| 22 | + const moduleResolver = plugins.find( |
| 23 | + plugin => Array.isArray(plugin) && plugin[0] === 'module-resolver' |
| 24 | + ); |
| 25 | + |
| 26 | + const aliases = getAliases(plugins); |
| 27 | + |
| 28 | + const linkedRepos = Object.keys(aliases).filter((aliasKey) => { |
| 29 | + const aliasPath = path.resolve(aliases[aliasKey]); |
| 30 | + return isFromCurrentRepo(aliasPath); |
| 31 | + }); |
| 32 | + |
| 33 | + const linkedReposMeta = linkedRepos.map((name) => { |
| 34 | + const rootPath = findRepoRootPath(aliases[name]); |
| 35 | + const babelConfig = getLinkedBabelrc(rootPath); |
| 36 | + const packageJSON = getLinkedPackageJSON(rootPath); |
| 37 | + return { |
| 38 | + name, |
| 39 | + rootPath, |
| 40 | + babelConfig, |
| 41 | + packageJSON, |
| 42 | + }; |
| 43 | + }); |
| 44 | + |
| 45 | + /** TODO: Add plugins from the linked packages as well. |
| 46 | + * For now that is not required as the plugins added on this repo is super set of all plugin used on linked repos |
| 47 | + * For now just get the alias from those babel files |
| 48 | + * */ |
| 49 | + |
| 50 | + const aliasMaps = {}; |
| 51 | + |
| 52 | + linkedReposMeta.forEach((meta) => { |
| 53 | + const { babelConfig, name, rootPath, packageJSON } = meta; |
| 54 | + |
| 55 | + aliasMaps[name] = {}; |
| 56 | + |
| 57 | + // Add alias from linked babelConfig |
| 58 | + if (babelConfig) { |
| 59 | + const aliases = getAliases(babelConfig.plugins); |
| 60 | + if (aliases) { |
| 61 | + aliasMaps[name] = Object.entries(aliases).reduce((updatedAliases, [key, value]) => { |
| 62 | + updatedAliases[key] = path.resolve(rootPath, value); |
| 63 | + return updatedAliases; |
| 64 | + }, aliasMaps[name]); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Add peer dependencies to aliasMaps to make sure the dependency conflict doesn't happens |
| 69 | + if (packageJSON.peerDependencies) { |
| 70 | + Object.keys(packageJSON.peerDependencies).forEach((module) => { |
| 71 | + aliasMaps[name][module] = path.resolve(rootPath, 'node_modules', module); |
| 72 | + }); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + moduleResolver[1].resolvePath = (sourcePath, currentFile, opts) => { |
| 77 | + opts = cloneDeep(opts); |
| 78 | + const repo = getFilesRepo(currentFile, linkedReposMeta); |
| 79 | + if (repo !== 'currentRepo' && aliasMaps[repo]) { |
| 80 | + opts.alias = { ...opts.alias, ...aliasMaps[repo] }; |
| 81 | + } |
| 82 | + const realPath = resolvePath(sourcePath, currentFile, opts); |
| 83 | + return realPath; |
| 84 | + }; |
| 85 | + |
| 86 | + return cloneBabelrc; |
| 87 | +} |
0 commit comments