vue編譯器

fight139發表於2024-04-11

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
    }
  }
}

相關文章