提交 5b14e4bb 编写于 作者: fxy060608's avatar fxy060608

chore(test): easycom

上级 7d4f3fd6
......@@ -62,7 +62,7 @@ declare namespace UniApp {
alias?: string
meta: PageRouteMeta
component?: any
loader: () => Promise<any>
loader?: () => Promise<any>
}
type UniRoutes = UniRoute[]
......@@ -150,7 +150,7 @@ declare namespace UniApp {
onReachBottomDistance?: number
}
interface PageRouteMeta extends PagesJsonPageStyle {
id: number
id?: number
route: string
isNVue?: boolean
isQuit?: boolean
......
import path from 'path'
import { initEasycoms, matchEasycom } from '../src/utils'
import { initEasycoms, matchEasycom } from '../src/easycom'
const rootDir = path.resolve(__dirname, 'example')
const rootDir = path.resolve(__dirname, 'examples/easycom')
describe('easycom', () => {
test('initEasycom with dirs', () => {
......
import { normalizeIdentifier } from '../../utils'
import { normalizePagesRoute } from '../pages'
export function normalizeAppPagesJson(pagesJson: Record<string, any>) {
return polyfillCode + restoreGlobalCode + definePageCode(pagesJson)
......@@ -40,7 +41,7 @@ if(uni.restoreGlobal){
export function normalizeAppConfigService(pagesJson: UniApp.PagesJson) {
return `
;(function(){
const u=void 0;isReady=false,onReadyCallbacks=[],isServiceReady=false,onServiceReadyCallbacks=[];
const u=void 0,isReady=false,onReadyCallbacks=[],isServiceReady=false,onServiceReadyCallbacks=[];
const __uniConfig = ${normalizeAppUniConfig(pagesJson)};
const __uniRoutes = ${normalizeAppUniRoutes(pagesJson)};
__uniConfig.onReady=function(callback){if(__uniConfig.ready){callback()}else{onReadyCallbacks.push(callback)}};Object.defineProperty(__uniConfig,"ready",{get:function(){return isReady},set:function(val){isReady=val;if(!isReady){return}const callbacks=onReadyCallbacks.slice(0);onReadyCallbacks.length=0;callbacks.forEach(function(callback){callback()})}});
......@@ -56,24 +57,7 @@ function normalizeAppUniConfig(pagesJson: UniApp.PagesJson) {
return JSON.stringify(pagesJson.globalStyle)
}
function normalizeAppUniRoutes(pagesJson: UniApp.PagesJson) {
const uniRoutes: {
path: string
meta: Partial<UniApp.PageRouteMeta>
style: UniApp.PagesJsonPageStyle
}[] = []
pagesJson.pages.forEach((page) => {
uniRoutes.push(normalizeAppUniRoute(page))
})
return JSON.stringify(pagesJson)
}
function normalizeAppUniRoute(page: UniApp.PagesJsonPageOptions) {
const pageMeta: Partial<UniApp.PageRouteMeta> = {}
return {
path: page.path,
meta: pageMeta,
style: page.style,
}
return JSON.stringify(normalizePagesRoute(pagesJson))
}
function normalizeGlobalStatement(globals: string[]) {
......
......@@ -281,3 +281,38 @@ function removePlatformStyle(pageStyle: UniApp.PagesJsonPageStyle) {
})
return pageStyle
}
export function normalizePagesRoute(
pagesJson: UniApp.PagesJson
): UniApp.UniRoute[] {
const firstPagePath = pagesJson.pages[0].path
const tabBarList = (pagesJson.tabBar && pagesJson.tabBar.list) || []
return pagesJson.pages.map((pageOptions) => {
const pagePath = pageOptions.path
const isEntry = firstPagePath === pagePath ? true : undefined
const tabBarIndex = tabBarList.findIndex(
(tabBarPage: { pagePath: string }) => tabBarPage.pagePath === pagePath
)
const isTabBar = tabBarIndex !== -1 ? true : undefined
const isNVue = fs.existsSync(
path.join(process.env.UNI_INPUT_DIR, pagePath + '.nvue')
)
let windowTop = 0
const meta = extend(
{
route: pageOptions.path,
isNVue: isNVue ? true : undefined,
isQuit: isEntry || isTabBar ? true : undefined,
isEntry,
isTabBar,
tabBarIndex,
windowTop,
},
pageOptions.style
)
return {
path: pageOptions.path,
meta,
}
})
}
......@@ -4,10 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.uniPagesJsonPlugin = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const slash_1 = __importDefault(require("slash"));
const shared_1 = require("@vue/shared");
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
const pkg = require('@dcloudio/vite-plugin-uni/package.json');
function uniPagesJsonPlugin() {
......@@ -152,39 +150,12 @@ function generatePagesDefineCode(pagesJson, _config) {
}
` + pages.map((pageOptions) => generatePageDefineCode(pageOptions)).join('\n'));
}
function normalizePagesRoute(pagesJson) {
const firstPagePath = pagesJson.pages[0].path;
const tabBarList = (pagesJson.tabBar && pagesJson.tabBar.list) || [];
return pagesJson.pages.map((pageOptions) => {
const pagePath = pageOptions.path;
const name = uni_cli_shared_1.normalizeIdentifier(pagePath);
const isEntry = firstPagePath === pagePath ? true : undefined;
const tabBarIndex = tabBarList.findIndex((tabBarPage) => tabBarPage.pagePath === pagePath);
const isTabBar = tabBarIndex !== -1 ? true : undefined;
const isNVue = fs_1.default.existsSync(path_1.default.join(process.env.UNI_INPUT_DIR, pagePath + '.nvue'));
let windowTop = 0;
const meta = shared_1.extend({
route: pageOptions.path,
isNVue: isNVue ? true : undefined,
isQuit: isEntry || isTabBar ? true : undefined,
isEntry,
isTabBar,
tabBarIndex,
windowTop,
}, pageOptions.style);
return {
name,
path: pageOptions.path,
meta,
};
});
}
function generatePageRoute({ name, path, meta }, config) {
function generatePageRoute({ path, meta }, config) {
const { isEntry } = meta;
const alias = isEntry ? `\n alias:'/${path}',` : '';
return `{
path:'/${isEntry ? '' : path}',${alias}
component:{setup(){return ()=>renderPage(${name})}},
component:{setup(){return ()=>renderPage(${uni_cli_shared_1.normalizeIdentifier(path)})}},
loader: ${uni_cli_shared_1.normalizeIdentifier(path)}Loader,
meta: ${JSON.stringify(meta)}
}`;
......@@ -198,7 +169,7 @@ function renderPage(component){
return (openBlock(), createBlock(PageComponent, null, {page: withCtx(() => [createVNode(component, { ref: "page" }, null, 512 /* NEED_PATCH */)]), _: 1 /* STABLE */}))
}
${globalName}.__uniRoutes=[${[
...generatePagesRoute(normalizePagesRoute(pagesJson), config),
...generatePagesRoute(uni_cli_shared_1.normalizePagesRoute(pagesJson), config),
].join(',')}]`;
}
function generateConfig(globalName, pagesJson, config) {
......
import fs from 'fs'
import path from 'path'
import slash from 'slash'
import { Plugin, ResolvedConfig } from 'vite'
import { extend } from '@vue/shared'
import {
API_DEPS_CSS,
FEATURE_DEFINES,
......@@ -11,6 +9,7 @@ import {
normalizeIdentifier,
normalizePagesJson,
defineUniPagesJsonPlugin,
normalizePagesRoute,
} from '@dcloudio/uni-cli-shared'
const pkg = require('@dcloudio/vite-plugin-uni/package.json')
......@@ -37,12 +36,6 @@ export function uniPagesJsonPlugin(): Plugin {
})
}
interface PageRouteOptions {
name: string
path: string
meta: Partial<UniApp.PageRouteMeta>
}
function generatePagesJsonCode(
ssr: boolean | undefined,
jsonStr: string,
......@@ -190,57 +183,22 @@ function generatePagesDefineCode(
)
}
function normalizePagesRoute(pagesJson: UniApp.PagesJson): PageRouteOptions[] {
const firstPagePath = pagesJson.pages[0].path
const tabBarList = (pagesJson.tabBar && pagesJson.tabBar.list) || []
return pagesJson.pages.map((pageOptions) => {
const pagePath = pageOptions.path
const name = normalizeIdentifier(pagePath)
const isEntry = firstPagePath === pagePath ? true : undefined
const tabBarIndex = tabBarList.findIndex(
(tabBarPage: { pagePath: string }) => tabBarPage.pagePath === pagePath
)
const isTabBar = tabBarIndex !== -1 ? true : undefined
const isNVue = fs.existsSync(
path.join(process.env.UNI_INPUT_DIR, pagePath + '.nvue')
)
let windowTop = 0
const meta = extend(
{
route: pageOptions.path,
isNVue: isNVue ? true : undefined,
isQuit: isEntry || isTabBar ? true : undefined,
isEntry,
isTabBar,
tabBarIndex,
windowTop,
},
pageOptions.style
)
return {
name,
path: pageOptions.path,
meta,
}
})
}
function generatePageRoute(
{ name, path, meta }: PageRouteOptions,
{ path, meta }: UniApp.UniRoute,
config: ResolvedConfig
) {
const { isEntry } = meta
const alias = isEntry ? `\n alias:'/${path}',` : ''
return `{
path:'/${isEntry ? '' : path}',${alias}
component:{setup(){return ()=>renderPage(${name})}},
component:{setup(){return ()=>renderPage(${normalizeIdentifier(path)})}},
loader: ${normalizeIdentifier(path)}Loader,
meta: ${JSON.stringify(meta)}
}`
}
function generatePagesRoute(
pagesRouteOptions: PageRouteOptions[],
pagesRouteOptions: UniApp.UniRoute[],
config: ResolvedConfig
) {
return pagesRouteOptions.map((pageOptions) =>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册