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

[babel 8] Publish .d.ts files for every package #16416

Merged
merged 25 commits into from May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9a7ee04
[babel 8] Publish .d.ts for every package
nicolo-ribaudo Apr 7, 2024
7bbfebb
Export `File` from `@babel/parser` to use in `@babel/core`
nicolo-ribaudo Apr 7, 2024
75a33c7
Update on top of new type checking infrastructure
nicolo-ribaudo Apr 24, 2024
cc180e6
Validate bundled .d.ts files
nicolo-ribaudo Apr 24, 2024
124b483
Add some missing type deps
nicolo-ribaudo Apr 24, 2024
3269404
Fix flag
nicolo-ribaudo Apr 24, 2024
434948b
Remove old types from parser's exports
nicolo-ribaudo Apr 24, 2024
7667d96
Export `Expression` type from `@babel/parser`
nicolo-ribaudo Apr 26, 2024
816aa05
Use top-evel Babel deps to build `babel-helpers`
nicolo-ribaudo May 8, 2024
f7ca32a
Suppress TS error
nicolo-ribaudo May 8, 2024
db06fca
Update babel-parser/typings/babel-parser.d.ts
nicolo-ribaudo May 8, 2024
35b99d8
Use ts-ignore
nicolo-ribaudo May 8, 2024
9d71c9a
Improve comment
nicolo-ribaudo May 8, 2024
1270dd2
Workaround rollup-plugin-dts bug
nicolo-ribaudo May 8, 2024
b322f40
Help tsc find babel-core/lib/index.d.ts
nicolo-ribaudo May 8, 2024
bdda2c5
Add minimal libdom for @babel/standalone
nicolo-ribaudo May 8, 2024
bda5b92
Mark options as optional in parser types
nicolo-ribaudo May 8, 2024
f617b85
Add `exports.types` in `package.json` (yarn constraint)
nicolo-ribaudo May 8, 2024
f1e02ea
yarn constraints --fix
nicolo-ribaudo May 8, 2024
3e86c5e
Manually `exports.types` to:
nicolo-ribaudo May 8, 2024
967b249
Add `@babel/types` as a devdep of parser (it should be dep)
nicolo-ribaudo May 9, 2024
4b8cb4c
Export `Options` type from preset-env
nicolo-ribaudo May 9, 2024
d4aa7cb
Export `NodePath`, `Scope` and `Visitor` types from core
nicolo-ribaudo May 9, 2024
7a8d156
Add some missing deps
nicolo-ribaudo May 9, 2024
d2fec6f
Update lockfile
nicolo-ribaudo May 9, 2024
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
24 changes: 24 additions & 0 deletions .yarn/patches/rollup-plugin-dts-npm-6.1.0-6d41e665a7.patch
@@ -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
Copy link
Member Author

Choose a reason for hiding this comment

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

rollup-plugin-dts currently crashes on namespace objects generated by Rollup. I will open a PR upstream.

+ 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))) {
200 changes: 110 additions & 90 deletions Gulpfile.mjs
Expand Up @@ -6,7 +6,6 @@ import { fileURLToPath } from "url";
import plumber from "gulp-plumber";
import through from "through2";
import colors from "picocolors";
import filter from "gulp-filter";
import gulp from "gulp";
import { rollup } from "rollup";
import {
Expand Down Expand Up @@ -100,13 +99,6 @@ function errorsLogger() {
});
}

function rename(fn) {
return through.obj(function (file, enc, callback) {
file.path = fn(file);
callback(null, file);
});
}

