Skip to content

Commit

Permalink
[babel 8] Publish .d.ts for every package
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Apr 7, 2024
1 parent eef8562 commit 774475b
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 57 deletions.
24 changes: 24 additions & 0 deletions .yarn/patches/rollup-plugin-dts-npm-6.1.0-6d41e665a7.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
diff --git a/dist/rollup-plugin-dts.mjs b/dist/rollup-plugin-dts.mjs
index 4a9412285c48c37d03340a086c771f8e61fd82ac..9e7c42c4ca12ddbee7e1bdbfe19e30b1d5555bc0 100644
--- a/dist/rollup-plugin-dts.mjs
+++ b/dist/rollup-plugin-dts.mjs
@@ -309,6 +309,19 @@ class NamespaceFixer {
}
const exports = [];
for (const prop of obj.properties) {
+ // Rollup generates an object of getters for re-exports sometimes
+ if (
+ ts.isGetAccessor(prop) &&
+ ts.isIdentifier(prop.name) &&
+ ts.isReturnStatement(prop.body.statements[0]) &&
+ ts.isIdentifier(prop.body.statements[0].expression)
+ ) {
+ exports.push({
+ exportedName: prop.name.text,
+ localName: prop.body.statements[0].expression.getText()
+ });
+ continue;
+ }
if (!ts.isPropertyAssignment(prop) ||
!(ts.isIdentifier(prop.name) || ts.isStringLiteral(prop.name)) ||
(prop.name.text !== "__proto__" && !ts.isIdentifier(prop.initializer))) {
163 changes: 109 additions & 54 deletions Gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -596,14 +596,47 @@ function buildRollup(packages, buildStandalone) {
}

function buildRollupDts(packages) {
async function build(input, output, banner) {
async function build(input, output, banner, packageName) {
log(`Bundling '${colors.cyan(output)}' with rollup ...`);

let external;
if (packageName) {
const pkgJSON = require("./" + packageName + "/package.json");
const { dependencies = {}, peerDependencies = {} } = pkgJSON;
external = [
...Object.keys(dependencies),
...Object.keys(peerDependencies),
// @babel/compat-data sub exports
/@babel\/compat-data\/.*/,
// Ideally they should be constructed from package.json exports
// required by modules-commonjs
/babel-plugin-dynamic-import-node\/utils/,
// required by preset-env
/@babel\/preset-modules\/.*/,
];
}

const bundle = await rollup({
input,
plugins: [
bool(process.env.BABEL_8_BREAKING) ? rollupDts() : rollupDts5(),
],
external,
onwarn(warning) {
if (
warning.code === "UNUSED_EXTERNAL_IMPORT" &&
warning.names.length === 1 &&
warning.names[0] === "default"
) {
// rollup-plugin-dts doesn't like default imports when they are just re-exported
return;
}
if (warning.code === "UNRESOLVED_IMPORT" && warning.exporter === "vm") {
// TODO: We probably need @types/node
return;
}
console.warn(warning);
},
});

await bundle.write({
Expand All @@ -617,7 +650,7 @@ function buildRollupDts(packages) {
const input = `${mapToDts(packageName)}/src/index.d.ts`;
const output = `${packageName}/lib/index.d.ts`;

await build(input, output);
await build(input, output, "", packageName);
});

tasks.push(
Expand All @@ -631,73 +664,77 @@ function buildRollupDts(packages) {
return Promise.all(tasks);
}

// Copies manually-written .d.ts files from `src` to /dts
function copyDts(packages) {
return getFiles(`${defaultPackagesGlob}/src/**/*.d.ts`, { include: packages })
.pipe(rename(file => path.resolve(file.base, mapToDts(file.relative))))
.pipe(gulp.dest(monorepoRoot));
}

function* libBundlesIterator() {
const noBundle = new Set([
// @rollup/plugin-commonjs will mess up with babel-helper-fixtures
"babel-helper-fixtures",
// babel-standalone is handled by rollup-babel-standalone task
"babel-standalone",
// todo: Rollup hangs on allowHashBang: true with babel-cli/src/babel/index.ts hashbang
"babel-cli",
// todo: @rollup/node-resolve 'browsers' option does not work when package.json contains `exports`
// https://github.com/rollup/plugins/tree/master/packages/node-resolve#browser
"babel-register",
"babel-core",
"babel-plugin-transform-runtime",
// @babel/node invokes internal lib/_babel-node.js
"babel-node",
// todo: test/helpers/define-helper requires internal lib/helpers access
"babel-helpers",
// multiple exports
"babel-plugin-transform-react-jsx",
// rollup bug https://github.com/babel/babel/pull/16001
"babel-helper-builder-react-jsx",
// exit-loader.cjs
"babel-helper-transform-fixture-test-runner",
]);
function* packagesIterator(exclude) {
for (const packageDir of ["packages", "codemods"]) {
for (const dir of fs.readdirSync(new URL(packageDir, import.meta.url))) {
if (noBundle.has(dir)) continue;

const src = `${packageDir}/${dir}`;

let pkgJSON;
try {
pkgJSON = JSON.parse(
fs.readFileSync(new URL(`${src}/package.json`, import.meta.url))
);
} catch (err) {
if (err.code !== "ENOENT" && err.code !== "ENOTDIR") throw err;
if (exclude.has(src)) continue;
if (!fs.existsSync(new URL(`${src}/package.json`, import.meta.url))) {
continue;
}
if (pkgJSON.main) {
yield src;
}
}
}

function* libBundlesIterator() {
const noBundle = new Set(
[
// @rollup/plugin-commonjs will mess up with babel-helper-fixtures
"babel-helper-fixtures",
// babel-standalone is handled by rollup-babel-standalone task
"babel-standalone",
// todo: Rollup hangs on allowHashBang: true with babel-cli/src/babel/index.ts hashbang
"babel-cli",
// todo: @rollup/node-resolve 'browsers' option does not work when package.json contains `exports`
// https://github.com/rollup/plugins/tree/master/packages/node-resolve#browser
"babel-register",
"babel-core",
"babel-plugin-transform-runtime",
// @babel/node invokes internal lib/_babel-node.js
"babel-node",
// todo: test/helpers/define-helper requires internal lib/helpers access
"babel-helpers",
// multiple exports
"babel-plugin-transform-react-jsx",
// rollup bug https://github.com/babel/babel/pull/16001
"babel-helper-builder-react-jsx",
// exit-loader.cjs
"babel-helper-transform-fixture-test-runner",
].map(n => `packages/${n}`)
);
for (const src of packagesIterator(noBundle)) {
const pkgJSON = JSON.parse(
fs.readFileSync(new URL(`${src}/package.json`, import.meta.url))
);
if (pkgJSON.main) {
yield {
src,
format: USE_ESM ? "esm" : "cjs",
dest: "lib",
input: getIndexFromPackage(src),
};
} else if (pkgJSON.bin) {
for (const binPath of Object.values(pkgJSON.bin)) {
const filename = binPath.slice(binPath.lastIndexOf("/") + 1);
const input =
src === "packages/babel-cli" && filename === "babel.js"
? `${src}/src/babel/index.ts`
: `${src}/src/${filename.slice(0, -3) + ".ts"}`;
yield {
src,
format: USE_ESM ? "esm" : "cjs",
dest: "lib",
input: getIndexFromPackage(src),
filename,
input,
};
} else if (pkgJSON.bin) {
for (const binPath of Object.values(pkgJSON.bin)) {
const filename = binPath.slice(binPath.lastIndexOf("/") + 1);
const input =
dir === "babel-cli" && filename === "babel.js"
? `${src}/src/babel/index.ts`
: `${src}/src/${filename.slice(0, -3) + ".ts"}`;
yield {
src,
format: USE_ESM ? "esm" : "cjs",
dest: "lib",
filename,
input,
};
}
}
}
}
Expand Down Expand Up @@ -732,7 +769,25 @@ const cjsBundles = [
{ src: "packages/babel-parser" },
];

const dtsBundles = ["packages/babel-types"];
const dtsBundles = bool(process.env.BABEL_8_BREAKING)
? Array.from(
packagesIterator(
new Set([
// CLIs
"packages/babel-cli",
"packages/babel-node",
// This will be just JSON
"packages/babel-compat-data",
// Not meant to be consumed manually
"packages/babel-runtime",
"packages/babel-runtime-corejs2",
"packages/babel-runtime-corejs3",
// TODO: Add type definitions
"packages/babel-register",
])
)
)
: ["packages/babel-types"];

const standaloneBundle = [
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"picocolors": "^1.0.0",
"prettier": "^3.2.5",
"rollup": "^4.9.1",
"rollup-plugin-dts": "^6.1.0",
"rollup-plugin-dts": "patch:rollup-plugin-dts@npm%3A6.1.0#~/.yarn/patches/rollup-plugin-dts-npm-6.1.0-6d41e665a7.patch",
"rollup-plugin-dts-5": "npm:rollup-plugin-dts@^5.3.1",
"rollup-plugin-polyfill-node": "^0.13.0",
"semver": "^6.3.1",
Expand Down
File renamed without changes.
20 changes: 18 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7127,7 +7127,7 @@ __metadata:
picocolors: "npm:^1.0.0"
prettier: "npm:^3.2.5"
rollup: "npm:^4.9.1"
rollup-plugin-dts: "npm:^6.1.0"
rollup-plugin-dts: "patch:rollup-plugin-dts@npm%3A6.1.0#~/.yarn/patches/rollup-plugin-dts-npm-6.1.0-6d41e665a7.patch"
rollup-plugin-dts-5: "npm:rollup-plugin-dts@^5.3.1"
rollup-plugin-polyfill-node: "npm:^0.13.0"
semver: "npm:^6.3.1"
Expand Down Expand Up @@ -15005,7 +15005,7 @@ __metadata:
languageName: node
linkType: hard

"rollup-plugin-dts@npm:^6.1.0":
"rollup-plugin-dts@npm:6.1.0":
version: 6.1.0
resolution: "rollup-plugin-dts@npm:6.1.0"
dependencies:
Expand All @@ -15021,6 +15021,22 @@ __metadata:
languageName: node
linkType: hard

"rollup-plugin-dts@patch:rollup-plugin-dts@npm%3A6.1.0#~/.yarn/patches/rollup-plugin-dts-npm-6.1.0-6d41e665a7.patch":
version: 6.1.0
resolution: "rollup-plugin-dts@patch:rollup-plugin-dts@npm%3A6.1.0#~/.yarn/patches/rollup-plugin-dts-npm-6.1.0-6d41e665a7.patch::version=6.1.0&hash=2a7348"
dependencies:
"@babel/code-frame": "npm:^7.22.13"
magic-string: "npm:^0.30.4"
peerDependencies:
rollup: ^3.29.4 || ^4
typescript: ^4.5 || ^5.0
dependenciesMeta:
"@babel/code-frame":
optional: true
checksum: 10/59e110adea284f2e50efc99e3d3d5859e96919f5b34e6815868dedc62966ffc06b6d8169effb371dcf80bacf4babca9ec146a7d9363cc138d170e07c65fb0c34
languageName: node
linkType: hard

"rollup-plugin-polyfill-node@npm:^0.13.0":
version: 0.13.0
resolution: "rollup-plugin-polyfill-node@npm:0.13.0"
Expand Down

0 comments on commit 774475b

Please sign in to comment.