npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@uni-ku/define-page

v0.1.0

Published

Define uniapp pages.json dynamically.

Downloads

3

Readme

@uni-ku/define-page

definePage 宏,用于动态生成 pages.json

  • 支持类型提示、约束
  • 支持 json
  • 支持函数和异步函数
  • 支持从外部导入变量、函数

安装

pnpm i -D @uni-ku/define-page

配置

vite 配置

import uni from '@dcloudio/vite-plugin-uni';
import { viteUniPagesJson } from '@uni-ku/define-page';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    viteUniPagesJson(), // 详细配置请看下面的详细描述
    uni(), // 添加在 viteUniPagesJson() 之后
    // 其他plugins
  ],
});

definePage 全局类型声明

将类型添加到 tsconfig.json 中的 compilerOptions.types

{
  "compilerOptions": {
    "types": ["@uni-ku/define-page"]
  }
}

vite 详细配置

export interface UserConfig {

  /**
   * 项目根目录
   * @default vite 的 `root` 属性
   */
  root?: string;

  /**
   * pages.json 的相对目录
   * @default "src"
   */
  basePath?: string;

  /**
   * 为页面路径生成 TypeScript 声明
   * 接受相对项目根目录的路径
   * null 则取消生成
   * @default basePath
   */
  dts?: string | null;

  /**
   * pages的相对路径
   * @default 'src/pages'
   */
  pages?: string;

  /**
   * subPackages的相对路径
   * @default []
   */
  subPackages?: string[];

  /**
   * 排除条件,应用于 pages 和 subPackages 的文件
   * @default ['node_modules', '.git', '** /__*__/ **']
   */
  exclude?: string[];

  /**
   * pages和subPages的文件扫描深度
   * @default 3
   */
  fileDeep?: number;

  /**
   * 显示调试
   * @default false
   */
  debug?: boolean | DebugType;
}

全局 pages.json.(ts|mts|cts|js|cjs|mjs) 配置

动态配置文件,和 pages.json 同级目录。

将与 definePage 合并,生成最终的 pages.json

import { UniPagesJson } from '@uni-ku/define-page';

export default UniPagesJson({
  globalStyle: {
    navigationBarTextStyle: 'black',
    navigationBarTitleText: 'uni-app',
    navigationBarBackgroundColor: '#F8F8F8',
    backgroundColor: '#F8F8F8',
  },
  pages: [
    // pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
    // {
    //   path: 'pages/index/index',
    //   style: {
    //     navigationBarTitleText: 'uni-app',
    //   },
    // },
  ],
});

使用

vue SFC文件内 definePage 宏使用方式

更多使用方式请参考 playground/pages/define-page

注意:

  • 以下代码需要写在 script setup
  • definePage 宏和当前 SFC 不同域,且先于 SFC 生成,SFC 内部变量无法使用。
  • 页面 path url 将会自动根据文件路径生成,如无须配置其他项目,definePage可省略
  • 同一个 script setup 内仅可使用一个 definePage

JSON 对象

definePage({
  style: {
    navigationBarTitleText: 'hello world',
  },
  middlewares: [
    'auth',
  ],
});

函数

definePage(() => {
  const hello = 'hello';
  const world = 'world';

  return {
    style: {
      navigationBarTitleText: [hello, world].join(' '),
    },
    middlewares: [
      'auth',
    ],
  };
});

嵌套函数

definePage(() => {
  function getTitle(): string {
    const hello = 'hello';
    const world = 'world';

    return [hello, world].join(' ');
  }

  return {
    style: {
      navigationBarTitleText: getTitle(),
    },
    middlewares: [
      'auth',
    ],
  };
});

引入外部函数、变量

import { randamText } from './utils';

definePage(() => {
  return {
    style: {
      navigationBarTitleText: randamText(),
    },
  };
});

获取当前上下文的数据

import { ctx } from '@uni-ku/define-page';

console.log(ctx.files);
console.log(ctx.pages);
console.log(ctx.subPackages);

感谢