merceyz / typescript-to-proptypes

Generate proptypes from typescript declarations
MIT License
53 stars 22 forks source link

Extract the babeljs plugin #18

Open jdeniau opened 4 years ago

jdeniau commented 4 years ago

Hi,

I found your repo, that looks really efficient to generate prop-types.

I saw that the inject method does inject prop-types into a file content, but the file content need to be a JS file, not a TS one.

The Javascript reality is that build is really complex and babel does really simplify it.

As a matter of facte, I saw that you do have a babel plugin that injects proptypes into the AST, that is called by the inject method. But it has it's own configuration, of babel.

Do you think it would be possible to externalize this plugin to make a "real" babel plugin ?

The idea behind that is to have a babel config like that:

module.exports = {
  presets: [
    '@babel/typescript',
    '@babel/react',
    [
      '@babel/env',
    ],
  ],
  plugins: [
    // ...
    'your-babel-plugin',
  ]
};

thank you !

merceyz commented 4 years ago

I saw that the inject method does inject prop-types into a file content, but the file content need to be a JS file, not a TS one.

It can be any valid source code, you just need to provide it the babel plugins to be able to understand it https://github.com/merceyz/typescript-to-proptypes/blob/d02a08a7f4c7a25e08c786fa8b625d838613c8b5/src/injector.ts#L45-L48

Do you think it would be possible to externalize this plugin to make a "real" babel plugin ?

Sure, that should be possible