Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Feb 14, 2025
1 parent c274fe3 commit 040daf2
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@ packages/babel-standalone/babel.min.js.map
.module-type

*.tsbuildinfo

deno.json
23 changes: 18 additions & 5 deletions Gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ function bool(value) {
* @returns {string}
*/
function mapSrcToLib(srcPath) {
const parts = srcPath
.replace(/(?<!\.d)\.ts$/, ".js")
.replace(/(?<!\.d)\.cts$/, ".cjs")
.split("/");
if (!bool(process.env.PRESERVE_TYPESCRIPT)) {
srcPath = srcPath
.replace(/(?<!\.d)\.ts$/, ".js")
.replace(/(?<!\.d)\.cts$/, ".cjs");
}
const parts = srcPath.split("/");
parts[2] = "lib";
return parts.join("/");
}
Expand Down Expand Up @@ -701,7 +703,9 @@ function* libBundlesIterator() {
}

let libBundles;
if (bool(process.env.BABEL_8_BREAKING)) {
if (bool(process.env.PRESERVE_TYPESCRIPT)) {
libBundles = [];
} else if (bool(process.env.BABEL_8_BREAKING)) {
libBundles = Array.from(libBundlesIterator());
} else {
libBundles = [
Expand Down Expand Up @@ -889,6 +893,15 @@ gulp.task("build-cjs-bundles", () => {
);
});

gulp.task("copy-dts-to-lib", async () => {
const files = new Glob(`${defaultPackagesGlob}/src/**/*.d.ts`, {
posix: true,
});
for await (const file of files) {
fs.cpSync(file, mapSrcToLib(file), {});
}
});

gulp.task(
"build",
gulp.series(
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ prepublish-prepare-dts:
prepublish-build:
$(MAKEJS) prepublish-build

prepublish-ts-build:
$(MAKEJS) prepublish-ts-build

prepublish:
$(MAKEJS) prepublish

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

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions Makefile.source.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,45 @@ target["prepublish-build"] = function () {
);
};

target["prepublish-ts-build"] = function () {
target["clean-lib"]();
target["clean-runtime-helpers"]();

node(["scripts/generators/npm-ignore.js"]);

env(
() => {
target["build-bundle"]();
target["prepublish-build-standalone"]();
target["clone-license"]();
},
{
NODE_ENV: "production",
BABEL_ENV: "production",
STRIP_BABEL_8_FLAG: "true",
}
);

env(
() => {
// After that everythins is built, re-compile
// /src/ to /lib/ preserving TypeScript
shell.rm(
"-rf",
SOURCES.map(source => `${source}/*/lib`)
);
yarn(["gulp", "build-babel"]);
yarn(["gulp", "copy-dts-to-lib"]);
},
{
NODE_ENV: "production",
BABEL_ENV: "production",
STRIP_BABEL_8_FLAG: "true",
PRESERVE_TYPESCRIPT: "true",
}
);
};

target["prepublish-build-standalone"] = function () {
env(
() => {
Expand Down
16 changes: 12 additions & 4 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module.exports = function (api) {

let targets = {};
let convertESM = outputType === "script";
let replaceTSImportExtension = true;
let replaceTSImportExtension = !bool(process.env.PRESERVE_TYPESCRIPT);
let ignoreLib = true;
let needsPolyfillsForOldNode = false;

Expand Down Expand Up @@ -163,7 +163,11 @@ module.exports = function (api) {
// other random files in Babel's codebase, so we use script as the default,
// and then mark actual modules as modules farther down.
sourceType: "script",
comments: false,

shouldPrintComment(comment) {
return /@__PURE__|@license|@preserve|@ts-self-types/.test(comment);
},

ignore: [
// These may not be strictly necessary with the newly-limited scope of
// babelrc searching, but including them for now because we had them
Expand All @@ -180,9 +184,13 @@ module.exports = function (api) {
presets: [
// presets are applied from right to left
["@babel/env", envOpts],
["@babel/preset-typescript", presetTsOpts],
],
bool(process.env.PRESERVE_TYPESCRIPT)
? null
: ["@babel/preset-typescript", presetTsOpts],
].filter(Boolean),
plugins: [
"@babel/plugin-syntax-typescript",

["@babel/transform-object-rest-spread", { useBuiltIns: true }],

convertESM ? "@babel/transform-export-namespace-from" : null,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@babel/eslint-parser": "workspace:^",
"@babel/eslint-plugin-development": "workspace:^",
"@babel/eslint-plugin-development-internal": "workspace:^",
"@babel/plugin-syntax-typescript": "8.0.0-alpha.16",
"@babel/plugin-transform-dynamic-import": "8.0.0-alpha.16",
"@babel/plugin-transform-export-namespace-from": "8.0.0-alpha.16",
"@babel/plugin-transform-json-modules": "8.0.0-alpha.16",
Expand Down
7 changes: 2 additions & 5 deletions packages/babel-core/src/config/files/module-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@ You are using a ${ext} config file, but Babel only supports transpiling .cts con
filename,
);
} catch (error) {
// TODO(Babel 8): Add this as an optional peer dependency
// eslint-disable-next-line import/no-extraneous-dependencies
const packageJson = require("@babel/preset-typescript/package.json");
const packageJson = require("@babel/preset-typescript/package.json" + "");
if (semver.lt(packageJson.version, "7.21.4")) {
console.error(
"`.cts` configuration file failed to load, please try to update `@babel/preset-typescript`.",
Expand All @@ -267,8 +265,7 @@ You are using a ${ext} config file, but Babel only supports transpiling .cts con

function getTSPreset(filepath: string) {
try {
// eslint-disable-next-line import/no-extraneous-dependencies
return require("@babel/preset-typescript");
return require("@babel/preset-typescript" + "");
} catch (error) {
if (error.code !== "MODULE_NOT_FOUND") throw error;

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helper-compilation-targets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
Browsers,
BrowserslistBrowserName,
TargetsTuple,
} from "./types.ts";
} from "./types.d.ts";

export type { Target, Targets, InputTargets };

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import type { Targets, InputTargets } from "@babel/helper-compilation-targets";
import availablePlugins from "./available-plugins.ts";
import { declarePreset } from "@babel/helper-plugin-utils";

import type { BuiltInsOption, ModuleOption, Options } from "./types.ts";
import type { BuiltInsOption, ModuleOption, Options } from "./types.d.ts";
export type { Options };

// TODO: Remove in Babel 8
Expand Down
126 changes: 126 additions & 0 deletions scripts/generators/deno-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { glob } from "glob";
import { repoRoot } from "$repo-utils";
import { existsSync, readFileSync, writeFileSync } from "fs";
import path from "path";

const packages = glob
.sync("./packages/*", {
cwd: repoRoot,
absolute: true,
})
.filter(packageDir => {
return existsSync(path.join(packageDir, "package.json"));
});

const allPackages = [];
const babelVersion = `0.0.8-${
JSON.parse(readFileSync(path.join(repoRoot, "package.json"), "utf8"))
.version_babel8
}`;

for (const packageDir of packages) {
if (
packageDir.includes("babel-runtime-corejs2") ||
packageDir.includes("babel-cli") ||
packageDir.includes("babel-node") ||
packageDir.includes("babel-register")
) {
continue;
}

const packageJson = JSON.parse(
readFileSync(path.join(packageDir, "package.json"), "utf8")
);

if (packageJson.private) {
continue;
}

allPackages.push(packageDir);

const imports = {};
takeDeps(imports, packageJson.dependencies);
takeDeps(imports, packageJson.peerDependencies);

const denoJson = {
name: jsrify(packageJson.name),
version: babelVersion,
exports: objectMap(packageJson.exports, (values, key) => {
if (!Array.isArray(values)) values = [values];
for (let value of values) {
if (typeof value === "object") {
value = value.default;
}
if (typeof value === "string") {
if (value === "./regenerator/*.js") return undefined;
if (value === "./regenerator/") return undefined;
if (value === "./core-js/*.js") return undefined;
if (value === "./core-js/") return undefined;
if (value === "./core-js-stable/*.js") return undefined;
if (value === "./core-js-stable/") return undefined;
if (value.startsWith("./lib")) {
return value.replace(/\.([mc]?)js$/, ".$1ts");
}
return value;
}
}
throw new Error(`Unexpected exports value for ${key} in ${packageDir}`);
}),
imports,
publish: {
exclude: [
"test/",
"!lib/",
packageDir.includes("babel-runtime") ? "!helpers/" : null,
packageDir.includes("babel-runtime") ? "!core-js/" : null,
packageDir.includes("babel-runtime") ? "!core-js-stable/" : null,
packageDir.includes("babel-standalone") ? "!babel.js/" : null,
packageDir.includes("babel-standalone") ? "!babel.min.js/" : null,
].filter(Boolean),
},
};

writeFileSync(
path.join(packageDir, "deno.json"),
JSON.stringify(denoJson, null, 2) + "\n"
);
}

writeFileSync(
path.join(repoRoot, "deno.json"),
JSON.stringify({ workspace: allPackages }, null, 2) + "\n"
);

function objectMap(obj, cb) {
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, cb(v, k)]));
}

function jsrify(npmName) {
// prettier-ignore
const [/* scope */ , name] = npmName.split("/");
return `@babel-test-6ae45912/${name.slice(0, 58)}`;
}

function takeDeps(imports, dependencies) {
if (!dependencies) return;

// eslint-disable-next-line prefer-const
for (let [name, version] of Object.entries(dependencies)) {
if (version.startsWith("condition:")) {
const match =
/condition:\s*\w+\s*\?\s*(?:(?<ifTrue>[\w.^~*]+)\s*)?:\s*(?:[\w.^~*]+\s*)?/.exec(
version
);
if (!match) {
throw new Error(`Invalid condition: ${version}`);
}
if (!match.groups.ifTrue) continue;
version = match.groups.ifTrue;
}
if (version.startsWith("workspace:")) {
imports[name] = `jsr:${jsrify(name)}@${babelVersion}`;
} else {
imports[name] = `npm:${name}@${version}`;
}
}
}
23 changes: 12 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,17 @@ __metadata:
languageName: node
linkType: hard

"@babel/plugin-syntax-typescript@npm:8.0.0-alpha.16, @babel/plugin-syntax-typescript@npm:^8.0.0-alpha.16":
version: 8.0.0-alpha.16
resolution: "@babel/plugin-syntax-typescript@npm:8.0.0-alpha.16"
dependencies:
"@babel/helper-plugin-utils": "npm:^8.0.0-alpha.16"
peerDependencies:
"@babel/core": ^8.0.0-alpha.16
checksum: 10/067cd1b7d31f0741ff966979dda96c010dbef37d4b34a07bbe71a9208094c8f36604acaee98ec0c657c5dca6b2735012cdd910662a2ddba5fcd37d433d1a7401
languageName: node
linkType: hard

"@babel/plugin-syntax-typescript@npm:^7.7.2":
version: 7.22.5
resolution: "@babel/plugin-syntax-typescript@npm:7.22.5"
Expand All @@ -2055,17 +2066,6 @@ __metadata:
languageName: node
linkType: hard

"@babel/plugin-syntax-typescript@npm:^8.0.0-alpha.16":
version: 8.0.0-alpha.16
resolution: "@babel/plugin-syntax-typescript@npm:8.0.0-alpha.16"
dependencies:
"@babel/helper-plugin-utils": "npm:^8.0.0-alpha.16"
peerDependencies:
"@babel/core": ^8.0.0-alpha.16
checksum: 10/067cd1b7d31f0741ff966979dda96c010dbef37d4b34a07bbe71a9208094c8f36604acaee98ec0c657c5dca6b2735012cdd910662a2ddba5fcd37d433d1a7401
languageName: node
linkType: hard

"@babel/plugin-syntax-typescript@workspace:^, @babel/plugin-syntax-typescript@workspace:packages/babel-plugin-syntax-typescript":
version: 0.0.0-use.local
resolution: "@babel/plugin-syntax-typescript@workspace:packages/babel-plugin-syntax-typescript"
Expand Down Expand Up @@ -6817,6 +6817,7 @@ __metadata:
"@babel/eslint-parser": "workspace:^"
"@babel/eslint-plugin-development": "workspace:^"
"@babel/eslint-plugin-development-internal": "workspace:^"
"@babel/plugin-syntax-typescript": "npm:8.0.0-alpha.16"
"@babel/plugin-transform-dynamic-import": "npm:8.0.0-alpha.16"
"@babel/plugin-transform-export-namespace-from": "npm:8.0.0-alpha.16"
"@babel/plugin-transform-json-modules": "npm:8.0.0-alpha.16"
Expand Down

0 comments on commit 040daf2

Please sign in to comment.