Use native ESM for dev scripts (#12296)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
@@ -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 () {
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user