babel/scripts/rollup-plugin-babel-source.js
Nicolò Ribaudo a677d59a63
Archive syntax plugins enabled by default (#10820)
* Remove syntax plugins enabled by default

Moved to babel-archive: babel/babel-archive@8f23ddce03

- `@babel/plugin-syntax-async-generators`
- `@babel/plugin-syntax-bigint`
- `@babel/plugin-syntax-dynamic-import`
- `@babel/plugin-syntax-json-strings`
- `@babel/plugin-syntax-nullish-coalescing-operator`
- `@babel/plugin-syntax-object-rest-spread`
- `@babel/plugin-syntax-optional-catch-binding`
- `@babel/plugin-syntax-optional-chaining`

* Fix build script

* Fix jest config

* Remove archived plugins from tests
2020-01-13 00:31:48 +01:00

85 lines
2.3 KiB
JavaScript

const path = require("path");
const fs = require("fs");
const dirname = path.join(__dirname, "..");
module.exports = function() {
return {
name: "babel-source",
load(id) {
const matches = id.match(/packages\/(babel-[^/]+)\/src\//);
if (matches) {
// check if browser field exists for this file and replace
const packageFolder = path.join(dirname, "packages", matches[1]);
const packageJson = require(path.join(packageFolder, "package.json"));
if (
packageJson["browser"] &&
typeof packageJson["browser"] === "object"
) {
for (let nodeFile in packageJson["browser"]) {
const browserFile = packageJson["browser"][nodeFile].replace(
/^(\.\/)?lib\//,
"src/"
);
nodeFile = nodeFile.replace(/^(\.\/)?lib\//, "src/");
if (id.endsWith(nodeFile)) {
if (browserFile === false) {
return "";
}
return fs.readFileSync(
path.join(packageFolder, browserFile),
"UTF-8"
);
}
}
}
}
return null;
},
resolveId(importee) {
if (importee === "@babel/runtime/regenerator") {
return path.join(
dirname,
"packages",
"babel-runtime",
"regenerator",
"index.js"
);
}
const matches = importee.match(/^@babel\/([^/]+)$/);
if (!matches) return null;
// resolve babel package names to their src index file
const packageFolder = path.join(
dirname,
"packages",
`babel-${matches[1]}`
);
let packageJsonSource;
try {
packageJsonSource = fs.readFileSync(
path.join(packageFolder, "package.json")
);
} catch (e) {
// Some Babel packahes aren't in this repository, but in
return null;
}
const packageJson = JSON.parse(packageJsonSource);
const filename =
typeof packageJson["browser"] === "string"
? packageJson["browser"]
: packageJson["main"];
return path.join(
packageFolder,
// replace lib with src in the pkg.json entry
filename.replace(/^(\.\/)?lib\//, "src/")
);
},
};
};