vue编译器

ast - 编译成代码

import * as path from 'path'
import type { Plugin, ResolvedConfig } from 'vite'
import { NodePath } from '@babel/traverse';
import { JSXElement } from '@babel/types';
import {compile, generate, transform, parse} from "@vue/compiler-dom";


export default function fullImportPlugin () {
  let config: ResolvedConfig
  return <Plugin>{
    name: 'autoCompleteCid',
    transform (code, id) {
      if (!id.endsWith(".vue")) {
        return code;
      }
      const ast = parse(code);
      const template = ast.children.find(item => item["tag"] == "template");
      if (template) {
        const result = compile(template.loc.source, {
          mode: "module",
        });
        console.log("...........");
        // console.log(result.ast)
        transform(result.ast as any, {
          // @ts-ignore
          preTransformNode: (node) => {
            console.log(node);
          }
        });
        const res = generate(result.ast as any, {
          mode: "module",
          filename: path.basename(id),
          prefixIdentifiers: true,
        });
      }

      return code
    }
  }
}

posted @ 2024-04-11 13:20  fight139  阅读(5)  评论(0编辑  收藏  举报