Skip to content

Commit

Permalink
Merge pull request #81 from Ares-Chang/main
Browse files Browse the repository at this point in the history
  • Loading branch information
KeJunMao authored May 12, 2023
2 parents 02b954c + 6f4b3bb commit dc7e754
Show file tree
Hide file tree
Showing 19 changed files with 1,182 additions and 1,237 deletions.
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"version": "0.1.8",
"private": true,
"packageManager": "pnpm@8.2.0",
"packageManager": "pnpm@8.5.0",
"author": "KeJun",
"license": "MIT",
"homepage": "https://github.com/uni-helper/vite-plugin-uni-pages#readme",
Expand All @@ -30,14 +30,15 @@
"lint:fix": "pnpm lint --fix"
},
"devDependencies": {
"@antfu/eslint-config": "^0.38.4",
"@types/node": "^18.15.11",
"@antfu/eslint-config": "^0.38.6",
"@types/node": "^18.16.7",
"@uni-helper/volar-plugin-uni-pages": "workspace:*",
"bumpp": "^9.1.0",
"eslint": "^8.37.0",
"rimraf": "^4.4.1",
"typescript": "^5.0.3",
"unbuild": "^1.2.0",
"vitest": "^0.30.0"
"eslint": "^8.40.0",
"rimraf": "^5.0.0",
"typescript": "^5.0.4",
"unbuild": "^1.2.1",
"vite": "^4.3.5",
"vitest": "^0.31.0"
}
}
9 changes: 4 additions & 5 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,20 @@
"stub": "unbuild --stub"
},
"dependencies": {
"@uni-helper/uni-env": "^0.0.2",
"@vue/compiler-sfc": "^3.2.47",
"@uni-helper/uni-env": "^0.0.3",
"@vue/compiler-sfc": "^3.3.1",
"chokidar": "^3.5.3",
"debug": "^4.3.4",
"fast-glob": "^3.2.12",
"json5": "^2.2.3",
"lodash-unified": "^1.0.3",
"magic-string": "^0.30.0",
"unconfig": "^0.3.7",
"yaml": "^2.2.1"
"yaml": "^2.2.2"
},
"devDependencies": {
"@antfu/utils": "^0.7.2",
"@types/debug": "^4.1.7",
"@types/node": "^18.15.11",
"vite": "^4.2.1"
"@types/node": "^18.16.7"
}
}
63 changes: 53 additions & 10 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { isH5 } from '@uni-helper/uni-env'
import dbg from 'debug'
import type { PagesConfig } from './config/types'
import type { PageMetaDatum, PagePath, ResolvedOptions, SubPageMetaDatum, UserOptions } from './types'
import { debug, getPagesConfigSourcePaths, invalidatePagesModule, isConfigFile, isTargetFile, mergePageMetaDataArray } from './utils'
import {
debug,
getPagesConfigSourcePaths,
invalidatePagesModule,
isConfigFile,
isTargetFile,
mergePageMetaDataArray,
} from './utils'
import { resolveOptions } from './options'
import { checkPagesJsonFile, getPageFiles, writeFileSync } from './files'
import { getRouteBlock } from './customBlock'
Expand Down Expand Up @@ -47,7 +54,9 @@ export class PageContext {
debug.options(this.options)
}

setLogger(logger: Logger) { this.logger = logger }
setLogger(logger: Logger) {
this.logger = logger
}

