From 047085f2511bcc3e7222a84a689a7d2236a82aea Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Fri, 19 Apr 2024 14:34:20 +0800 Subject: [PATCH] =?UTF-8?q?wip(x-web):=20=E5=87=86=E5=A4=87=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 + packages/uni-h5/package.json | 2 + packages/uni-h5/src/index.ts | 9 +++ packages/uni-h5/src/x/service/api/index.ts | 2 + packages/uni-h5/src/x/service/api/uni.ts | 2 + packages/uni-h5/vite.config.ts | 87 +++++++++++++++++++++- packages/uni-runtime/src/index.ts | 1 + pnpm-lock.yaml | 25 ++++++- scripts/build.js | 33 ++++++-- 9 files changed, 150 insertions(+), 13 deletions(-) create mode 100644 packages/uni-h5/src/x/service/api/index.ts create mode 100644 packages/uni-h5/src/x/service/api/uni.ts diff --git a/package.json b/package.json index 4723c4bc36..73ef5696c5 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "@rollup/plugin-replace": "^5.0.2", "@rollup/plugin-strip": "^3.0.2", "@rollup/plugin-terser": "^0.2.1", + "@types/fs-extra": "^9.0.13", "@types/jest": "^29.2.3", "@types/node": "^18.16.2", "@types/react": "^18.2.67", @@ -72,6 +73,7 @@ "@vue/shared": "3.4.21", "core-js": "^2.6.12", "cypress": "^10.7.0", + "dotenv": "^16.4.5", "enquirer": "^2.3.6", "eslint": "^8.57.0", "eslint-plugin-import": "npm:eslint-plugin-i@^2.29.1", diff --git a/packages/uni-h5/package.json b/packages/uni-h5/package.json index 9ededec4f8..871dd5fe09 100644 --- a/packages/uni-h5/package.json +++ b/packages/uni-h5/package.json @@ -52,6 +52,8 @@ "acorn-loose": "^8.2.1", "acorn-walk": "^8.2.0", "estree-walker": "^2.0.2", + "fast-glob": "^3.2.11", + "fs-extra": "^10.0.0", "vue": "3.4.21" } } diff --git a/packages/uni-h5/src/index.ts b/packages/uni-h5/src/index.ts index a116033d7f..f028005ff1 100644 --- a/packages/uni-h5/src/index.ts +++ b/packages/uni-h5/src/index.ts @@ -82,8 +82,17 @@ export * from './view/components' export * from './view/bridge' +//#if _NEW_X_ +export * from './x/service/api' +export * from './x/service/api/uni' +//#endif + +//#if !_NEW_X_ export * from './service/api' +// @ts-expect-error export * from './service/api/uni' +//#endif + export * from './service/bridge' export { getApp, getCurrentPages } from './framework' diff --git a/packages/uni-h5/src/x/service/api/index.ts b/packages/uni-h5/src/x/service/api/index.ts new file mode 100644 index 0000000000..80d7cbb694 --- /dev/null +++ b/packages/uni-h5/src/x/service/api/index.ts @@ -0,0 +1,2 @@ +// @ts-expect-error +export * from '@dcloudio/uni-ext-api/uni-getLaunchOptionsSync' diff --git a/packages/uni-h5/src/x/service/api/uni.ts b/packages/uni-h5/src/x/service/api/uni.ts new file mode 100644 index 0000000000..7f00ce8e51 --- /dev/null +++ b/packages/uni-h5/src/x/service/api/uni.ts @@ -0,0 +1,2 @@ +import * as api from './index' +export const uni = api diff --git a/packages/uni-h5/vite.config.ts b/packages/uni-h5/vite.config.ts index 6d82683955..8b89d3cfca 100644 --- a/packages/uni-h5/vite.config.ts +++ b/packages/uni-h5/vite.config.ts @@ -1,6 +1,8 @@ +import fs from 'fs-extra' import path from 'path' -import { defineConfig } from 'vite' +import { type Plugin, defineConfig } from 'vite' +import { sync } from 'fast-glob' import jscc from 'rollup-plugin-jscc' import strip from '@rollup/plugin-strip' import replace from '@rollup/plugin-replace' @@ -39,6 +41,7 @@ const rollupPlugins = [ // 该插件限制了不能以__开头 _NODE_JS_: FORMAT === 'cjs' ? 1 : 0, _X_: isX ? 1 : 0, + _NEW_X_: isX && process.env.UNI_APP_EXT_API_DIR ? 1 : 0, }, }), ] @@ -105,6 +108,7 @@ export default defineConfig({ ], }, plugins: [ + ...(isX && process.env.UNI_APP_EXT_API_DIR ? [uts2ts()] : []), vue({ template: { compilerOptions: { @@ -168,3 +172,84 @@ export default defineConfig({ }, }, }) + +// if (!process.env.UNI_APP_EXT_API_DIR) { +// console.error(`UNI_APP_EXT_API_DIR is not defined`) +// process.exit(0) +// } + +const extApiDirTemp = path.resolve(__dirname, 'temp', 'uni-ext-api') + +function checkExtApiDir(name: string) { + if (fs.existsSync(path.resolve(extApiDirTemp, name))) { + return + } + const extApiDir = path.resolve(process.env.UNI_APP_EXT_API_DIR!) + + // 拷贝到临时目录 + fs.copySync(path.resolve(extApiDir, name), path.resolve(extApiDirTemp, name)) + // 重命名后缀 + sync('**/*.uts', { + absolute: true, + cwd: path.resolve(extApiDirTemp, name), + }).forEach((file) => { + fs.renameSync(file, file + '.ts') + }) +} + +function resolveExtApi(name: string) { + checkExtApiDir(name) + const filename = path.resolve( + extApiDirTemp, + name, + 'utssdk', + 'app-android', + 'index.uts.ts' + ) + return fs.existsSync(filename) + ? filename + : path.resolve(extApiDirTemp, name, 'utssdk', 'index.uts.ts') +} + +function uts2ts(): Plugin { + return { + name: 'uts2ts', + config() { + return { + resolve: { + extensions: [ + '.mjs', + '.js', + '.mts', + '.ts', + '.jsx', + '.tsx', + '.json', + '.uts.ts', + ], + alias: [ + { + find: '@dcloudio/uni-runtime', + replacement: resolve('../uni-runtime/src/index.ts'), + }, + { + find: /^@dcloudio\/uni-ext-api\/(.*)/, + replacement: '$1', + customResolver(source) { + return resolveExtApi(source) + }, + }, + ], + }, + } + }, + buildStart() { + // 清理临时目录 + fs.emptyDirSync(extApiDirTemp) + }, + buildEnd() { + // 清理临时目录 + fs.emptyDirSync(extApiDirTemp) + }, + } +} diff --git a/packages/uni-runtime/src/index.ts b/packages/uni-runtime/src/index.ts index cc8bc800e0..80a03548c7 100644 --- a/packages/uni-runtime/src/index.ts +++ b/packages/uni-runtime/src/index.ts @@ -1 +1,2 @@ export { isFunction, isString } from '@vue/shared' +export const __uniConfig = global.__uniConfig diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b3bded9a91..c484e441e0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,5 +1,9 @@ lockfileVersion: '6.0' +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + overrides: '@babel/plugin-transform-block-scoping': 7.19.4 @@ -55,6 +59,9 @@ importers: '@rollup/plugin-terser': specifier: ^0.2.1 version: 0.2.1(rollup@4.14.2) + '@types/fs-extra': + specifier: ^9.0.13 + version: 9.0.13 '@types/jest': specifier: ^29.2.3 version: 29.5.12 @@ -100,6 +107,9 @@ importers: cypress: specifier: ^10.7.0 version: 10.11.0 + dotenv: + specifier: ^16.4.5 + version: 16.4.5 enquirer: specifier: ^2.3.6 version: 2.4.1 @@ -950,6 +960,12 @@ importers: estree-walker: specifier: ^2.0.2 version: 2.0.2 + fast-glob: + specifier: ^3.2.11 + version: 3.3.2 + fs-extra: + specifier: ^10.0.0 + version: 10.1.0 vue: specifier: 3.4.21 version: 3.4.21(typescript@5.3.2) @@ -6386,6 +6402,11 @@ packages: webidl-conversions: 5.0.0 dev: false + /dotenv@16.4.5: + resolution: {integrity: sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==} + engines: {node: '>=12'} + dev: true + /ecc-jsbn@0.1.2: resolution: {integrity: sha512-eh9O+hwRHNbG4BLTjEl3nw044CkGm5X6LoaCf7LPp7UU8Qrt47JYNi6nPX8xjW97TKGKm1ouctg0QSpZe9qrnw==} dependencies: @@ -11487,7 +11508,3 @@ packages: optionalDependencies: commander: 9.5.0 dev: true - -settings: - autoInstallPeers: true - excludeLinksFromLockfile: false diff --git a/scripts/build.js b/scripts/build.js index dd6850aba2..190a237c15 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -4,6 +4,10 @@ const colors = require('picocolors') const execa = require('execa') const { spawn } = require('child_process') +const { config } = require('dotenv') + +config() + const { extract } = require('./apiExtractor') const { targets: allTargets, fuzzyMatchTarget, priority } = require('./utils') @@ -20,7 +24,7 @@ const transpileOnly = args.transpileOnly run() -async function run () { +async function run() { if (!targets.length) { await buildAll(allTargets) } else { @@ -28,7 +32,7 @@ async function run () { } } -function buildWithChildProcess (target) { +function buildWithChildProcess(target) { const args = [__filename, target] devOnly && args.push('-d') isRelease && args.push('--release') @@ -48,7 +52,7 @@ function buildWithChildProcess (target) { }) } -function getTargetGroup (targets) { +function getTargetGroup(targets) { const group = {} for (let i = 0; i < targets.length; i++) { const target = targets[i] @@ -62,7 +66,7 @@ function getTargetGroup (targets) { return group } -async function buildAll (targets) { +async function buildAll(targets) { if (!multiProcess) { for (const target of targets) { try { @@ -92,7 +96,7 @@ async function buildAll (targets) { } } -async function build (target) { +async function build(target) { console.log(`\n${colors.bold(target)}:`) const pkgDir = path.resolve(`packages/${target}`) const pkg = require(`${pkgDir}/package.json`) @@ -155,7 +159,11 @@ async function build (target) { ['build', '--config', path.resolve(pkgDir, 'vite.config.ts')], { stdio: 'inherit', - env: Object.assign({ FORMAT: 'es', UNI_APP_X: 'true' }, process.env, env), + env: Object.assign( + { FORMAT: 'es', UNI_APP_X: 'true' }, + process.env, + env + ), cwd: pkgDir, } ) @@ -164,7 +172,11 @@ async function build (target) { ['build', '--config', path.resolve(pkgDir, 'vite.config.ts')], { stdio: 'inherit', - env: Object.assign({ FORMAT: 'cjs', UNI_APP_X: 'true' }, process.env, env), + env: Object.assign( + { FORMAT: 'cjs', UNI_APP_X: 'true' }, + process.env, + env + ), cwd: pkgDir, } ) @@ -200,7 +212,12 @@ async function build (target) { [ '-c', '--environment', - [`NODE_ENV:${env}`, types ? `TYPES:true` : ``, `TARGET:${target}`, transpileOnly ? `TRANSPILE_ONLY:true` : ``] + [ + `NODE_ENV:${env}`, + types ? `TYPES:true` : ``, + `TARGET:${target}`, + transpileOnly ? `TRANSPILE_ONLY:true` : ``, + ] .filter(Boolean) .join(','), ], -- GitLab