diff --git a/packages/uni-app-plus/dist/uni.runtime.esm.js b/packages/uni-app-plus/dist/uni.runtime.esm.js index 78ec64b2d313d21ac60b360557793ab2558e6377..883823c8f1bb2d1cddac659ae3ec46b42fc1c975 100644 --- a/packages/uni-app-plus/dist/uni.runtime.esm.js +++ b/packages/uni-app-plus/dist/uni.runtime.esm.js @@ -17434,6 +17434,17 @@ function initUtsClassName(moduleName, className, is_uni_modules) { capitalize(className)); } return ''; +} +const pluginDefines = {}; +function registerUTSPlugin(name, define) { + pluginDefines[name] = define; +} +function requireUTSPlugin(name) { + const define = pluginDefines[name]; + if (!define) { + console.error(`${name} is not found`); + } + return define; } const EventType = { @@ -19556,6 +19567,8 @@ var uni$1 = { initUtsIndexClassName: initUtsIndexClassName, initUtsClassName: initUtsClassName, initUtsPackageName: initUtsPackageName, + requireUTSPlugin: requireUTSPlugin, + registerUTSPlugin: registerUTSPlugin, navigateTo: navigateTo, reLaunch: reLaunch, switchTab: switchTab, diff --git a/packages/uni-app-plus/src/service/api/index.ts b/packages/uni-app-plus/src/service/api/index.ts index 922bd582d1149cf0d150070cd42f60c1bc2220a7..07118fb78530c95c1ec31d229f9cb4ef9ca19f31 100644 --- a/packages/uni-app-plus/src/service/api/index.ts +++ b/packages/uni-app-plus/src/service/api/index.ts @@ -90,6 +90,8 @@ export { initUtsIndexClassName, initUtsClassName, initUtsPackageName, + requireUTSPlugin, + registerUTSPlugin, } from './plugin/uts' export * from './ad/rewardedVideoAd' diff --git a/packages/uni-app-plus/src/service/api/plugin/uts.ts b/packages/uni-app-plus/src/service/api/plugin/uts.ts index 0ef85ae347907df09487178702b53ee675b5f8fe..c393d65f17c7c8ee4ef64262271abd4206512949 100644 --- a/packages/uni-app-plus/src/service/api/plugin/uts.ts +++ b/packages/uni-app-plus/src/service/api/plugin/uts.ts @@ -387,3 +387,19 @@ export function initUtsClassName( } return '' } + +const pluginDefines: Record> = {} +export function registerUTSPlugin( + name: string, + define: Record +) { + pluginDefines[name] = define +} + +export function requireUTSPlugin(name: string) { + const define = pluginDefines[name] + if (!define) { + console.error(`${name} is not found`) + } + return define +} diff --git a/packages/uni-uts-v1/__tests__/__snapshots__/code.spec.ts.snap b/packages/uni-uts-v1/__tests__/__snapshots__/code.spec.ts.snap index 76a017a0a565572232f46f15b31acfd0ff345b28..da2e7f82a7e5a873ae2087882ffbecc62a4dc640 100644 --- a/packages/uni-uts-v1/__tests__/__snapshots__/code.spec.ts.snap +++ b/packages/uni-uts-v1/__tests__/__snapshots__/code.spec.ts.snap @@ -12,5 +12,26 @@ const cls = initUtsIndexClassName(name, is_uni_modules) export const onMemoryWarning = initUtsProxyFunction(false, { errMsg, main: true, package: pkg, class: cls, name: 'onMemoryWarning', params: [{"name":"callback","type":"UTSCallback"}]}) export const offMemoryWarning = initUtsProxyFunction(false, { errMsg, main: true, package: pkg, class: cls, name: 'offMemoryWarning', params: [{"name":"callback","type":"UTSCallback","default":"UTSNull"}]}) +export default initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, 'User', is_uni_modules) }, {"constructor":{"params":[]},"methods":{},"staticMethods":{},"props":[],"staticProps":[]} )) +export const a = 1 +" +`; + +exports[`code genProxyCode cjs 1`] = ` +" +import { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } from '@dcloudio/uni-app' +// const { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } = uni +const name = 'test-uts' +const errMsg = \`\` +const is_uni_modules = false +const pkg = initUtsPackageName(name, is_uni_modules) +const cls = initUtsIndexClassName(name, is_uni_modules) + +Object.defineProperty(exports, '__esModule', { value: true }) +exports.onMemoryWarning = initUtsProxyFunction(false, { errMsg, main: true, package: pkg, class: cls, name: 'onMemoryWarning', params: [{"name":"callback","type":"UTSCallback"}]}) +exports.offMemoryWarning = initUtsProxyFunction(false, { errMsg, main: true, package: pkg, class: cls, name: 'offMemoryWarning', params: [{"name":"callback","type":"UTSCallback","default":"UTSNull"}]}) +exports.default = initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, 'User', is_uni_modules) }, {"constructor":{"params":[]},"methods":{},"staticMethods":{},"props":[],"staticProps":[]} )) +const a = 1 +exports.a = a " `; diff --git a/packages/uni-uts-v1/__tests__/code.spec.ts b/packages/uni-uts-v1/__tests__/code.spec.ts index c63c3a49c7fe25a86cfa72a1a9a9c55fed66edf2..fc378235bce6a04369d62b928e60fe9e2153fdc1 100644 --- a/packages/uni-uts-v1/__tests__/code.spec.ts +++ b/packages/uni-uts-v1/__tests__/code.spec.ts @@ -1,5 +1,5 @@ import { resolve } from 'path' -import { genProxyCode } from '../src/code' +import { FORMATS, genProxyCode } from '../src/code' import { ERR_MSG_PLACEHOLDER } from '../src/utils' const pluginDir = resolve(__dirname, 'examples/uts/utssdk/test-uts') @@ -18,4 +18,18 @@ describe('code', () => { ).replace(ERR_MSG_PLACEHOLDER, '') ).toMatchSnapshot() }) + test('genProxyCode cjs', async () => { + expect( + ( + await genProxyCode(pluginDir, { + id: 'test-uts', + is_uni_modules: false, + name: 'test-uts', + namespace: 'uts.sdk.testUts', + extname: '.uts', + format: FORMATS.CJS, + }) + ).replace(ERR_MSG_PLACEHOLDER, '') + ).toMatchSnapshot() + }) }) diff --git a/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/utssdk/test-uts/manifest.json b/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/utssdk/test-uts/manifest.json index a4970bce880aded4dc28ee115eede4bafef36ee6..7ee43c3a93bc95380c106f6d486a480045f25c11 100644 --- a/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/utssdk/test-uts/manifest.json +++ b/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/utssdk/test-uts/manifest.json @@ -11,7 +11,7 @@ "md5": "d41d8cd98f00b204e9800998ecf8427e" }, "index.uts": { - "md5": "012e7488376fec3b6d104a0242a63535" + "md5": "0ab09e7091607dbfb059a9745160ad25" }, "package.json": { "md5": "9595031a0d4158abb72060cdf3a200c1" diff --git a/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/utssdk/test-uts/manifest.json b/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/utssdk/test-uts/manifest.json index a4970bce880aded4dc28ee115eede4bafef36ee6..7ee43c3a93bc95380c106f6d486a480045f25c11 100644 --- a/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/utssdk/test-uts/manifest.json +++ b/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/utssdk/test-uts/manifest.json @@ -11,7 +11,7 @@ "md5": "d41d8cd98f00b204e9800998ecf8427e" }, "index.uts": { - "md5": "012e7488376fec3b6d104a0242a63535" + "md5": "0ab09e7091607dbfb059a9745160ad25" }, "package.json": { "md5": "9595031a0d4158abb72060cdf3a200c1" diff --git a/packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/index.uts b/packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/index.uts index e251e7c28176104a27262e8d46195a09647de052..0b87ae73d06705503b6ab8fe86f3f73d57d4a8d4 100644 --- a/packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/index.uts +++ b/packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/index.uts @@ -1,4 +1,9 @@ -export function onMemoryWarning(callback: (level: number) => void) {} +export function onMemoryWarning(callback: (level: number) => void) { } export function offMemoryWarning( callback: ((level: number) => void) | null = null -) {} \ No newline at end of file +) { } +export default class User { + +} +export const a = 1 +export let b = 2, c = 3 \ No newline at end of file diff --git a/packages/uni-uts-v1/src/code.ts b/packages/uni-uts-v1/src/code.ts index a449ca9a66c7692bdca869e45b158989e9144df2..698ce421e62cb6af998b88bcfa347e49d3cf0ed7 100644 --- a/packages/uni-uts-v1/src/code.ts +++ b/packages/uni-uts-v1/src/code.ts @@ -13,10 +13,16 @@ import { Param, TsTypeAnnotation, VariableDeclaration, + VariableDeclarationKind, } from '../types/types' import { createResolveTypeReferenceName, ERR_MSG_PLACEHOLDER } from './utils' import { isInHBuilderX } from './shared' import { camelize, capitalize } from '@vue/shared' + +export const enum FORMATS { + ES = 'es', + CJS = 'cjs', +} interface GenProxyCodeOptions { is_uni_modules: boolean id: string @@ -25,13 +31,14 @@ interface GenProxyCodeOptions { namespace: string androidComponents?: Record iosComponents?: Record + format?: FORMATS } export async function genProxyCode( module: string, options: GenProxyCodeOptions ) { - const { name, is_uni_modules } = options + const { name, is_uni_modules, format } = options return ` import { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } from '@dcloudio/uni-app' // const { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } = uni @@ -44,7 +51,7 @@ ${genComponentsCode( options.androidComponents || {}, options.iosComponents || {} )} -${genModuleCode(await parseModuleDecls(module, options))} +${genModuleCode(await parseModuleDecls(module, options), format)} ` } @@ -92,19 +99,35 @@ export function resolvePlatformIndex( return fs.existsSync(filename) && filename } -function genModuleCode(decls: ProxyDecl[]) { +function exportDefaultCode(format: FORMATS) { + return format === FORMATS.ES ? 'export default ' : 'exports.default = ' +} + +function exportVarCode(format: FORMATS, kind: VariableDeclarationKind) { + if (format === FORMATS.ES) { + return `export ${kind} ` + } + return `exports.` +} + +function genModuleCode(decls: ProxyDecl[], format: FORMATS = FORMATS.ES) { const codes: string[] = [] + if (format === FORMATS.CJS) { + codes.push(`Object.defineProperty(exports, '__esModule', { value: true })`) + } + const exportDefault = exportDefaultCode(format) + const exportConst = exportVarCode(format, 'const') decls.forEach((decl) => { if (decl.type === 'Class') { if (decl.isDefault) { codes.push( - `export default initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, '${ + `${exportDefault}initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, '${ decl.cls }', is_uni_modules) }, ${JSON.stringify(decl.options)} ))` ) } else { codes.push( - `export const ${ + `${exportConst}${ decl.cls } = initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, '${ decl.cls @@ -114,7 +137,7 @@ function genModuleCode(decls: ProxyDecl[]) { } else if (decl.type === 'FunctionDeclaration') { if (decl.isDefault) { codes.push( - `export default initUtsProxyFunction(${ + `${exportDefault}nitUtsProxyFunction(${ decl.async }, { errMsg, main: true, package: pkg, class: cls, name: '${ decl.method @@ -122,7 +145,7 @@ function genModuleCode(decls: ProxyDecl[]) { ) } else { codes.push( - `export const ${decl.method} = initUtsProxyFunction(${ + `${exportConst}${decl.method} = initUtsProxyFunction(${ decl.async }, { errMsg, main: true, package: pkg, class: cls, name: '${ decl.method @@ -130,11 +153,28 @@ function genModuleCode(decls: ProxyDecl[]) { ) } } else if (decl.type === 'VariableDeclaration') { - codes.push( - `export ${decl.kind} ${decl.declarations - .map((d) => `${(d.id as Identifier).value} = ${genInitCode(d.init!)}`) - .join(', ')}` - ) + if (format === FORMATS.ES) { + codes.push( + `export ${decl.kind} ${decl.declarations + .map( + (d) => `${(d.id as Identifier).value} = ${genInitCode(d.init!)}` + ) + .join(', ')}` + ) + } else if (format === FORMATS.CJS) { + codes.push( + `${decl.kind} ${decl.declarations + .map( + (d) => `${(d.id as Identifier).value} = ${genInitCode(d.init!)}` + ) + .join(', ')}` + ) + const exportVar = exportVarCode(format, decl.kind) + decl.declarations.forEach((d) => { + const name = (d.id as Identifier).value + codes.push(`${exportVar}${name} = ${name}`) + }) + } } }) return codes.join(`\n`) diff --git a/packages/uni-uts-v1/src/index.ts b/packages/uni-uts-v1/src/index.ts index 9ac4dc4232bcc358d1e3ffc5eb32ed05bb455d34..6b1cf366e067d6275233fa07d36903ad2a65a9e7 100644 --- a/packages/uni-uts-v1/src/index.ts +++ b/packages/uni-uts-v1/src/index.ts @@ -15,6 +15,7 @@ import { } from './swift' import { + FORMATS, genProxyCode, resolvePlatformIndex, resolvePlatformIndexFilename, @@ -93,7 +94,17 @@ export async function compile(pluginDir: string) { const deps: string[] = [] const code = await genProxyCode( pluginDir, - extend({ androidComponents, iosComponents }, pkg) + extend( + { + androidComponents, + iosComponents, + format: + process.env.UNI_UTS_JS_CODE_FORMAT === 'cjs' + ? FORMATS.CJS + : FORMATS.ES, + }, + pkg + ) ) let errMsg = '' if (process.env.NODE_ENV !== 'development') {