fix build config to work the same when running on windows (#11688)

This commit is contained in:
Bogdan Savluk
2020-07-30 20:11:58 +02:00
committed by GitHub
parent 2ac49ba7c4
commit 32e7bb4027
2 changed files with 31 additions and 14 deletions

View File

@@ -2,11 +2,16 @@ const path = require("path");
const fs = require("fs");
const dirname = path.join(__dirname, "..");
const BABEL_SRC_REGEXP =
path.sep === "/"
? /packages\/(babel-[^/]+)\/src\//
: /packages\\(babel-[^\\]+)\\src\\/;
module.exports = function () {
return {
name: "babel-source",
load(id) {
const matches = id.match(/packages\/(babel-[^/]+)\/src\//);
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]);
@@ -16,18 +21,20 @@ module.exports = function () {
packageJson["browser"] &&
typeof packageJson["browser"] === "object"
) {
for (let nodeFile in packageJson["browser"]) {
for (const nodeFile in packageJson["browser"]) {
const browserFile = packageJson["browser"][nodeFile].replace(
/^(\.\/)?lib\//,
"src/"
);
nodeFile = nodeFile.replace(/^(\.\/)?lib\//, "src/");
if (id.endsWith(nodeFile)) {
const nodeFileSrc = path.normalize(
nodeFile.replace(/^(\.\/)?lib\//, "src/")
);
if (id.endsWith(nodeFileSrc)) {
if (browserFile === false) {
return "";
}
return fs.readFileSync(
path.join(packageFolder, browserFile),
path.join(packageFolder, path.normalize(browserFile)),
"UTF-8"
);
}
@@ -74,10 +81,12 @@ module.exports = function () {
? packageJson["browser"]
: packageJson["main"];
return path.join(
packageFolder,
// replace lib with src in the package.json entry
filename.replace(/^(\.\/)?lib\//, "src/")
return path.normalize(
path.join(
packageFolder,
// replace lib with src in the package.json entry
filename.replace(/^(\.\/)?lib\//, "src/")
)
);
},
};