Skip to content

Commit

Permalink
[babel 8] Publish .d.ts files for every package (#16416)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed May 15, 2024
1 parent 6e14020 commit 6e3539b
Show file tree
Hide file tree
Showing 193 changed files with 934 additions and 381 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))) {
200 changes: 110 additions & 90 deletions Gulpfile.mjs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 6e3539b

Please sign in to comment.