Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions babel.config.js
@@ -1,5 +1,11 @@
"use strict";

const path = require("path");

function normalize(src) {
return src.replace(/\//, path.sep);
}

module.exports = function (api) {
const env = api.env();

Expand Down Expand Up @@ -93,7 +99,9 @@ module.exports = function (api) {
"packages/*/test/fixtures",
ignoreLib ? "packages/*/lib" : null,
"packages/babel-standalone/babel.js",
].filter(Boolean),
]
.filter(Boolean)
.map(normalize),
presets: [["@babel/env", envOpts]],
plugins: [
// TODO: Use @babel/preset-flow when
Expand All @@ -115,14 +123,14 @@ module.exports = function (api) {
test: [
"packages/babel-parser",
"packages/babel-helper-validator-identifier",
],
].map(normalize),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Babel 8 I'd like to normalize the paths internally in @babel/core

plugins: [
"babel-plugin-transform-charcodes",
["@babel/transform-for-of", { assumeArray: true }],
],
},
{
test: ["./packages/babel-cli", "./packages/babel-core"],
test: ["./packages/babel-cli", "./packages/babel-core"].map(normalize),
plugins: [
// Explicitly use the lazy version of CommonJS modules.
convertESM
Expand All @@ -131,11 +139,11 @@ module.exports = function (api) {
].filter(Boolean),
},
{
test: "./packages/babel-polyfill",
test: normalize("./packages/babel-polyfill"),
presets: [["@babel/env", envOptsNoTargets]],
},
{
test: unambiguousSources,
test: unambiguousSources.map(normalize),
sourceType: "unambiguous",
},
includeRegeneratorRuntime && {
Expand Down
27 changes: 18 additions & 9 deletions scripts/rollup-plugin-babel-source.js
Expand Up @@ -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]);
Expand All @@ -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"
);
}
Expand Down Expand Up @@ -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/")
)
);
},
};
Expand Down