Skip to content

Commit

Permalink
chore: move babel-plugin-transform-typescript-metadata in plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
sirenkovladd committed Sep 13, 2023
1 parent 47ab269 commit b03cf3f
Show file tree
Hide file tree
Showing 9 changed files with 565 additions and 41 deletions.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@
},
"devDependencies": {
"@babel/core": "^7.22.15",
"@babel/helper-plugin-utils": "^7.22.5",
"@babel/plugin-proposal-decorators": "^7.22.15",
"@babel/plugin-transform-export-namespace-from": "^7.18.9",
"@babel/plugin-transform-nullish-coalescing-operator": "^7.18.6",
"@babel/plugin-transform-optional-chaining": "^7.21.0",
"@babel/plugin-syntax-class-properties": "^7.12.13",
"@babel/plugin-syntax-import-assertions": "^7.22.5",
"@babel/plugin-transform-export-namespace-from": "^7.18.9",
"@babel/plugin-transform-modules-commonjs": "^7.22.15",
"@babel/plugin-transform-nullish-coalescing-operator": "^7.18.6",
"@babel/plugin-transform-optional-chaining": "^7.21.0",
"@babel/plugin-transform-typescript": "^7.22.15",
"@babel/preset-typescript": "^7.22.15",
"@babel/template": "^7.22.15",
"@babel/traverse": "^7.22.15",
"@babel/types": "^7.22.15",
"@types/babel__core": "^7.20.1",
"@types/babel__template": "^7.4.1",
"@types/babel__traverse": "^7.20.1",
"@types/node": "^20.5.9",
"@types/object-hash": "^3.0.4",
"@types/resolve": "^1.20.2",
Expand All @@ -47,7 +50,6 @@
"acorn": "^8.10.0",
"babel-plugin-dynamic-import-node": "^2.3.3",
"babel-plugin-parameter-decorator": "^1.0.16",
"@sirenko/babel-plugin-transform-typescript-metadata": "^0.3.2-fix-reflect",
"changelogen": "^0.5.5",
"config": "^3.3.9",
"create-require": "^1.1.1",
Expand Down
72 changes: 37 additions & 35 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
import { TransformOptions, TRANSFORM_RESULT } from "./types";
import { TransformImportMetaPlugin } from "./plugins/babel-plugin-transform-import-meta";
import { importMetaEnvPlugin } from "./plugins/import-meta-env";
import babelPluginTransformTypescriptMetadata from "./plugins/babel-plugin-transform-typescript-metadata";

export default function transform(opts: TransformOptions): TRANSFORM_RESULT {
const _opts: BabelTransformOptions & { plugins: PluginItem[] } = {
Expand Down Expand Up @@ -37,7 +38,7 @@ export default function transform(opts: TransformOptions): TRANSFORM_RESULT {
]);
// `unshift` because these plugin must come before `@babel/plugin-syntax-class-properties`
_opts.plugins.unshift(
[require("@sirenko/babel-plugin-transform-typescript-metadata")],
[babelPluginTransformTypescriptMetadata],
[require("@babel/plugin-proposal-decorators"), { legacy: true }],
);
_opts.plugins.push(require("babel-plugin-parameter-decorator"));
Expand Down
47 changes: 47 additions & 0 deletions src/plugins/babel-plugin-transform-typescript-metadata/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { PluginObj } from '@babel/core';
import { declare } from '@babel/helper-plugin-utils';
import { parameterVisitor } from './parameter/parameter-visitor';
import { metadataVisitor } from './metadata/metadata-visitor';

export default declare(
(api: any): PluginObj => {
api.assertVersion(7);

return {
visitor: {
Program(programPath) {
/**
* We need to traverse the program right here since
* `@babel/preset-typescript` removes imports at this level.
*
* Since we need to convert some typings into **bindings**, used in
* `Reflect.metadata` calls, we need to process them **before**
* the typescript preset.
*/
programPath.traverse({
ClassDeclaration(path) {
for (const field of path.get('body').get('body')) {
if (
field.type !== 'ClassMethod' &&
field.type !== 'ClassProperty'
) {
continue;
}

parameterVisitor(path, field as any);
metadataVisitor(path, field as any);
}

/**
* We need to keep binding in order to let babel know where imports
* are used as a Value (and not just as a type), so that
* `babel-transform-typescript` do not strip the import.
*/
(path.parentPath.scope as any).crawl();
}
});
}
}
};
}
);
Loading

0 comments on commit b03cf3f

Please sign in to comment.