| 17 | return { |
| 18 | name: "babel-source", |
| 19 | resolveId(importee) { |
| 20 | if (importee === "@babel/runtime/regenerator") { |
| 21 | return path.join( |
| 22 | monorepoRoot, |
| 23 | "packages", |
| 24 | "babel-runtime", |
| 25 | "regenerator", |
| 26 | "index.js" |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | const matches = /^@babel\/(?<pkg>[^/]+)(?:\/lib\/(?<internal>.*))?$/.exec( |
| 31 | importee |
| 32 | ); |
| 33 | if (!matches) return null; |
| 34 | // @ts-expect-error pkg and internal are the names of capturing groups |
| 35 | const { pkg, internal } = matches.groups; |
| 36 | |
| 37 | // resolve babel package names to their src index file |
| 38 | const packageFolder = path.join(monorepoRoot, "packages", `babel-${pkg}`); |
| 39 | |
| 40 | let packageJsonSource; |
| 41 | try { |
| 42 | packageJsonSource = fs.readFileSync( |
| 43 | path.join(packageFolder, "package.json"), |
| 44 | "utf8" |
| 45 | ); |
| 46 | } catch (_) { |
| 47 | // Some Babel packages aren't in this repository |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | const packageJson = JSON.parse(packageJsonSource); |
| 52 | |
| 53 | const filename = internal |
| 54 | ? `src/${internal}` |
| 55 | : typeof packageJson.browser === "string" |
| 56 | ? packageJson.browser |
| 57 | : packageJson.main; |
| 58 | |
| 59 | let asJS = path.normalize( |
| 60 | path.join( |
| 61 | packageFolder, |
| 62 | // replace lib with src in the package.json entry |
| 63 | filename.replace(/^(\.\/)?lib\//, "src/") |
| 64 | ) |
| 65 | ); |
| 66 | if (!/\.[a-z]+$/.test(asJS)) asJS += ".js"; |
| 67 | const asTS = asJS.replace(/\.js$/, ".ts"); |
| 68 | |
| 69 | return fs.existsSync(asTS) ? asTS : asJS; |
| 70 | }, |
| 71 | }; |
| 72 | } |