/**
* @param {string} generator
* @param {string} pkg
Expand Down Expand Up @@ -233,22 +225,6 @@ function finish(stream) {
});
}

function getFiles(glob, { include, exclude }) {
let stream = gulp.src(glob, { base: monorepoRoot });

if (exclude) {
const filters = exclude.map(p => `!**/${p}/**`);
filters.unshift("**");
stream = stream.pipe(filter(filters));
}
if (include) {
const filters = include.map(p => `**/${p}/**`);
stream = stream.pipe(filter(filters));
}

return stream;
}

function createWorker(useWorker) {
const numWorkers = Math.ceil(Math.max(cpus().length, 1) / 2) - 1;
if (
Expand Down Expand Up @@ -596,14 +572,46 @@ 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 = {},
devDependencies = {},
peerDependencies = {},
} = pkgJSON;
external = [
...Object.keys(dependencies),
...Object.keys(peerDependencies),
// TODO: These should all be moved to dependencies
...Object.keys(devDependencies),
].map(dep => new RegExp(`^${dep}(?:/.+)?$`));
}

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,87 +625,85 @@ 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(
build(
"packages/babel-parser/typings/babel-parser.source.d.ts",
"packages/babel-parser/typings/babel-parser.d.ts",
"// This file is auto-generated! Do not modify it directly.\n/* eslint-disable import/no-extraneous-dependencies, @typescript-eslint/consistent-type-imports, prettier/prettier */"
"// This file is auto-generated! Do not modify it directly.\n/* eslint-disable import/no-extraneous-dependencies, @typescript-eslint/consistent-type-imports, prettier/prettier */",
"packages/babel-parser"
)
);

return Promise.all(tasks);
}

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 +738,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 Expand Up @@ -778,11 +802,7 @@ gulp.task(
gulp.series("generate-standalone", "rollup-babel-standalone")
);

gulp.task("copy-dts", () => copyDts(dtsBundles));
gulp.task(
"bundle-dts",
gulp.series("copy-dts", () => buildRollupDts(dtsBundles))
);
gulp.task("bundle-dts", () => buildRollupDts(dtsBundles));

gulp.task("build-babel", () => buildBabel(true, /* exclude */ libBundles));

Expand Down
4 changes: 2 additions & 2 deletions Makefile.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Makefile.source.mjs
Expand Up @@ -293,6 +293,7 @@ target["prepublish-prepare-dts"] = function () {
target["prepublish-prepare-dts-no-clean"] = function () {
yarn(["gulp", "bundle-dts"]);
target["build-typescript-legacy-typings"]();
yarn(["tsc", "-p", "tsconfig.dts-bundles.json"]);
};

target["tscheck"] = function () {
Expand Down
Expand Up @@ -49,7 +49,10 @@
]
},
"exports": {
".": "./lib/index.js",
".": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"./package.json": "./package.json"
},
"type": "commonjs"
Expand Down
Expand Up @@ -49,7 +49,10 @@
]
},
"exports": {
".": "./lib/index.js",
".": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"./package.json": "./package.json"
},
"type": "commonjs"
Expand Down
8 changes: 8 additions & 0 deletions lib/libdom-minimal.d.ts
@@ -0,0 +1,8 @@
// Minimal version of lib.dom.d.ts. @babel/standalone needs these types
// to be defined, but we don't want to load the full lib.dom.d.ts in
// tscondif.dts-bundles.json because we don't want other .d.ts files to
// accidenally rely on DOM features.

// eslint-disable-next-line @typescript-eslint/no-unused-vars
declare interface HTMLCollectionOf<T> {}
declare interface HTMLScriptElement {}
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -61,7 +61,6 @@
"execa": "^8.0.1",
"glob": "^10.3.10",
"gulp": "^4.0.2",
"gulp-filter": "^7.0.0",
"gulp-plumber": "^1.2.1",
"husky": "^8.0.3",
"import-meta-resolve": "^4.1.0",
Expand All @@ -75,7 +74,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
5 changes: 4 additions & 1 deletion packages/babel-cli/package.json
Expand Up @@ -76,7 +76,10 @@
]
},
"exports": {
".": "./lib/index.js",
".": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"./package.json": "./package.json"
},
"type": "commonjs"
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-code-frame/package.json
Expand Up @@ -45,7 +45,10 @@
]
},
"exports": {
".": "./lib/index.js",
".": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"./package.json": "./package.json"
},
"type": "commonjs"
Expand Down