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

convert @babel/types to typescript #12431

Merged
merged 2 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
117 changes: 101 additions & 16 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ const rollupNodePolyfills = require("rollup-plugin-node-polyfills");
const rollupNodeResolve = require("@rollup/plugin-node-resolve").default;
const rollupReplace = require("@rollup/plugin-replace");
const { terser: rollupTerser } = require("rollup-plugin-terser");
const { default: rollupDts } = require("rollup-plugin-dts");

const defaultPackagesGlob = "./@(codemods|packages|eslint)/*";
const defaultSourcesGlob = `${defaultPackagesGlob}/src/**/{*.js,!(*.d).ts}`;
const defaultDtsGlob = `${defaultPackagesGlob}/lib/**/*.d.ts{,.map}`;

const defaultSourcesGlob = "./@(codemods|packages|eslint)/*/src/**/*.{js,ts}";
const babelStandalonePluginConfigGlob =
"./packages/babel-standalone/scripts/pluginConfig.json";

const buildTypingsWatchGlob = [
"./packages/babel-types/lib/definitions/**/*.js",
"./packages/babel-types/scripts/generators/*.js",
Expand All @@ -37,11 +42,14 @@ const buildTypingsWatchGlob = [
* @example
* mapSrcToLib("packages/babel-template/src/index.ts")
* // returns "packages/babel-template/lib/index.js"
* @example
* mapSrcToLib("packages/babel-template/src/index.d.ts")
* // returns "packages/babel-template/lib/index.d.ts"
* @param {string} srcPath
* @returns {string}
*/
function mapSrcToLib(srcPath) {
const parts = srcPath.replace(/\.ts$/, ".js").split(path.sep);
const parts = srcPath.replace(/(?<!\.d)\.ts$/, ".js").split(path.sep);
parts[2] = "lib";
return parts.join(path.sep);
}
Expand Down Expand Up @@ -81,19 +89,22 @@ function rename(fn) {
*
* @typedef {("asserts" | "builders" | "constants" | "validators")} HelperKind
* @param {HelperKind} helperKind
* @param {string} filename
*/
function generateTypeHelpers(helperKind) {
function generateTypeHelpers(helperKind, filename = "index.ts") {
const dest = `./packages/babel-types/src/${helperKind}/generated/`;
const formatCode = require("./scripts/utils/formatCode");
return gulp
const stream = gulp
.src(".", { base: __dirname })
.pipe(errorsLogger())
.pipe(
through.obj(function (file, enc, callback) {
file.path = "index.js";
file.path = filename;
file.contents = Buffer.from(
formatCode(
require(`./packages/babel-types/scripts/generators/${helperKind}`)(),
require(`./packages/babel-types/scripts/generators/${helperKind}`)(
filename
),
dest + file.path
)
);
Expand All @@ -102,6 +113,8 @@ function generateTypeHelpers(helperKind) {
})
)
.pipe(gulp.dest(dest));

return finish(stream);
Copy link
Member

Choose a reason for hiding this comment

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

@JLHwung This is a bug introduced by the recent gulpfile changes: we were returning a stream instead of a promise, and we were passing those streams to Promise.all.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah good catch!

}

function generateStandalone() {
Expand Down Expand Up @@ -139,18 +152,42 @@ export const all = {${allList}};`;
.pipe(gulp.dest(dest));
}

function buildBabel(exclude, sourcesGlob = defaultSourcesGlob) {
const base = __dirname;
function unlink() {
return through.obj(function (file, enc, callback) {
fs.unlink(file.path, () => callback());
});
}

function finish(stream) {
return new Promise((resolve, reject) => {
stream.on("end", resolve);
stream.on("finish", resolve);
stream.on("error", reject);
});
}

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

if (exclude) {
const filters = exclude.map(p => `!**/${p.src}/**`);
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 buildBabel(exclude) {
const base = __dirname;

return stream
return getFiles(defaultSourcesGlob, {
exclude: exclude && exclude.map(p => p.src),
})
.pipe(errorsLogger())
.pipe(newer({ dest: base, map: mapSrcToLib }))
.pipe(compilationLogger())
Expand Down Expand Up @@ -283,6 +320,41 @@ function buildRollup(packages, targetBrowsers) {
);
}

function buildRollupDts(packages) {
const sourcemap = process.env.NODE_ENV === "production";
return Promise.all(
packages.map(async packageName => {
const input = `${packageName}/lib/index.d.ts`;
fancyLog(`Bundling '${chalk.cyan(input)}' with rollup ...`);
const bundle = await rollup.rollup({
input,
plugins: [rollupDts()],
});

await finish(
gulp.src(`${packageName}/lib/**/*.d.ts{,.map}`).pipe(unlink())
);

await bundle.write({
file: `${packageName}/lib/index.d.ts`,
format: "es",
sourcemap: sourcemap,
exports: "named",
});
})
);
}

function removeDts(exclude) {
return getFiles(defaultDtsGlob, { exclude }).pipe(unlink());
}

function copyDts(packages) {
return getFiles(`${defaultPackagesGlob}/src/**/*.d.ts`, { include: packages })
.pipe(rename(file => path.resolve(file.base, mapSrcToLib(file.relative))))
.pipe(gulp.dest(__dirname));
}

const libBundles = [
"packages/babel-parser",
"packages/babel-plugin-proposal-optional-chaining",
Expand All @@ -293,6 +365,8 @@ const libBundles = [
dest: "lib",
}));

const dtsBundles = ["packages/babel-types"];

const standaloneBundle = [
{
src: "packages/babel-standalone",
Expand All @@ -306,11 +380,15 @@ const standaloneBundle = [

gulp.task("generate-type-helpers", () => {
fancyLog("Generating @babel/types dynamic functions");
return Promise.all(
["asserts", "builders", "constants", "validators"].map(helperKind =>
generateTypeHelpers(helperKind)
)
);

return Promise.all([
generateTypeHelpers("asserts"),
generateTypeHelpers("builders"),
generateTypeHelpers("builders", "uppercase.js"),
generateTypeHelpers("constants"),
generateTypeHelpers("validators"),
generateTypeHelpers("ast-types"),
]);
});

gulp.task("generate-standalone", () => generateStandalone());
Expand All @@ -322,6 +400,13 @@ 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("clean-dts", () => removeDts(/* exclude */ dtsBundles));

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

gulp.task(
Expand Down
45 changes: 16 additions & 29 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ endif

build-bundle: clean clean-lib
$(YARN) gulp build
$(MAKE) build-typings
$(MAKE) build-flow-typings
$(MAKE) build-dist

build-bundle-ci: bootstrap-only
Expand All @@ -39,14 +39,11 @@ generate-tsconfig:
generate-type-helpers:
$(YARN) gulp generate-type-helpers

build-typings: build-flow-typings build-typescript-typings

build-flow-typings:
$(NODE) packages/babel-types/scripts/generators/flow.js > packages/babel-types/lib/index.js.flow

build-typescript-typings:
$(NODE) packages/babel-types/scripts/generators/typescript.js > packages/babel-types/lib/index.d.ts
$(NODE) packages/babel-types/scripts/generators/typescript.js --ts3.7 > packages/babel-types/lib/index-ts3.7.d.ts
build-typescript-3.7-typings:
$(NODE) packages/babel-types/scripts/generators/typescript-3.7.js > packages/babel-types/lib/index-ts3.7.d.ts
Copy link
Contributor Author

@zxbodya zxbodya Dec 9, 2020

Choose a reason for hiding this comment

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

would like to not have duplicated script for typescript type generation… - going to try https://github.com/sandersn/downlevel-dts (but this can also be after merging this pr)

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer to keep this solution that has been working well for a while. We'll remove it in Babel 8 anyway.


build-standalone: build-babel-standalone

Expand Down Expand Up @@ -74,7 +71,7 @@ build-no-bundle: clean clean-lib
# Ensure that build artifacts for types are created during local
# development too.
# Babel-transform-fixture-test-runner requires minified polyfill for performance
$(MAKE) build-typings build-polyfill-dist
$(MAKE) build-flow-typings build-polyfill-dist

watch: build-no-bundle
BABEL_ENV=development $(YARN) gulp watch
Expand All @@ -85,31 +82,19 @@ flowcheck-ci:
code-quality: tscheck flow lint

tscheck: generate-tsconfig
make build-typescript-typings
$(YARN) tsc -b .

flow:
flow: build-flow-typings
$(YARN) flow check --strip-root

lint-ci: lint-js-ci lint-ts-ci check-compat-data-ci

lint-js-ci:
$(MAKE) lint-js

lint-ts-ci:
$(MAKE) lint-ts
lint-ci: lint check-compat-data-ci

check-compat-data-ci:
$(MAKE) check-compat-data

lint: lint-js lint-ts

lint-js:
lint:
BABEL_ENV=test $(YARN) eslint scripts $(SOURCES) '*.{js,ts}' --format=codeframe --ext .js,.cjs,.mjs,.ts

lint-ts:
scripts/lint-ts-typings.sh

fix: fix-json fix-js

fix-js:
Expand Down Expand Up @@ -202,19 +187,21 @@ clone-license:

prepublish-build: clean-lib clean-runtime-helpers
NODE_ENV=production BABEL_ENV=production STRIP_BABEL_8_FLAG=true $(MAKE) build-bundle
$(MAKE) prepublish-build-standalone clone-license
$(MAKE) prepublish-build-standalone clone-license prepublish-prepare-dts

prepublish-prepare-dts:
$(MAKE) clean-tsconfig
$(MAKE) tscheck
$(YARN) gulp bundle-dts
$(YARN) gulp clean-dts
$(MAKE) build-typescript-3.7-typings
$(MAKE) clean-tsconfig

prepublish:
$(MAKE) check-yarn-bug-1882
$(MAKE) bootstrap-only
$(MAKE) prepublish-build
IS_PUBLISH=true $(MAKE) test
# We don't want to publish TS-related files yet, except for @babel/types
rm -f packages/*/lib/**/*.d.ts{,.map}
rm -f codemods/*/lib/**/*.d.ts{,.map}
rm -f eslint/*/lib/**/*.d.ts{,.map}
$(MAKE) clean-tsconfig
$(MAKE) build-typescript-typings

new-version:
git pull --rebase
Expand Down
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ coverage:
patch:
enabled: false
ignore:
- packages/babel-types/src/*/generated/index.js
- packages/babel-types/src/*/generated/*
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"mergeiterator": "^1.2.5",
"prettier": "^2.0.5",
"rollup": "^2.26.5",
"rollup-plugin-dts": "^2.0.0",
"rollup-plugin-node-polyfills": "^0.2.1",
"rollup-plugin-terser": "^7.0.0",
"test262-stream": "^1.3.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helper-replace-supers/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function skipAllButComputedKey(path: NodePath) {
// Avoid using `path.scope` here
export const environmentVisitor = {
// todo (Babel 8): remove StaticBlock brand checks
[`${t.StaticBlock ? "StaticBlock|" : ""}ClassPrivateProperty|TypeAnnotation`](
[`${t.staticBlock ? "StaticBlock|" : ""}ClassPrivateProperty|TypeAnnotation`](
path: NodePath,
) {
path.skip();
Expand Down
2 changes: 0 additions & 2 deletions packages/babel-standalone/src/generated/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ import transformTypeofSymbol from "@babel/plugin-transform-typeof-symbol";
import transformTypescript from "@babel/plugin-transform-typescript";
import transformUnicodeEscapes from "@babel/plugin-transform-unicode-escapes";
import transformUnicodeRegex from "@babel/plugin-transform-unicode-regex";

export {
externalHelpers,
syntaxAsyncGenerators,
Expand Down Expand Up @@ -191,7 +190,6 @@ export {
transformUnicodeEscapes,
transformUnicodeRegex,
};

export const all = {
"external-helpers": externalHelpers,
"syntax-async-generators": syntaxAsyncGenerators,
Expand Down
7 changes: 4 additions & 3 deletions packages/babel-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
"directory": "packages/babel-types"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
"typesVersions": {
">=3.7": {
"<3.7": {
"lib/index.d.ts": [
"lib/index-ts3.7.d.ts"
]
Expand All @@ -29,6 +28,8 @@
},
"devDependencies": {
"@babel/generator": "workspace:*",
"@babel/parser": "workspace:*"
"@babel/parser": "workspace:*",
"@types/lodash": "^4.14.162",
"chalk": "^4.1.0"
}
}