Skip to content

Commit

Permalink
move to @uni-ku/define-page
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinhuish committed Aug 15, 2024
1 parent c74b0e3 commit 9c8ff99
Show file tree
Hide file tree
Showing 19 changed files with 247 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": [
"@changesets/changelog-github",
{ "repo": "edwinhuish/uni-macros" }
{ "repo": "uni-ku/define-page" }
],
"commit": true,
"fixed": [],
Expand Down
217 changes: 213 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,216 @@
# uni-macros
# @uni-ku/define-page

用于 `uniapp` vue文件 `<script setup>` 内的宏定义的vite插件
`definePage` 宏,用于动态生成 `pages.json`

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

- [vite-plugin-pages-json](./packages/pages-json/README.md) 可使用 `json``function` 动态生成 `pages.json` 的宏(`definePage`
## 安装

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

## 配置

### vite 配置
```ts
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`

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

### vite 详细配置

```ts
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`

```ts
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](../playground/src/pages/define-page/)

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

JSON 对象
```ts
definePage({
style: {
navigationBarTitleText: 'hello world',
},
middlewares: [
'auth',
],
});
```

函数
```ts
definePage(() => {
const hello = 'hello';
const world = 'world';

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

嵌套函数
```ts
definePage(() => {
function getTitle(): string {
const hello = 'hello';
const world = 'world';

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

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

引入外部函数、变量
```ts
import { randamText } from './utils';

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

### 获取当前上下文的数据

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

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

## 感谢
- [vite-plugin-uni-pages](https://github.com/uni-helper/vite-plugin-uni-pages)
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "uni-macros",
"name": "@uni-ku/macros",
"type": "module",
"version": "0.0.2",
"version": "0.1.0",
"description": "",
"author": "Edwin Xu",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/pages-json/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# vite-plugin-pages-json
# @uni-ku/define-page

## 0.0.10

Expand Down
18 changes: 9 additions & 9 deletions packages/pages-json/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# vite-plugin-pages-json
# @uni-ku/define-page

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

Expand All @@ -10,21 +10,21 @@
## 安装

```shell
pnpm i -D vite-plugin-pages-json
pnpm i -D @uni-ku/define-page
```

## 配置

### vite 配置
```ts
import uni from '@dcloudio/vite-plugin-uni';
import { viteUniPagesJson } from '@uni-ku/define-page';
import { defineConfig } from 'vite';
import VitePagesJson from 'vite-plugin-pages-json';

export default defineConfig({
plugins: [
VitePagesJson(), // 详细配置请看下面的详细描述
uni(), // 添加在 VitePagesJson() 之后
viteUniPagesJson(), // 详细配置请看下面的详细描述
uni(), // 添加在 viteUniPagesJson() 之后
// 其他plugins
],
});
Expand All @@ -37,7 +37,7 @@ export default defineConfig({
```json
{
"compilerOptions": {
"types": ["vite-plugin-pages-json"]
"types": ["@uni-ku/define-page"]
}
}
```
Expand Down Expand Up @@ -106,9 +106,9 @@ export interface UserConfig {
将与 `definePage` 合并,生成最终的 `pages.json`

```ts
import { definePagesJson } from 'vite-plugin-pages-json';
import { UniPagesJson } from '@uni-ku/define-page';

export default definePagesJson({
export default UniPagesJson({
globalStyle: {
navigationBarTextStyle: 'black',
navigationBarTitleText: 'uni-app',
Expand Down Expand Up @@ -205,7 +205,7 @@ definePage(() => {
### 获取当前上下文的数据

```ts
import { ctx } from 'vite-plugin-pages-json';
import { ctx } from '@uni-ku/define-page';

console.log(ctx.files);
console.log(ctx.pages);
Expand Down
10 changes: 5 additions & 5 deletions packages/pages-json/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "vite-plugin-pages-json",
"name": "@uni-ku/define-page",
"type": "module",
"version": "0.0.11",
"version": "0.1.0",
"description": "Define uniapp pages.json dynamically.",
"author": "Edwin Xu <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/edwinhuish/uni-macros",
"homepage": "https://github.com/uni-ku/define-page",
"repository": {
"type": "git",
"url": "git+https://github.com/edwinhuish/uni-macros.git"
"url": "git+https://github.com/uni-ku/define-page.git"
},
"bugs": "https://github.com/edwinhuish/uni-macros/issues",
"bugs": "https://github.com/uni-ku/define-page/issues",
"keywords": [
"uniapp",
"uni-app",
Expand Down
2 changes: 1 addition & 1 deletion packages/pages-json/src/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function getDeclaration(pagesJSON: PagesJson) {
const code = `/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// Generated by vite-plugin-pages-json
// Generated by @uni-ku/define-page
interface NavigateToOptions {
url: ${allPagesPath.join(' |\n ')};
Expand Down
2 changes: 1 addition & 1 deletion packages/pages-json/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ async function exec<R = any>(file: string, exp: t.Expression, imports: t.ImportD
}

if (!fs.existsSync(tsx)) {
throw new Error(`[vite-plugin-pages-json] "tsx" is required parse macro expression value`);
throw new Error(`[@uni-ku/define-page] "tsx" is required parse macro expression value`);
}

const ast = t.file(t.program([
Expand Down
4 changes: 0 additions & 4 deletions packages/pages-json/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { viteDefinePagesJson } from './vite/plugin';

export * from './config';
export * from './constant';
export * from './context';
export * from './page';
export * from './pagesJson';
export * from './types';
export * from './vite/plugin';

export default viteDefinePagesJson;
2 changes: 1 addition & 1 deletion packages/pages-json/src/pagesJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getConfig } from './config';
import { PAGES_CONFIG_EXT, PAGES_CONFIG_FILE } from './constant';
import { debug } from './utils/debug';

export function definePagesJson(userConfig: Partial<PagesJson>) {
export function UniPagesJson(userConfig: Partial<PagesJson>) {
return userConfig;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/pages-json/src/utils/debug.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Debug from 'debug';

const PREFIX = 'define-pages-json:';
const PREFIX = 'define-page:';

const DEBUG_TYPES = [
'loadPagesConfig',
Expand Down
4 changes: 2 additions & 2 deletions packages/pages-json/src/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ async function restart() {
});
}

export function viteDefinePagesJson(userConfig: UserConfig = {}): Plugin {
export function viteUniPagesJson(userConfig: UserConfig = {}): Plugin {
ctx.init(userConfig);

checkPagesJsonFile();

return {
name: 'vite-plugin-pages-json',
name: '@uni-ku/define-page',
enforce: 'pre',
async configResolved(viteConf) {
if (!userConfig.root) {
Expand Down
1 change: 1 addition & 0 deletions packages/playground/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ dist
*.sw?

src/pages.json
src/pages.d.ts
Loading

0 comments on commit 9c8ff99

Please sign in to comment.