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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 14 deletions

View File

@ -1,5 +1,11 @@
"use strict"; "use strict";
const path = require("path");
function normalize(src) {
return src.replace(/\//, path.sep);
}
module.exports = function (api) { module.exports = function (api) {
const env = api.env(); const env = api.env();
@ -91,7 +97,9 @@ module.exports = function (api) {
"packages/*/test/fixtures", "packages/*/test/fixtures",
ignoreLib ? "packages/*/lib" : null, ignoreLib ? "packages/*/lib" : null,
"packages/babel-standalone/babel.js", "packages/babel-standalone/babel.js",
].filter(Boolean), ]
.filter(Boolean)
.map(normalize),
presets: [["@babel/env", envOpts]], presets: [["@babel/env", envOpts]],
plugins: [ plugins: [
// TODO: Use @babel/preset-flow when // TODO: Use @babel/preset-flow when
@ -113,14 +121,14 @@ module.exports = function (api) {
test: [ test: [
"packages/babel-parser", "packages/babel-parser",
"packages/babel-helper-validator-identifier", "packages/babel-helper-validator-identifier",
], ].map(normalize),
plugins: [ plugins: [
"babel-plugin-transform-charcodes", "babel-plugin-transform-charcodes",
["@babel/transform-for-of", { assumeArray: true }], ["@babel/transform-for-of", { assumeArray: true }],
], ],
}, },
{ {
test: ["./packages/babel-cli", "./packages/babel-core"], test: ["./packages/babel-cli", "./packages/babel-core"].map(normalize),
plugins: [ plugins: [
// Explicitly use the lazy version of CommonJS modules. // Explicitly use the lazy version of CommonJS modules.
convertESM convertESM
@ -129,11 +137,11 @@ module.exports = function (api) {
].filter(Boolean), ].filter(Boolean),
}, },
{ {
test: "./packages/babel-polyfill", test: normalize("./packages/babel-polyfill"),
presets: [["@babel/env", envOptsNoTargets]], presets: [["@babel/env", envOptsNoTargets]],
}, },
{ {
test: unambiguousSources, test: unambiguousSources.map(normalize),
sourceType: "unambiguous", sourceType: "unambiguous",
}, },
includeRegeneratorRuntime && { includeRegeneratorRuntime && {

View File

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