unjs / unplugin

Unified plugin system for Vite, Rollup, Webpack, esbuild, Rolldown, and more
https://unplugin.unjs.io
MIT License
3.02k stars 109 forks source link

[Feature]:Babel plugin support #320

Closed SnowingFox closed 1 year ago

SnowingFox commented 1 year ago

Describe the feature

In some case, like React Native devlopment, it should use babel instead of webpack, vite ..., so it's necessary to support babel plugin devlopment in unplugin.

Do you have any plan for this feature?

Additional information

antfu commented 1 year ago

I don't feel that are the same category of plugins, how do you expect it to work?

SnowingFox commented 1 year ago

I don't feel that are the same category of plugins, how do you expect it to work?

We don't necessarily need to be compatible with unplugin devlope system like transform, resolveId ..., we just need to provide a entry for babel plugin so that developers can resuse their logic and maintain their code in a same repository instead of creating a new repository to do some repeat thing.

Take your unplugin-auto-import repository as an example

import insertImport from './insert'

export const unplugin = createUnplugin((options: UserOptions) => {
  return {
    name: 'auto-import',
    transformInclude(id) {
      return id.endsWith('.ts')
    },
    transform(code) {
      return insertImport(code)
    },
    babel: {
      visitor: {
        Identifier(path, { opts: options, file }) {
          let { node: identifier, scope } = path;
          let { declarations } = options;

          let filename = file.opts.filename
            ? basename(file.opts.filename)
            : "";

          declarations.some(handleDeclaration, {
            path,
            identifier,
            filename,
          });
        },
        handleDeclaration(declaration) {
          insertImport(declaration);
          return true;
        }
      }
    },
  }
}
so1ve commented 1 year ago

@SnowingFox IMHO this is out-of-scope...

antfu commented 1 year ago

Yeah. As unplugin we certainly don't want to couple with one single parser, as there are many more to choose, or simply no parsers at all. Close as I'd consider it out-of-scope. Thanks.