@cc-heart/vite-plugin-uni-pages-to-enum
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

vite-plugin-uni-pages-to-enum

vite-plugin-uni-pages-to-enum 是一个 Vite 插件,用于将 uni-app 项目中的 pages.json 路由配置文件转换为常量枚举,以便在代码中更安全地使用路由路径。

安装

使用 npm:

npm install @cc-heart/vite-plugin-uni-pages-to-enum --save-dev

使用

vite.config.js 中引入该插件:

import { defineConfig } from 'vite';
import uniPagesToEnum from '@cc-heart/vite-plugin-uni-pages-to-enum';

export default defineConfig({
  plugins: [
    uniPagesToEnum({
      input: 'src/pages.json',
      output: 'src/pages-router.ts',
      isConstEnum: true,
      enumName: 'PAGES'
    }),
  ],
});

该插件会根据配置读取文件,并且生成相应的路由枚举路径

例如 page.json 包含:

{
  "pages": [
    {
      "path": "pages/index/index",
      "type": "home",
      "style": {
        "navigationStyle": "custom"
      }
    },
    {
      "path": "pages/delivery/add-consignee",
      "type": "page",
      "style": {
        "navigationStyle": "custom"
      }
    }
  ]
}

则会生成以下的内容:

// isConstEnum: false, export const PAGES = { ... }

export const enum PAGES {
  PAGES_INDEX_INDEX = '/pages/index/index',
  PAGES_DELIVERY_ADD_CONSIGNEE = '/pages/delivery/add-consignee',
}

许可证

MIT License

Package Sidebar

Install

npm i @cc-heart/vite-plugin-uni-pages-to-enum

Weekly Downloads

0

Version

0.0.3

License

MIT

Unpacked Size

108 kB

Total Files

6

Last publish

Collaborators

  • cc-heart