Use native ESM for dev scripts (#12296)

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Karan Sapolia
2021-01-30 23:06:21 +05:30
committed by GitHub
parent f8fe8eaab1
commit b63be942ce
39 changed files with 205 additions and 193 deletions

View File

@@ -1,20 +1,27 @@
const path = require("path");
const fs = require("fs");
const dirname = path.join(__dirname, "..");
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const monorepoRoot = path.join(
path.dirname(fileURLToPath(import.meta.url)),
".."
);
const BABEL_SRC_REGEXP =
path.sep === "/"
? /packages\/(babel-[^/]+)\/src\//
: /packages\\(babel-[^\\]+)\\src\\/;
module.exports = function () {
export default function () {
return {
name: "babel-source",
load(id) {
const matches = id.match(BABEL_SRC_REGEXP);
if (matches) {
// check if browser field exists for this file and replace
const packageFolder = path.join(dirname, "packages", matches[1]);
const packageFolder = path.join(monorepoRoot, "packages", matches[1]);
const packageJson = require(path.join(packageFolder, "package.json"));
if (
@@ -46,7 +53,7 @@ module.exports = function () {
resolveId(importee) {
if (importee === "@babel/runtime/regenerator") {
return path.join(
dirname,
monorepoRoot,
"packages",
"babel-runtime",
"regenerator",
@@ -61,7 +68,7 @@ module.exports = function () {
const { pkg, internal } = matches.groups;
// resolve babel package names to their src index file
const packageFolder = path.join(dirname, "packages", `babel-${pkg}`);
const packageFolder = path.join(monorepoRoot, "packages", `babel-${pkg}`);
let packageJsonSource;
try {
@@ -98,4 +105,4 @@ module.exports = function () {
}
},
};
};
}