async loadUserPagesConfig() {
const { config } = await loadConfig<PagesConfig>({ sources: [{ files: 'pages.config' }] })
Expand Down Expand Up @@ -137,7 +146,10 @@ export class PageContext {
const { relativePath, absolutePath } = page
const routeBlock = await getRouteBlock(absolutePath, this.options)
const relativePathWithFileName = relativePath.replace(path.extname(relativePath), '')
const pageMetaDatum: PageMetaDatum = { path: normalizePath(relativePathWithFileName), type: routeBlock?.attr.type ?? 'page' }
const pageMetaDatum: PageMetaDatum = {
path: normalizePath(relativePathWithFileName),
type: routeBlock?.attr.type ?? 'page',
}

if (routeBlock)
Object.assign(pageMetaDatum, routeBlock.content)
Expand All @@ -146,20 +158,52 @@ export class PageContext {
}

async parsePages(pages: PagePath[], overrides?: PageMetaDatum[]) {
const generatedPageMetaData = await Promise.all(
pages.map(async page => await this.parsePage(page)),
)
const generatedPageMetaData = await Promise.all(pages.map(async page => await this.parsePage(page)))
const customPageMetaData = overrides || []

const result = customPageMetaData.length
? mergePageMetaDataArray(generatedPageMetaData.concat(customPageMetaData))
: generatedPageMetaData

this.setHomePage(result)

result.sort(page => (page.type === 'home' ? -1 : 0))

return result
}

setHomePage(result: PageMetaDatum[]) {
const hasHome = result.some((page) => {
if (page.type === 'home')
return true

// Exclusion of subcontracting
const base = page.path.split('/')[0]
if (this.options.subPackages.includes(`src/${base}`))
return true

return false
})

if (hasHome)
return true

const isFoundHome = result.some((item) => {
if (this.options.homePage.includes(item.path)) {
item.type = 'home'
return true
}
else { return false }
})

if (isFoundHome) { return true }
else {
this.logger?.warn('No home page found, check the configuration of pages.config.ts, or add the `homePage` option to UniPages in vite.config.js, or add `type="home"` to the routeBlock of your vue page.', {
timestamp: true,
})
}
}

async mergePageMetaData() {
const pageMetaData = await this.parsePages(this.pagesPath, this.pagesGlobConfig?.pages)
this.pageMetaData = pageMetaData
Expand All @@ -184,9 +228,7 @@ export class PageContext {
subPageMaps[root] = pages || []
}

const subPageMetaData = Object
.keys(subPageMaps)
.map(root => ({ root, pages: subPageMaps[root] }))
const subPageMetaData = Object.keys(subPageMaps).map(root => ({ root, pages: subPageMaps[root] }))

this.subPageMetaData = subPageMetaData
debug.subPages(this.subPageMetaData)
Expand Down Expand Up @@ -238,7 +280,8 @@ function getPagePaths(dir: string, options: ResolvedOptions) {
const basePath = slash(path.join(options.root, options.outDir))
const files = getPageFiles(pagesDirPath, options)
debug.pages(dir, files)
const pagePaths = files.map(file => slash(file))
const pagePaths = files
.map(file => slash(file))
.map(file => ({
relativePath: path.relative(basePath, slash(path.resolve(pagesDirPath, file))),
absolutePath: slash(path.resolve(pagesDirPath, file)),
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs'
import fs from 'node:fs'
import fg from 'fast-glob'
import { extsToGlob } from './utils'

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import { spawn } from 'child_process'
import path from 'node:path'
import { spawn } from 'node:child_process'
import type { Plugin } from 'vite'
import { createLogger } from 'vite'
import MagicString from 'magic-string'
Expand Down Expand Up @@ -37,7 +37,7 @@ async function restart() {
})
}

export const VitePluginUniPages = (userOptions: UserOptions = {}): Plugin => {
export function VitePluginUniPages(userOptions: UserOptions = {}): Plugin {
let ctx: PageContext

// TODO: check if the pages.json file is valid
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ResolvedOptions, UserOptions } from './types'

export function resolveOptions(userOptions: UserOptions, viteRoot?: string): ResolvedOptions {
const {
homePage = ['pages/index', 'pages/index/index'],
mergePages = true,
dir = 'src/pages',
subPackages = [],
Expand All @@ -27,8 +28,10 @@ export function resolveOptions(userOptions: UserOptions, viteRoot?: string): Res
const root = viteRoot || slash(process.cwd())
const resolvedDirs = resolvePageDirs(dir, root, exclude)
const resolvedSubDirs = subPackages.map(dir => slash(dir))
const ResolvedHomePage = typeof homePage === 'string' ? [homePage] : homePage

const resolvedOptions: ResolvedOptions = {
homePage: ResolvedHomePage,
mergePages,
dirs: resolvedDirs,
subPackages: resolvedSubDirs,
Expand Down
13 changes: 11 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export interface CustomBlock {
export type debugType = keyof typeof debug

export interface Options {
/**
* The default application entry page is the home page
* @default 'pages/index' or 'pages/index/index'
*/
homePage: string

/**
* Whether to merge pages in pages.json
* @default true
Expand Down Expand Up @@ -69,7 +75,7 @@ export interface Options {

export type UserOptions = Partial<Options>

export interface ResolvedOptions extends Omit<Options, 'dir'> {
export interface ResolvedOptions extends Omit<Options, 'dir' | 'homePage'> {
/**
* Resolves to the `root` value from Vite config.
* @default config.root
Expand All @@ -80,7 +86,10 @@ export interface ResolvedOptions extends Omit<Options, 'dir'> {
* Resolved page dirs
*/
dirs: string[]

/**
* Resolved entry page
*/
homePage: string[]
}

export interface PagePath {
Expand Down
21 changes: 10 additions & 11 deletions packages/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@
"build:mp-weixin": "uni build -p mp-weixin"
},
"dependencies": {
"@dcloudio/uni-app": "3.0.0-alpha-3071220230331002",
"@dcloudio/uni-components": "3.0.0-alpha-3071220230331002",
"@dcloudio/uni-h5": "3.0.0-alpha-3071220230331002",
"@dcloudio/uni-mp-weixin": "3.0.0-alpha-3071220230331002",
"vue": "^3.2.45"
"@dcloudio/uni-app": "3.0.0-alpha-3080220230511001",
"@dcloudio/uni-components": "3.0.0-alpha-3080220230511001",
"@dcloudio/uni-h5": "3.0.0-alpha-3080220230511001",
"@dcloudio/uni-mp-weixin": "3.0.0-alpha-3080220230511001",
"vue": "^3.3.1"
},
"devDependencies": {
"@dcloudio/types": "^3.2.7",
"@dcloudio/uni-automator": "3.0.0-alpha-3071220230331002",
"@dcloudio/uni-cli-shared": "3.0.0-alpha-3071220230331002",
"@dcloudio/vite-plugin-uni": "3.0.0-alpha-3071220230331002",
"@dcloudio/types": "^3.3.3",
"@dcloudio/uni-automator": "3.0.0-alpha-3080220230511001",
"@dcloudio/uni-cli-shared": "3.0.0-alpha-3080220230511001",
"@dcloudio/vite-plugin-uni": "3.0.0-alpha-3080220230511001",
"@uni-helper/vite-plugin-uni-pages": "workspace:*",
"postcss": "^8.4.20",
"vite": "^4.0.3"
"postcss": "^8.4.23"
}
}
1 change: 0 additions & 1 deletion packages/playground/pages.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default defineUniPages({
navigationBarTextStyle: 'black',
navigationBarTitleText: 'uni-helper',
},
type: 'home',
},
],
})
7 changes: 7 additions & 0 deletions packages/playground/src/pages/A-top.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script setup lang="ts">
</script>

<template>
<div>A-Top page</div>
</template>
2 changes: 1 addition & 1 deletion packages/playground/src/pages/test-json.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div>test</div>
</template>

<route type="home" lang="jsonc">
<route lang="jsonc">
{
"style": {
"navigationBarTitleText": "test json page"
Expand Down
16 changes: 16 additions & 0 deletions packages/playground/src/pages/test.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script lang="ts" setup></script>

<template>
<div>test</div>
</template>

<route lang="jsonc">
{
"style": {
"navigationBarTitleText": "test page"
},
"middlewares": [
"auth"
]
}
</route>
12 changes: 8 additions & 4 deletions packages/playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import UniPages from '@uni-helper/vite-plugin-uni-pages'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [UniPages({
debug: true,
subPackages: ['src/pages-sub'],
}), uni()],
plugins: [
UniPages({
homePage: 'pages/index',
debug: true,
subPackages: ['src/pages-sub'],
}),
uni(),
],
})
2 changes: 1 addition & 1 deletion packages/volar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"dependencies": {
"@uni-helper/pages-json-schema": "workspace:^",
"ts-json-schema-generator": "^1.2.0",
"vscode-json-languageservice": "^5.2.0",
"vscode-json-languageservice": "^5.3.5",
"vscode-languageserver-protocol": "^3.17.3",
"vscode-languageserver-textdocument": "^1.0.8"
},
Expand Down
Loading

0 comments on commit dc7e754

Please sign in to comment.