diff --git a/package.json b/package.json index 627131480462666ba2d3907bb5043c5282316707..f7e16b26a4ff2f3834ea6bbe3ed68cc899931e94 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "build": "node scripts/build.js", "build:h5": "node scripts/build.js uni-app uni-cli-shared uni-h5 uni-i18n uni-stat uni-shared uni-h5-vite vite-plugin-uni", "build:app": "node scripts/build.js uni-app-plus uni-app-vite uni-app-vue uni-cli-nvue", - "build:mp": "node scripts/build.js uni-mp-vue uni-mp-alipay uni-mp-baidu uni-mp-qq uni-mp-toutiao uni-mp-weixin uni-mp-kuaishou uni-quickapp-webview", + "build:mp": "node scripts/build.js uni-mp-vue uni-mp-vite uni-mp-alipay uni-mp-baidu uni-mp-kuaishou uni-mp-lark uni-mp-qq uni-mp-toutiao uni-mp-weixin uni-quickapp-webview", "size": "npm run build size-check", "lint": "eslint packages/*/src/**/*.ts", "format": "prettier --write --parser typescript \"packages/**/*.ts?(x)\"", diff --git a/packages/shims-node.d.ts b/packages/shims-node.d.ts index bbd134b5563d0263c50798d6bbcbf99806f2ea12..927dd1cc316aaaf965f5b5cf82936be4ccf1f743 100644 --- a/packages/shims-node.d.ts +++ b/packages/shims-node.d.ts @@ -11,6 +11,7 @@ declare namespace NodeJS { UNI_OUTPUT_DIR: string UNI_CLI_CONTEXT: string UNI_SUBPACKAGE?: string + UNI_MP_PLUGIN?: 'true' UNI_COMPILER_VERSION: string UNI_COMPILER_VERSION_TYPE: 'a' | 'r' UNI_HBUILDERX_PLUGINS: string diff --git a/packages/shims-uni-app.d.ts b/packages/shims-uni-app.d.ts index 21717cc2ceff7f3b23fc1dfffe9c00ae744eeac4..8ea7580552ceba75617a6a0ffe37b48f8c3c3ee2 100644 --- a/packages/shims-uni-app.d.ts +++ b/packages/shims-uni-app.d.ts @@ -235,6 +235,12 @@ declare namespace UniApp { interface PagesJson { pages: PagesJsonPageOptions[] + preloadRule?: { + [page: string]: { + network?: 'all' | 'wifi' + packages: string[] + } + } subpackages?: PagesJsonSubpackagesOptions[] subPackages?: PagesJsonSubpackagesOptions[] globalStyle: PagesJsonPageStyle diff --git a/packages/uni-cli-shared/src/env/define.ts b/packages/uni-cli-shared/src/env/define.ts index 7454c60f62b78981ddf45e6196b716eaa88f6e87..68c59322edaccd096235c00637578c3d5edee85b 100644 --- a/packages/uni-cli-shared/src/env/define.ts +++ b/packages/uni-cli-shared/src/env/define.ts @@ -13,6 +13,7 @@ export function initDefine(stringifyBoolean: boolean = false) { 'process.env.UNI_APP_ID': JSON.stringify(manifestJson.appid || ''), 'process.env.UNI_APP_NAME': JSON.stringify(manifestJson.name || ''), 'process.env.UNI_PLATFORM': JSON.stringify(process.env.UNI_PLATFORM), + 'process.env.UNI_MP_PLUGIN': JSON.stringify(process.env.UNI_MP_PLUGIN), 'process.env.UNI_SUBPACKAGE': JSON.stringify(process.env.UNI_SUBPACKAGE), 'process.env.UNI_COMPILER_VERSION': JSON.stringify( process.env.UNI_COMPILER_VERSION diff --git a/packages/uni-cli-shared/src/messages/index.ts b/packages/uni-cli-shared/src/messages/index.ts index a2ed24645d12bf35587dd316704917c53e177d00..b2d636998e021219664ed678f6746884d48e4485 100644 --- a/packages/uni-cli-shared/src/messages/index.ts +++ b/packages/uni-cli-shared/src/messages/index.ts @@ -21,4 +21,5 @@ export const M = { 'mp.component.args[0]': '{0}的第一个参数必须为静态字符串', 'mp.component.args[1]': '{0}需要两个参数', 'mp.360.unsupported': 'vue3暂不支持360小程序', + 'file.notfound': '{file} 文件不存在', } diff --git a/packages/uni-cli-shared/src/mp/constants.ts b/packages/uni-cli-shared/src/mp/constants.ts index a5ea2c3f210d822ae8a776c0f7b768277b828505..a4bea26fdfe219a142efb782242dbd4d855f449b 100644 --- a/packages/uni-cli-shared/src/mp/constants.ts +++ b/packages/uni-cli-shared/src/mp/constants.ts @@ -2,3 +2,4 @@ export const COMPONENT_ON_LINK = 'onVI' export const COMPONENT_BIND_LINK = '__l' export const COMPONENT_CUSTOM_HIDDEN = 'data-c-h' export const COMPONENT_CUSTOM_HIDDEN_BIND = 'bind:-' + COMPONENT_CUSTOM_HIDDEN +export const MP_PLUGIN_JSON_NAME = 'plugin.json' diff --git a/packages/uni-cli-shared/src/mp/index.ts b/packages/uni-cli-shared/src/mp/index.ts index 8fd3c1573b2367d04847968a2b67df2639db5012..6b26f71c4ffede8347e104d4acd30b68d25f283d 100644 --- a/packages/uni-cli-shared/src/mp/index.ts +++ b/packages/uni-cli-shared/src/mp/index.ts @@ -3,4 +3,5 @@ export * from './event' export * from './style' export * from './template' export * from './constants' +export { copyMiniProgramPluginJson } from './plugin' export { transformVueComponentImports } from './transformImports' diff --git a/packages/uni-cli-shared/src/mp/plugin.ts b/packages/uni-cli-shared/src/mp/plugin.ts new file mode 100644 index 0000000000000000000000000000000000000000..3315ac39fc43f1b69fb978d2985c5b62abd57bed --- /dev/null +++ b/packages/uni-cli-shared/src/mp/plugin.ts @@ -0,0 +1,12 @@ +import { UniViteCopyPluginTarget } from '../vite/plugins/copy' +import { parseJson } from '../json/json' + +export const copyMiniProgramPluginJson: UniViteCopyPluginTarget = { + src: ['plugin.json'], + get dest() { + return process.env.UNI_OUTPUT_DIR + }, + transform(source) { + return JSON.stringify(parseJson(source.toString(), true), null, 2) + }, +} diff --git a/packages/uni-mp-alipay/dist/uni.compiler.js b/packages/uni-mp-alipay/dist/uni.compiler.js index 347a9b8692caefb12b976596d9aba6d1ebe6c973..c02bddb6911105ef35fc19059f85d08ae07af412 100644 --- a/packages/uni-mp-alipay/dist/uni.compiler.js +++ b/packages/uni-mp-alipay/dist/uni.compiler.js @@ -170,6 +170,7 @@ const options = { }, copyOptions: { assets: ['mycomponents'], + targets: process.env.UNI_MP_PLUGIN ? [uniCliShared.copyMiniProgramPluginJson] : [], }, }, global: 'my', @@ -203,6 +204,7 @@ const options = { app: { darkmode: false, subpackages: true, + plugins: true, }, project: { filename: projectConfigFilename, diff --git a/packages/uni-mp-alipay/dist/uni.mp.esm.js b/packages/uni-mp-alipay/dist/uni.mp.esm.js index 0925fb6ee6e1f1b58d436b825bf14bb327ea2734..d387201cc09d94a95c5e966fb1e8a3124d6cd0ab 100644 --- a/packages/uni-mp-alipay/dist/uni.mp.esm.js +++ b/packages/uni-mp-alipay/dist/uni.mp.esm.js @@ -362,23 +362,25 @@ function initCreateSubpackageApp(parseAppOptions) { app[name] = appOptions[name]; } }); - if (isFunction(appOptions.onShow) && my.onAppShow) { - my.onAppShow((args) => { - vm.$callHook('onShow', args); - }); - } - if (isFunction(appOptions.onHide) && my.onAppHide) { - my.onAppHide((args) => { - vm.$callHook('onHide', args); - }); - } - if (isFunction(appOptions.onLaunch)) { - const args = my.getLaunchOptionsSync && my.getLaunchOptionsSync(); - vm.$callHook('onLaunch', args); - } - return App(appOptions); + initAppLifecycle(appOptions, vm); }; } +function initAppLifecycle(appOptions, vm) { + if (isFunction(appOptions.onShow) && my.onAppShow) { + my.onAppShow((args) => { + vm.$callHook('onShow', args); + }); + } + if (isFunction(appOptions.onHide) && my.onAppHide) { + my.onAppHide((args) => { + vm.$callHook('onHide', args); + }); + } + if (isFunction(appOptions.onLaunch)) { + const args = my.getLaunchOptionsSync && my.getLaunchOptionsSync(); + vm.$callHook('onLaunch', args || {}); + } +} function initLocale(appVm) { const locale = ref(my.getSystemInfoSync().language || 'zh-Hans'); Object.defineProperty(appVm, '$locale', { @@ -617,6 +619,12 @@ function $destroyComponent(instance) { return $destroyComponentFn(instance); } +function initCreatePluginApp(parseAppOptions) { + return function createApp(vm) { + initAppLifecycle(parseApp(vm, parseAppOptions), vm); + }; +} + function onAliAuthError(method, $event) { $event.type = 'getphonenumber'; $event.detail.errMsg = @@ -1055,11 +1063,13 @@ function initCreateComponent() { const createApp = initCreateApp(parseAppOptions); const createPage = initCreatePage(); const createComponent = initCreateComponent(); +const createPluginApp = initCreatePluginApp(parseAppOptions); const createSubpackageApp = initCreateSubpackageApp(parseAppOptions); my.EventChannel = EventChannel; my.createApp = createApp; my.createPage = createPage; my.createComponent = createComponent; +my.createPluginApp = createPluginApp; my.createSubpackageApp = createSubpackageApp; -export { createApp, createComponent, createPage, createSubpackageApp }; +export { createApp, createComponent, createPage, createPluginApp, createSubpackageApp }; diff --git a/packages/uni-mp-alipay/src/compiler/options.ts b/packages/uni-mp-alipay/src/compiler/options.ts index 64886ba66409ac3b5264a7420bdb90539f940c2d..6db1c2d783207204aa37b320c639ad45bcf5f80f 100644 --- a/packages/uni-mp-alipay/src/compiler/options.ts +++ b/packages/uni-mp-alipay/src/compiler/options.ts @@ -2,6 +2,7 @@ import path from 'path' import { CompilerOptions, NodeTypes } from '@vue/compiler-core' import { COMPONENT_ON_LINK, + copyMiniProgramPluginJson, createTransformComponentLink, MiniProgramCompilerOptions, transformMatchMedia, @@ -61,6 +62,7 @@ export const options: UniMiniProgramPluginOptions = { }, copyOptions: { assets: ['mycomponents'], + targets: process.env.UNI_MP_PLUGIN ? [copyMiniProgramPluginJson] : [], }, }, global: 'my', @@ -94,6 +96,7 @@ export const options: UniMiniProgramPluginOptions = { app: { darkmode: false, subpackages: true, + plugins: true, }, project: { filename: projectConfigFilename, diff --git a/packages/uni-mp-alipay/src/runtime/index.ts b/packages/uni-mp-alipay/src/runtime/index.ts index 1f44c8c7d4608d8011cd4919b0ef1aba5c67acd4..0c9dd43d0067fcb1e0698c12f1fcc99d00db97aa 100644 --- a/packages/uni-mp-alipay/src/runtime/index.ts +++ b/packages/uni-mp-alipay/src/runtime/index.ts @@ -1,6 +1,10 @@ import { EventChannel } from '@dcloudio/uni-shared' -import { initCreateApp, initCreateSubpackageApp } from '@dcloudio/uni-mp-core' +import { + initCreateApp, + initCreatePluginApp, + initCreateSubpackageApp, +} from '@dcloudio/uni-mp-core' import * as parseAppOptions from './parseAppOptions' @@ -9,9 +13,11 @@ import { initCreateComponent } from './createComponent' export const createApp = initCreateApp(parseAppOptions) export const createPage = initCreatePage() export const createComponent = initCreateComponent() +export const createPluginApp = initCreatePluginApp(parseAppOptions) export const createSubpackageApp = initCreateSubpackageApp(parseAppOptions) ;(my as any).EventChannel = EventChannel ;(my as any).createApp = createApp ;(my as any).createPage = createPage ;(my as any).createComponent = createComponent +;(my as any).createPluginApp = createPluginApp ;(my as any).createSubpackageApp = createSubpackageApp diff --git a/packages/uni-mp-baidu/dist/uni.mp.esm.js b/packages/uni-mp-baidu/dist/uni.mp.esm.js index 0ac676915bf615874f07191d485464e4cc9837aa..db34f4da47870e295155fd01aad2b9cb4ac5402d 100644 --- a/packages/uni-mp-baidu/dist/uni.mp.esm.js +++ b/packages/uni-mp-baidu/dist/uni.mp.esm.js @@ -421,23 +421,25 @@ function initCreateSubpackageApp(parseAppOptions) { app[name] = appOptions[name]; } }); - if (isFunction(appOptions.onShow) && swan.onAppShow) { - swan.onAppShow((args) => { - vm.$callHook('onShow', args); - }); - } - if (isFunction(appOptions.onHide) && swan.onAppHide) { - swan.onAppHide((args) => { - vm.$callHook('onHide', args); - }); - } - if (isFunction(appOptions.onLaunch)) { - const args = swan.getLaunchOptionsSync && swan.getLaunchOptionsSync(); - vm.$callHook('onLaunch', args); - } - return App(appOptions); + initAppLifecycle(appOptions, vm); }; } +function initAppLifecycle(appOptions, vm) { + if (isFunction(appOptions.onShow) && swan.onAppShow) { + swan.onAppShow((args) => { + vm.$callHook('onShow', args); + }); + } + if (isFunction(appOptions.onHide) && swan.onAppHide) { + swan.onAppHide((args) => { + vm.$callHook('onHide', args); + }); + } + if (isFunction(appOptions.onLaunch)) { + const args = swan.getLaunchOptionsSync && swan.getLaunchOptionsSync(); + vm.$callHook('onLaunch', args || {}); + } +} function initLocale(appVm) { const locale = ref(swan.getSystemInfoSync().language || 'zh-Hans'); Object.defineProperty(appVm, '$locale', { diff --git a/packages/uni-mp-core/src/index.ts b/packages/uni-mp-core/src/index.ts index d81a83edd4abef9c0506588a53da77e6569eeac7..268da6f11e0de2a73b1ba1bb4e65153fcdc4696b 100644 --- a/packages/uni-mp-core/src/index.ts +++ b/packages/uni-mp-core/src/index.ts @@ -10,6 +10,7 @@ declare module '@vue/runtime-core' { export { initCreateApp, initCreateSubpackageApp } from './runtime/app' export { initCreatePage } from './runtime/page' export { initCreateComponent } from './runtime/component' +export { initCreatePluginApp } from './runtime/plugin' export { initUni } from './api/index' export { initGetProvider } from './api/shims' diff --git a/packages/uni-mp-core/src/runtime/app.ts b/packages/uni-mp-core/src/runtime/app.ts index f1f349d35804a60ddec24ad03867d1677dd50132..f609e865a46ded94eea32a741e9dd83df3c043c0 100644 --- a/packages/uni-mp-core/src/runtime/app.ts +++ b/packages/uni-mp-core/src/runtime/app.ts @@ -42,7 +42,7 @@ export interface ParseAppOptions { parse: (appOptions: MiniProgramAppOptions) => void } -function parseApp( +export function parseApp( instance: ComponentPublicInstance, parseAppOptions?: ParseAppOptions ) { @@ -121,22 +121,28 @@ export function initCreateSubpackageApp(parseAppOptions?: ParseAppOptions) { app[name] = appOptions[name] } }) - if (isFunction(appOptions.onShow) && __GLOBAL__.onAppShow) { - __GLOBAL__.onAppShow((args: unknown) => { - vm.$callHook('onShow', args) - }) - } - if (isFunction(appOptions.onHide) && __GLOBAL__.onAppHide) { - __GLOBAL__.onAppHide((args: unknown) => { - vm.$callHook('onHide', args) - }) - } - if (isFunction(appOptions.onLaunch)) { - const args = - __GLOBAL__.getLaunchOptionsSync && __GLOBAL__.getLaunchOptionsSync() - vm.$callHook('onLaunch', args) - } - return App(appOptions) + initAppLifecycle(appOptions, vm) + } +} + +export function initAppLifecycle( + appOptions: MiniProgramAppOptions, + vm: ComponentPublicInstance +) { + if (isFunction(appOptions.onShow) && __GLOBAL__.onAppShow) { + __GLOBAL__.onAppShow((args: unknown) => { + vm.$callHook('onShow', args) + }) + } + if (isFunction(appOptions.onHide) && __GLOBAL__.onAppHide) { + __GLOBAL__.onAppHide((args: unknown) => { + vm.$callHook('onHide', args) + }) + } + if (isFunction(appOptions.onLaunch)) { + const args = + __GLOBAL__.getLaunchOptionsSync && __GLOBAL__.getLaunchOptionsSync() + vm.$callHook('onLaunch', args || {}) } } diff --git a/packages/uni-mp-core/src/runtime/plugin.ts b/packages/uni-mp-core/src/runtime/plugin.ts new file mode 100644 index 0000000000000000000000000000000000000000..4220c2aac331b1e9d59cc0aaba30d52bf7129049 --- /dev/null +++ b/packages/uni-mp-core/src/runtime/plugin.ts @@ -0,0 +1,8 @@ +import { ComponentPublicInstance } from 'vue' +import { initAppLifecycle, parseApp, ParseAppOptions } from './app' + +export function initCreatePluginApp(parseAppOptions?: ParseAppOptions) { + return function createApp(vm: ComponentPublicInstance) { + initAppLifecycle(parseApp(vm, parseAppOptions), vm) + } +} diff --git a/packages/uni-mp-kuaishou/dist/uni.mp.esm.js b/packages/uni-mp-kuaishou/dist/uni.mp.esm.js index 20b5144a7f7b632aa7f58ce112347547eea78ba2..4f11ae3be6e79f1fb18abf64254cc46407e40e7f 100644 --- a/packages/uni-mp-kuaishou/dist/uni.mp.esm.js +++ b/packages/uni-mp-kuaishou/dist/uni.mp.esm.js @@ -416,23 +416,25 @@ function initCreateSubpackageApp(parseAppOptions) { app[name] = appOptions[name]; } }); - if (isFunction(appOptions.onShow) && ks.onAppShow) { - ks.onAppShow((args) => { - vm.$callHook('onShow', args); - }); - } - if (isFunction(appOptions.onHide) && ks.onAppHide) { - ks.onAppHide((args) => { - vm.$callHook('onHide', args); - }); - } - if (isFunction(appOptions.onLaunch)) { - const args = ks.getLaunchOptionsSync && ks.getLaunchOptionsSync(); - vm.$callHook('onLaunch', args); - } - return App(appOptions); + initAppLifecycle(appOptions, vm); }; } +function initAppLifecycle(appOptions, vm) { + if (isFunction(appOptions.onShow) && ks.onAppShow) { + ks.onAppShow((args) => { + vm.$callHook('onShow', args); + }); + } + if (isFunction(appOptions.onHide) && ks.onAppHide) { + ks.onAppHide((args) => { + vm.$callHook('onHide', args); + }); + } + if (isFunction(appOptions.onLaunch)) { + const args = ks.getLaunchOptionsSync && ks.getLaunchOptionsSync(); + vm.$callHook('onLaunch', args || {}); + } +} function initLocale(appVm) { const locale = ref(ks.getSystemInfoSync().language || 'zh-Hans'); Object.defineProperty(appVm, '$locale', { diff --git a/packages/uni-mp-lark/dist/uni.mp.esm.js b/packages/uni-mp-lark/dist/uni.mp.esm.js index d8790d0a861ceea6483fd5ea5a8b586e4969dee7..c36431c36b60813b024427e37a048b51d43bc8a8 100644 --- a/packages/uni-mp-lark/dist/uni.mp.esm.js +++ b/packages/uni-mp-lark/dist/uni.mp.esm.js @@ -415,23 +415,25 @@ function initCreateSubpackageApp(parseAppOptions) { app[name] = appOptions[name]; } }); - if (isFunction(appOptions.onShow) && tt.onAppShow) { - tt.onAppShow((args) => { - vm.$callHook('onShow', args); - }); - } - if (isFunction(appOptions.onHide) && tt.onAppHide) { - tt.onAppHide((args) => { - vm.$callHook('onHide', args); - }); - } - if (isFunction(appOptions.onLaunch)) { - const args = tt.getLaunchOptionsSync && tt.getLaunchOptionsSync(); - vm.$callHook('onLaunch', args); - } - return App(appOptions); + initAppLifecycle(appOptions, vm); }; } +function initAppLifecycle(appOptions, vm) { + if (isFunction(appOptions.onShow) && tt.onAppShow) { + tt.onAppShow((args) => { + vm.$callHook('onShow', args); + }); + } + if (isFunction(appOptions.onHide) && tt.onAppHide) { + tt.onAppHide((args) => { + vm.$callHook('onHide', args); + }); + } + if (isFunction(appOptions.onLaunch)) { + const args = tt.getLaunchOptionsSync && tt.getLaunchOptionsSync(); + vm.$callHook('onLaunch', args || {}); + } +} function initLocale(appVm) { const locale = ref(tt.getSystemInfoSync().language || 'zh-Hans'); Object.defineProperty(appVm, '$locale', { diff --git a/packages/uni-mp-qq/dist/uni.mp.esm.js b/packages/uni-mp-qq/dist/uni.mp.esm.js index cce1ebf76561ec2ee924b3bb340ffc1bad115d8c..6c9ba026dbde7c3d576da9f6d66ae8df1a636f59 100644 --- a/packages/uni-mp-qq/dist/uni.mp.esm.js +++ b/packages/uni-mp-qq/dist/uni.mp.esm.js @@ -412,23 +412,25 @@ function initCreateSubpackageApp(parseAppOptions) { app[name] = appOptions[name]; } }); - if (isFunction(appOptions.onShow) && qq.onAppShow) { - qq.onAppShow((args) => { - vm.$callHook('onShow', args); - }); - } - if (isFunction(appOptions.onHide) && qq.onAppHide) { - qq.onAppHide((args) => { - vm.$callHook('onHide', args); - }); - } - if (isFunction(appOptions.onLaunch)) { - const args = qq.getLaunchOptionsSync && qq.getLaunchOptionsSync(); - vm.$callHook('onLaunch', args); - } - return App(appOptions); + initAppLifecycle(appOptions, vm); }; } +function initAppLifecycle(appOptions, vm) { + if (isFunction(appOptions.onShow) && qq.onAppShow) { + qq.onAppShow((args) => { + vm.$callHook('onShow', args); + }); + } + if (isFunction(appOptions.onHide) && qq.onAppHide) { + qq.onAppHide((args) => { + vm.$callHook('onHide', args); + }); + } + if (isFunction(appOptions.onLaunch)) { + const args = qq.getLaunchOptionsSync && qq.getLaunchOptionsSync(); + vm.$callHook('onLaunch', args || {}); + } +} function initLocale(appVm) { const locale = ref(qq.getSystemInfoSync().language || 'zh-Hans'); Object.defineProperty(appVm, '$locale', { @@ -804,6 +806,12 @@ function initCreatePage(parseOptions) { }; } +function initCreatePluginApp(parseAppOptions) { + return function createApp(vm) { + initAppLifecycle(parseApp(vm, parseAppOptions), vm); + }; +} + const MPPage = Page; const MPComponent = Component; const customizeRE = /:/g; @@ -919,10 +927,12 @@ var parseOptions = /*#__PURE__*/Object.freeze({ const createApp = initCreateApp(); const createPage = initCreatePage(parseOptions); const createComponent = initCreateComponent(parseOptions); +const createPluginApp = initCreatePluginApp(); const createSubpackageApp = initCreateSubpackageApp(); wx.createApp = global.createApp = createApp; wx.createPage = createPage; wx.createComponent = createComponent; +wx.createPluginApp = createPluginApp; wx.createSubpackageApp = createSubpackageApp; qq.EventChannel = EventChannel$1; @@ -931,4 +941,4 @@ qq.createPage = createPage; qq.createComponent = createComponent; qq.createSubpackageApp = createSubpackageApp; -export { createApp, createComponent, createPage, createSubpackageApp }; +export { createApp, createComponent, createPage, createPluginApp, createSubpackageApp }; diff --git a/packages/uni-mp-toutiao/dist/uni.mp.esm.js b/packages/uni-mp-toutiao/dist/uni.mp.esm.js index bb975214684426043fb7b5782f1deb3bfd49c637..73a3f457f322725f50d979d9745070fe55a4ffaa 100644 --- a/packages/uni-mp-toutiao/dist/uni.mp.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.mp.esm.js @@ -415,23 +415,25 @@ function initCreateSubpackageApp(parseAppOptions) { app[name] = appOptions[name]; } }); - if (isFunction(appOptions.onShow) && tt.onAppShow) { - tt.onAppShow((args) => { - vm.$callHook('onShow', args); - }); - } - if (isFunction(appOptions.onHide) && tt.onAppHide) { - tt.onAppHide((args) => { - vm.$callHook('onHide', args); - }); - } - if (isFunction(appOptions.onLaunch)) { - const args = tt.getLaunchOptionsSync && tt.getLaunchOptionsSync(); - vm.$callHook('onLaunch', args); - } - return App(appOptions); + initAppLifecycle(appOptions, vm); }; } +function initAppLifecycle(appOptions, vm) { + if (isFunction(appOptions.onShow) && tt.onAppShow) { + tt.onAppShow((args) => { + vm.$callHook('onShow', args); + }); + } + if (isFunction(appOptions.onHide) && tt.onAppHide) { + tt.onAppHide((args) => { + vm.$callHook('onHide', args); + }); + } + if (isFunction(appOptions.onLaunch)) { + const args = tt.getLaunchOptionsSync && tt.getLaunchOptionsSync(); + vm.$callHook('onLaunch', args || {}); + } +} function initLocale(appVm) { const locale = ref(tt.getSystemInfoSync().language || 'zh-Hans'); Object.defineProperty(appVm, '$locale', { diff --git a/packages/uni-mp-vite/src/index.ts b/packages/uni-mp-vite/src/index.ts index 7f51627a7d230f621a90885c14f7d85af8f6cc0c..2e240b0cc502d09f824663c8ae0db310f5e74f20 100644 --- a/packages/uni-mp-vite/src/index.ts +++ b/packages/uni-mp-vite/src/index.ts @@ -11,13 +11,16 @@ import { uniEntryPlugin } from './plugins/entry' import { uniRenderjsPlugin } from './plugins/renderjs' import { uniSubpackagePlugin } from './plugins/subpackage' +import { uniMiniProgramPluginPlugin } from './plugins/plugin' export { UniMiniProgramPluginOptions } from './plugin' export default (options: UniMiniProgramPluginOptions) => { if (!options.app.subpackages) { delete process.env.UNI_SUBPACKAGE } - + if (!options.app.plugins) { + delete process.env.UNI_MP_PLUGIN + } return [ (options: { vueOptions?: { script?: Partial } @@ -36,5 +39,6 @@ export default (options: UniMiniProgramPluginOptions) => { return uniUsingComponentsPlugin(options.vueOptions?.script) }, ...(process.env.UNI_SUBPACKAGE ? [uniSubpackagePlugin(options)] : []), + ...(process.env.UNI_MP_PLUGIN ? [uniMiniProgramPluginPlugin(options)] : []), ] } diff --git a/packages/uni-mp-vite/src/plugin/build.ts b/packages/uni-mp-vite/src/plugin/build.ts index 5bafdfd8d1ac844c6f6c3cf45d79f6e5306c5473..5a66c28e1b9d1f33257c5287729309f228dbfe3e 100644 --- a/packages/uni-mp-vite/src/plugin/build.ts +++ b/packages/uni-mp-vite/src/plugin/build.ts @@ -1,7 +1,7 @@ import fs from 'fs' import path from 'path' import debug from 'debug' -import { UserConfig } from 'vite' +import { BuildOptions, UserConfig } from 'vite' import { emptyDir, @@ -12,6 +12,8 @@ import { resolveMainPathOnce, normalizeMiniProgramFilename, isCSSRequest, + parseManifestJsonOnce, + M, } from '@dcloudio/uni-cli-shared' import { GetManualChunk, GetModuleInfo, Plugin, PreRenderedChunk } from 'rollup' import { @@ -24,12 +26,20 @@ import { const debugChunk = debug('vite:uni:chunk') export function buildOptions(): UserConfig['build'] { + const platform = process.env.UNI_PLATFORM const inputDir = process.env.UNI_INPUT_DIR const outputDir = process.env.UNI_OUTPUT_DIR // 开始编译时,清空输出目录 if (fs.existsSync(outputDir)) { emptyDir(outputDir) } + return createBuildOptions(inputDir, platform) +} + +export function createBuildOptions( + inputDir: string, + platform: UniApp.PLATFORM +): BuildOptions { return { // sourcemap: 'inline', // TODO // target: ['chrome53'], // 由小程序自己启用 es6 编译 @@ -41,8 +51,14 @@ export function buildOptions(): UserConfig['build'] { formats: ['cjs'], }, rollupOptions: { + input: parseRollupInput(inputDir, platform), output: { - entryFileNames: 'app.js', + entryFileNames(chunk) { + if (chunk.name === 'main') { + return 'app.js' + } + return chunk.name + '.js' + }, format: 'cjs', manualChunks: createMoveToVendorChunkFn(), chunkFileNames: createChunkFileNames(inputDir), @@ -53,6 +69,29 @@ export function buildOptions(): UserConfig['build'] { } } +function parseRollupInput(inputDir: string, platform: UniApp.PLATFORM) { + const inputOptions: Record = { + app: resolveMainPathOnce(inputDir), + } + if (process.env.UNI_MP_PLUGIN) { + return inputOptions + } + const manifestJson = parseManifestJsonOnce(inputDir) + const plugins = manifestJson[platform]?.plugins || {} + Object.keys(plugins).forEach((name) => { + const pluginExport = plugins[name].export + if (!pluginExport) { + return + } + const pluginExportFile = path.resolve(inputDir, pluginExport) + if (!fs.existsSync(pluginExportFile)) { + notFound(pluginExportFile) + } + inputOptions[removeExt(pluginExport)] = pluginExportFile + }) + return inputOptions +} + function isVueJs(id: string) { return id.includes('plugin-vue:export-helper') } @@ -163,3 +202,10 @@ function dynamicImportPolyfill(): Plugin { }, } } + +export function notFound(filename: string): never { + console.log() + console.error(M['file.notfound'].replace('{file}', filename)) + console.log() + process.exit(0) +} diff --git a/packages/uni-mp-vite/src/plugin/index.ts b/packages/uni-mp-vite/src/plugin/index.ts index e561c099bf26749b2e8457fef888eef33fdf1391..bdc7237407b79b30c16fccde9f816339e3d412a8 100644 --- a/packages/uni-mp-vite/src/plugin/index.ts +++ b/packages/uni-mp-vite/src/plugin/index.ts @@ -43,8 +43,18 @@ export interface UniMiniProgramPluginOptions { ) => void } app: { - darkmode: boolean - subpackages: boolean + /** + * 是否支持darkmode + */ + darkmode?: boolean + /** + * 是否支持subpackages + */ + subpackages?: boolean + /** + * 是否支持发行插件 + */ + plugins?: boolean } project?: { filename: string diff --git a/packages/uni-mp-vite/src/plugins/pagesJson.ts b/packages/uni-mp-vite/src/plugins/pagesJson.ts index 4d6903a956d4de94904b32c2802a42532d8f19fe..5df8c295c376032ec21c828c644a0a4a8dae8cbe 100644 --- a/packages/uni-mp-vite/src/plugins/pagesJson.ts +++ b/packages/uni-mp-vite/src/plugins/pagesJson.ts @@ -54,7 +54,7 @@ export function uniPagesJsonPlugin( options.app.darkmode && fs.existsSync(path.resolve(inputDir, 'theme.json')), networkTimeout: manifestJson.networkTimeout, - subpackages: options.app.subpackages, + subpackages: !!options.app.subpackages, ...options.json, } ) diff --git a/packages/uni-mp-vite/src/plugins/plugin.ts b/packages/uni-mp-vite/src/plugins/plugin.ts new file mode 100644 index 0000000000000000000000000000000000000000..24c93fa88d968e18b3bc0cea750a9b57120a3678 --- /dev/null +++ b/packages/uni-mp-vite/src/plugins/plugin.ts @@ -0,0 +1,65 @@ +import fs from 'fs' +import path from 'path' +import type { Plugin } from 'vite' +import type { RollupOptions } from 'rollup' +import { + MP_PLUGIN_JSON_NAME, + parseJson, + removeExt, + resolveMainPathOnce, +} from '@dcloudio/uni-cli-shared' +import { UniMiniProgramPluginOptions } from '../plugin' +import { createNonAppGenerateBundle } from './subpackage' +import { extend } from '@vue/shared' +import { notFound } from '../plugin/build' + +export function uniMiniProgramPluginPlugin({ + style: { extname }, +}: UniMiniProgramPluginOptions): Plugin { + const entry = initPluginEntry() + const rollupOptions: RollupOptions = {} + if (entry) { + rollupOptions.input = extend( + { + app: resolveMainPathOnce(process.env.UNI_INPUT_DIR), + }, + entry + ) + } + return { + name: 'vite:uni-mp-plugin', + enforce: 'post', + config() { + return { + build: { + rollupOptions, + }, + } + }, + generateBundle: createNonAppGenerateBundle(extname), + } +} + +function initPluginEntry(): Record | void { + const pluginJsonFilename = path.resolve( + process.env.UNI_INPUT_DIR, + MP_PLUGIN_JSON_NAME + ) + if (!fs.existsSync(pluginJsonFilename)) { + notFound(pluginJsonFilename) + } + const pluginJson = parseJson( + fs.readFileSync(pluginJsonFilename, 'utf8'), + true + ) + if (!pluginJson.main) { + return + } + const mainFilename = path.resolve(process.env.UNI_INPUT_DIR, pluginJson.main) + if (!fs.existsSync(mainFilename)) { + notFound(mainFilename) + } + return { + [removeExt(pluginJson.main)]: mainFilename, + } +} diff --git a/packages/uni-mp-vite/src/plugins/subpackage.ts b/packages/uni-mp-vite/src/plugins/subpackage.ts index fc07168f4177e8bebbd5675d1c3ca2826be20183..39017a94318b929218582f09415106d1d4697268 100644 --- a/packages/uni-mp-vite/src/plugins/subpackage.ts +++ b/packages/uni-mp-vite/src/plugins/subpackage.ts @@ -1,4 +1,4 @@ -import { OutputAsset, OutputChunk } from 'rollup' +import { OutputAsset, OutputChunk, OutputPluginHooks } from 'rollup' import type { Plugin } from 'vite' import { isMiniProgramPageFile, relativeFile } from '@dcloudio/uni-cli-shared' import { UniMiniProgramPluginOptions } from '../plugin' @@ -9,28 +9,34 @@ export function uniSubpackagePlugin({ return { name: 'vite:uni-mp-subpackage', enforce: 'post', - generateBundle(_, bundle) { - ;['project.config.json', 'app.json'].forEach((name) => { - delete bundle[name] - }) - const appJsFile = 'app.js' - const appCssFile = 'app' + extname - Object.keys(bundle).forEach((name) => { - if (!isMiniProgramPageFile(name)) { - return - } - // 仅页面级 wxss 需要补充 app.wxss - if (name.endsWith(extname)) { - const cssFile = bundle[name] as OutputAsset - cssFile.source = - `@import "${relativeFile(name, appCssFile)}";\n` + - cssFile.source.toString() - } else if (name.endsWith('.js')) { - const jsFile = bundle[name] as OutputChunk - jsFile.code = - `require('${relativeFile(name, appJsFile)}');\n` + jsFile.code - } - }) - }, + generateBundle: createNonAppGenerateBundle(extname), + } +} + +export function createNonAppGenerateBundle( + extname: string +): OutputPluginHooks['generateBundle'] { + return function generateBundle(_, bundle) { + ;['project.config.json', 'app.json'].forEach((name) => { + delete bundle[name] + }) + const appJsFile = 'app.js' + const appCssFile = 'app' + extname + Object.keys(bundle).forEach((name) => { + if (!isMiniProgramPageFile(name)) { + return + } + // 仅页面级 wxss 需要补充 app.wxss + if (name.endsWith(extname)) { + const cssFile = bundle[name] as OutputAsset + cssFile.source = + `@import "${relativeFile(name, appCssFile)}";\n` + + cssFile.source.toString() + } else if (name.endsWith('.js')) { + const jsFile = bundle[name] as OutputChunk + jsFile.code = + `require('${relativeFile(name, appJsFile)}');\n` + jsFile.code + } + }) } } diff --git a/packages/uni-mp-vue/dist/vue.runtime.esm.js b/packages/uni-mp-vue/dist/vue.runtime.esm.js index 4f9ca8023f103c489920c86585ce22ccc1b1f132..ca85f1f512e8f491bd11feaac60d11965f179da9 100644 --- a/packages/uni-mp-vue/dist/vue.runtime.esm.js +++ b/packages/uni-mp-vue/dist/vue.runtime.esm.js @@ -4995,9 +4995,11 @@ var plugin = { }, }; function getCreateApp() { - const method = process.env.UNI_SUBPACKAGE - ? 'createSubpackageApp' - : 'createApp'; + const method = process.env.UNI_MP_PLUGIN + ? 'createPluginApp' + : process.env.UNI_SUBPACKAGE + ? 'createSubpackageApp' + : 'createApp'; if (typeof global !== 'undefined') { return global[method]; } diff --git a/packages/uni-mp-vue/src/plugin.ts b/packages/uni-mp-vue/src/plugin.ts index 5b873c5b6ca8dce1a873cc72e004891665aeb060..e90da17c383fb0a4b9dd6ac92ee57e671dc5d495 100644 --- a/packages/uni-mp-vue/src/plugin.ts +++ b/packages/uni-mp-vue/src/plugin.ts @@ -29,7 +29,9 @@ export default { } function getCreateApp() { - const method = process.env.UNI_SUBPACKAGE + const method = process.env.UNI_MP_PLUGIN + ? 'createPluginApp' + : process.env.UNI_SUBPACKAGE ? 'createSubpackageApp' : 'createApp' if (typeof global !== 'undefined') { diff --git a/packages/uni-mp-weixin/dist/uni.compiler.js b/packages/uni-mp-weixin/dist/uni.compiler.js index 13fbfeef5d52f08f1dfb83abf7ef633e40d99bcb..2945406c7b37254824032fbe7dd4c40478fbac9a 100644 --- a/packages/uni-mp-weixin/dist/uni.compiler.js +++ b/packages/uni-mp-weixin/dist/uni.compiler.js @@ -91,6 +91,7 @@ const options = { copyOptions: { assets: ['wxcomponents'], targets: [ + ...(process.env.UNI_MP_PLUGIN ? [uniCliShared.copyMiniProgramPluginJson] : []), { src: [ 'theme.json', @@ -111,6 +112,7 @@ const options = { app: { darkmode: true, subpackages: true, + plugins: true, }, project: { filename: projectConfigFilename, diff --git a/packages/uni-mp-weixin/dist/uni.mp.esm.js b/packages/uni-mp-weixin/dist/uni.mp.esm.js index 80d0e6db3ccd1b5e012f2c78e310924bd8a259d2..5bbcc0d6158fa313cb0786a7f45f595d972226bf 100644 --- a/packages/uni-mp-weixin/dist/uni.mp.esm.js +++ b/packages/uni-mp-weixin/dist/uni.mp.esm.js @@ -284,23 +284,25 @@ function initCreateSubpackageApp(parseAppOptions) { app[name] = appOptions[name]; } }); - if (isFunction(appOptions.onShow) && wx.onAppShow) { - wx.onAppShow((args) => { - vm.$callHook('onShow', args); - }); - } - if (isFunction(appOptions.onHide) && wx.onAppHide) { - wx.onAppHide((args) => { - vm.$callHook('onHide', args); - }); - } - if (isFunction(appOptions.onLaunch)) { - const args = wx.getLaunchOptionsSync && wx.getLaunchOptionsSync(); - vm.$callHook('onLaunch', args); - } - return App(appOptions); + initAppLifecycle(appOptions, vm); }; } +function initAppLifecycle(appOptions, vm) { + if (isFunction(appOptions.onShow) && wx.onAppShow) { + wx.onAppShow((args) => { + vm.$callHook('onShow', args); + }); + } + if (isFunction(appOptions.onHide) && wx.onAppHide) { + wx.onAppHide((args) => { + vm.$callHook('onHide', args); + }); + } + if (isFunction(appOptions.onLaunch)) { + const args = wx.getLaunchOptionsSync && wx.getLaunchOptionsSync(); + vm.$callHook('onLaunch', args || {}); + } +} function initLocale(appVm) { const locale = ref(wx.getSystemInfoSync().language || 'zh-Hans'); Object.defineProperty(appVm, '$locale', { @@ -670,6 +672,12 @@ function initCreatePage(parseOptions) { }; } +function initCreatePluginApp(parseAppOptions) { + return function createApp(vm) { + initAppLifecycle(parseApp(vm, parseAppOptions), vm); + }; +} + const ON_READY = 'onReady'; const MPPage = Page; @@ -787,10 +795,12 @@ var parseOptions = /*#__PURE__*/Object.freeze({ const createApp = initCreateApp(); const createPage = initCreatePage(parseOptions); const createComponent = initCreateComponent(parseOptions); +const createPluginApp = initCreatePluginApp(); const createSubpackageApp = initCreateSubpackageApp(); wx.createApp = global.createApp = createApp; wx.createPage = createPage; wx.createComponent = createComponent; +wx.createPluginApp = createPluginApp; wx.createSubpackageApp = createSubpackageApp; -export { createApp, createComponent, createPage, createSubpackageApp }; +export { createApp, createComponent, createPage, createPluginApp, createSubpackageApp }; diff --git a/packages/uni-mp-weixin/src/compiler/options.ts b/packages/uni-mp-weixin/src/compiler/options.ts index f01c682c51f8c41d302f6d78adcc9bf1b61fed23..af9ff5b6d9183769cc24e995dcd1b87c4a81d719 100644 --- a/packages/uni-mp-weixin/src/compiler/options.ts +++ b/packages/uni-mp-weixin/src/compiler/options.ts @@ -2,6 +2,7 @@ import path from 'path' import type { CompilerOptions } from '@vue/compiler-core' import { COMPONENT_CUSTOM_HIDDEN, + copyMiniProgramPluginJson, MiniProgramCompilerOptions, transformComponentLink, transformRef, @@ -46,6 +47,7 @@ export const options: UniMiniProgramPluginOptions = { copyOptions: { assets: ['wxcomponents'], targets: [ + ...(process.env.UNI_MP_PLUGIN ? [copyMiniProgramPluginJson] : []), { src: [ 'theme.json', @@ -66,6 +68,7 @@ export const options: UniMiniProgramPluginOptions = { app: { darkmode: true, subpackages: true, + plugins: true, }, project: { filename: projectConfigFilename, diff --git a/packages/uni-mp-weixin/src/runtime/index.ts b/packages/uni-mp-weixin/src/runtime/index.ts index f6f006e11788f2b482299e99062287e9b6661d5a..9cdc2d9d3267a8ed7875a13b9f524b5d88aeafd3 100644 --- a/packages/uni-mp-weixin/src/runtime/index.ts +++ b/packages/uni-mp-weixin/src/runtime/index.ts @@ -2,6 +2,7 @@ import { initCreateApp, initCreatePage, initCreateComponent, + initCreatePluginApp, initCreateSubpackageApp, } from '@dcloudio/uni-mp-core' @@ -12,8 +13,10 @@ import * as parseOptions from './parseOptions' export const createApp = initCreateApp() export const createPage = initCreatePage(parseOptions) export const createComponent = initCreateComponent(parseOptions) +export const createPluginApp = initCreatePluginApp() export const createSubpackageApp = initCreateSubpackageApp() ;(wx as any).createApp = (global as any).createApp = createApp ;(wx as any).createPage = createPage ;(wx as any).createComponent = createComponent +;(wx as any).createPluginApp = createPluginApp ;(wx as any).createSubpackageApp = createSubpackageApp diff --git a/packages/uni-quickapp-webview/dist/uni.mp.esm.js b/packages/uni-quickapp-webview/dist/uni.mp.esm.js index fb9080861fc25b4faab0dca6426263915b476cf2..362050a8de6b12fc6741cecfc1fe8b12705c4e35 100644 --- a/packages/uni-quickapp-webview/dist/uni.mp.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.mp.esm.js @@ -412,23 +412,25 @@ function initCreateSubpackageApp(parseAppOptions) { app[name] = appOptions[name]; } }); - if (isFunction(appOptions.onShow) && qa.onAppShow) { - qa.onAppShow((args) => { - vm.$callHook('onShow', args); - }); - } - if (isFunction(appOptions.onHide) && qa.onAppHide) { - qa.onAppHide((args) => { - vm.$callHook('onHide', args); - }); - } - if (isFunction(appOptions.onLaunch)) { - const args = qa.getLaunchOptionsSync && qa.getLaunchOptionsSync(); - vm.$callHook('onLaunch', args); - } - return App(appOptions); + initAppLifecycle(appOptions, vm); }; } +function initAppLifecycle(appOptions, vm) { + if (isFunction(appOptions.onShow) && qa.onAppShow) { + qa.onAppShow((args) => { + vm.$callHook('onShow', args); + }); + } + if (isFunction(appOptions.onHide) && qa.onAppHide) { + qa.onAppHide((args) => { + vm.$callHook('onHide', args); + }); + } + if (isFunction(appOptions.onLaunch)) { + const args = qa.getLaunchOptionsSync && qa.getLaunchOptionsSync(); + vm.$callHook('onLaunch', args || {}); + } +} function initLocale(appVm) { const locale = ref(qa.getSystemInfoSync().language || 'zh-Hans'); Object.defineProperty(appVm, '$locale', { diff --git a/packages/vite-plugin-uni/src/cli/index.ts b/packages/vite-plugin-uni/src/cli/index.ts index 49e515b606a2b70842a6586225e14d5ab95dd4db..73b73c18508e6e2f09523167d85850aa5dd8fec3 100644 --- a/packages/vite-plugin-uni/src/cli/index.ts +++ b/packages/vite-plugin-uni/src/cli/index.ts @@ -26,6 +26,7 @@ export interface CliOptions { autoHost?: string autoPort?: number subpackage?: string + plugin?: boolean } cli @@ -43,6 +44,7 @@ cli .option('--autoHost [autoHost]', `[string] specify automator hostname`) .option('--autoPort [autoPort]', `[number] specify automator port`) .option('--subpackage [subpackage]', `[string] specify subpackage to build`) + .option('--plugin', `[boolean] build plugin`) cli .command('') diff --git a/packages/vite-plugin-uni/src/cli/utils.ts b/packages/vite-plugin-uni/src/cli/utils.ts index 159c7d0c862fb646bb2786dfff531ef19c3fbc17..4e010832b73c6af57e8f09be8e5c598ee563a397 100644 --- a/packages/vite-plugin-uni/src/cli/utils.ts +++ b/packages/vite-plugin-uni/src/cli/utils.ts @@ -47,6 +47,9 @@ export function initEnv(type: 'dev' | 'build', options: CliOptions) { console.error(M['mp.360.unsupported']) process.exit(0) } + if (options.plugin) { + process.env.UNI_MP_PLUGIN = 'true' + } if (type === 'dev') { process.env.NODE_ENV = 'development' } else if (type === 'build') { @@ -137,8 +140,8 @@ export function initEnv(type: 'dev' | 'build', options: CliOptions) { } function initAutomator({ autoHost, autoPort }: CliOptions) { - // 发行分包也不需要自动化测试 - if (!autoPort || process.env.UNI_SUBPACKAGE) { + // 发行分包,插件也不需要自动化测试 + if (!autoPort || process.env.UNI_SUBPACKAGE || process.env.UNI_MP_PLUGIN) { return } process.env.UNI_AUTOMATOR_WS_ENDPOINT =