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

feat(mp): support build subpackage

上级 bb489034
...@@ -9,6 +9,7 @@ declare namespace NodeJS { ...@@ -9,6 +9,7 @@ declare namespace NodeJS {
UNI_INPUT_DIR: string UNI_INPUT_DIR: string
UNI_OUTPUT_DIR: string UNI_OUTPUT_DIR: string
UNI_CLI_CONTEXT: string UNI_CLI_CONTEXT: string
UNI_SUBPACKAGE?: string
UNI_COMPILER_VERSION: string UNI_COMPILER_VERSION: string
UNI_COMPILER_VERSION_TYPE: 'a' | 'r' UNI_COMPILER_VERSION_TYPE: 'a' | 'r'
UNI_HBUILDERX_PLUGINS: string UNI_HBUILDERX_PLUGINS: string
......
...@@ -13,6 +13,7 @@ export function initDefine(stringifyBoolean: boolean = false) { ...@@ -13,6 +13,7 @@ export function initDefine(stringifyBoolean: boolean = false) {
'process.env.UNI_APP_ID': JSON.stringify(manifestJson.appid || ''), 'process.env.UNI_APP_ID': JSON.stringify(manifestJson.appid || ''),
'process.env.UNI_APP_NAME': JSON.stringify(manifestJson.name || ''), 'process.env.UNI_APP_NAME': JSON.stringify(manifestJson.name || ''),
'process.env.UNI_PLATFORM': JSON.stringify(process.env.UNI_PLATFORM), 'process.env.UNI_PLATFORM': JSON.stringify(process.env.UNI_PLATFORM),
'process.env.UNI_SUBPACKAGE': JSON.stringify(process.env.UNI_SUBPACKAGE),
'process.env.UNI_COMPILER_VERSION': JSON.stringify( 'process.env.UNI_COMPILER_VERSION': JSON.stringify(
process.env.UNI_COMPILER_VERSION process.env.UNI_COMPILER_VERSION
), ),
......
import { extend } from '@vue/shared' import { extend } from '@vue/shared'
import { ComponentJson, PageWindowOptions, UsingComponents } from './types' import { ComponentJson, PageWindowOptions, UsingComponents } from './types'
import { normalizeNodeModules } from '../../utils' import { removeExt, relativeFile, normalizeNodeModules } from '../../utils'
let appJsonCache: Record<string, any> = {} let appJsonCache: Record<string, any> = {}
const jsonFilesCache = new Map<string, string>() const jsonFilesCache = new Map<string, string>()
...@@ -8,6 +8,10 @@ const jsonPagesCache = new Map<string, PageWindowOptions>() ...@@ -8,6 +8,10 @@ const jsonPagesCache = new Map<string, PageWindowOptions>()
const jsonComponentsCache = new Map<string, ComponentJson>() const jsonComponentsCache = new Map<string, ComponentJson>()
const jsonUsingComponentsCache = new Map<string, UsingComponents>() const jsonUsingComponentsCache = new Map<string, UsingComponents>()
export function isPageFile(file: string) {
return jsonPagesCache.has(removeExt(file))
}
export function hasJsonFile(filename: string) { export function hasJsonFile(filename: string) {
return ( return (
filename === 'app' || filename === 'app' ||
...@@ -22,16 +26,25 @@ export function normalizeJsonFilename(filename: string) { ...@@ -22,16 +26,25 @@ export function normalizeJsonFilename(filename: string) {
export function findChangedJsonFiles() { export function findChangedJsonFiles() {
const changedJsonFiles = new Map<string, string>() const changedJsonFiles = new Map<string, string>()
function findChangedFile(name: string, json: Record<string, any>) { function findChangedFile(filename: string, json: Record<string, any>) {
const newJson = extend({}, json) const newJson = extend({}, json)
if (!newJson.usingComponents) { if (!newJson.usingComponents) {
newJson.usingComponents = {} newJson.usingComponents = {}
} }
extend(newJson.usingComponents, jsonUsingComponentsCache.get(name)) extend(newJson.usingComponents, jsonUsingComponentsCache.get(filename))
const usingComponents = newJson.usingComponents
// 格式化为相对路径,这样作为分包也可以直接运行
Object.keys(usingComponents).forEach((name) => {
usingComponents[name] = relativeFile(
filename,
usingComponents[name].slice(1)
)
})
const jsonStr = JSON.stringify(newJson, null, 2) const jsonStr = JSON.stringify(newJson, null, 2)
if (jsonFilesCache.get(name) !== jsonStr) { if (jsonFilesCache.get(filename) !== jsonStr) {
changedJsonFiles.set(name, jsonStr) changedJsonFiles.set(filename, jsonStr)
jsonFilesCache.set(name, jsonStr) jsonFilesCache.set(filename, jsonStr)
} }
} }
function findChangedFiles(jsonsCache: Map<string, any>) { function findChangedFiles(jsonsCache: Map<string, any>) {
......
...@@ -19,6 +19,9 @@ export function normalizePath(id: string): string { ...@@ -19,6 +19,9 @@ export function normalizePath(id: string): string {
return isWindows ? id.replace(/\\/g, '/') : id return isWindows ? id.replace(/\\/g, '/') : id
} }
export function relativeFile(from: string, to: string) {
return normalizePath(path.relative(path.dirname(from), to))
}
export function checkElementNodeTag( export function checkElementNodeTag(
node: RootNode | TemplateChildNode | null | undefined, node: RootNode | TemplateChildNode | null | undefined,
tag: string tag: string
......
...@@ -334,6 +334,43 @@ function initCreateApp(parseAppOptions) { ...@@ -334,6 +334,43 @@ function initCreateApp(parseAppOptions) {
return App(parseApp(vm, parseAppOptions)); return App(parseApp(vm, parseAppOptions));
}; };
} }
function initCreateSubpackageApp(parseAppOptions) {
return function createApp(vm) {
const appOptions = parseApp(vm, parseAppOptions);
const app = getApp({
allowDefault: true,
});
vm.$.ctx.$scope = app;
const globalData = app.globalData;
if (globalData) {
Object.keys(appOptions.globalData).forEach((name) => {
if (!hasOwn(globalData, name)) {
globalData[name] = appOptions.globalData[name];
}
});
}
Object.keys(appOptions).forEach((name) => {
if (!hasOwn(app, name)) {
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);
};
}
function initLocale(appVm) { function initLocale(appVm) {
const locale = ref(my.getSystemInfoSync().language || 'zh-Hans'); const locale = ref(my.getSystemInfoSync().language || 'zh-Hans');
Object.defineProperty(appVm, '$locale', { Object.defineProperty(appVm, '$locale', {
...@@ -571,20 +608,15 @@ function onAliGetAuthorize(method, $event) { ...@@ -571,20 +608,15 @@ function onAliGetAuthorize(method, $event) {
my.getPhoneNumber({ my.getPhoneNumber({
success: (res) => { success: (res) => {
$event.type = 'getphonenumber'; $event.type = 'getphonenumber';
const response = JSON.parse(res.response).response; const response = JSON.parse(res.response);
if (response.code === '10000') { $event.detail.errMsg = 'getPhoneNumber:ok';
// success $event.detail.encryptedData = response.response;
$event.detail.errMsg = 'getPhoneNumber:ok'; $event.detail.sign = response.sign;
$event.detail.encryptedData = res.response;
}
else {
$event.detail.errMsg = 'getPhoneNumber:fail Error: ' + res.response;
}
this[method]($event); this[method]($event);
}, },
fail: () => { fail: (res) => {
$event.type = 'getphonenumber'; $event.type = 'getphonenumber';
$event.detail.errMsg = 'getPhoneNumber:fail'; $event.detail.errMsg = 'getPhoneNumber:fail Error: ' + JSON.stringify(res);
this[method]($event); this[method]($event);
}, },
}); });
...@@ -983,9 +1015,11 @@ function initCreateComponent() { ...@@ -983,9 +1015,11 @@ function initCreateComponent() {
const createApp = initCreateApp(parseAppOptions); const createApp = initCreateApp(parseAppOptions);
const createPage = initCreatePage(); const createPage = initCreatePage();
const createComponent = initCreateComponent(); const createComponent = initCreateComponent();
const createSubpackageApp = initCreateSubpackageApp(parseAppOptions);
my.EventChannel = EventChannel; my.EventChannel = EventChannel;
my.createApp = createApp; my.createApp = createApp;
my.createPage = createPage; my.createPage = createPage;
my.createComponent = createComponent; my.createComponent = createComponent;
my.createSubpackageApp = createSubpackageApp;
export { createApp, createComponent, createPage }; export { createApp, createComponent, createPage, createSubpackageApp };
import { EventChannel } from '@dcloudio/uni-shared' import { EventChannel } from '@dcloudio/uni-shared'
import { initCreateApp } from '@dcloudio/uni-mp-core' import { initCreateApp, initCreateSubpackageApp } from '@dcloudio/uni-mp-core'
import * as parseAppOptions from './parseAppOptions' import * as parseAppOptions from './parseAppOptions'
...@@ -9,7 +9,9 @@ import { initCreateComponent } from './createComponent' ...@@ -9,7 +9,9 @@ import { initCreateComponent } from './createComponent'
export const createApp = initCreateApp(parseAppOptions) export const createApp = initCreateApp(parseAppOptions)
export const createPage = initCreatePage() export const createPage = initCreatePage()
export const createComponent = initCreateComponent() export const createComponent = initCreateComponent()
export const createSubpackageApp = initCreateSubpackageApp(parseAppOptions)
;(my as any).EventChannel = EventChannel ;(my as any).EventChannel = EventChannel
;(my as any).createApp = createApp ;(my as any).createApp = createApp
;(my as any).createPage = createPage ;(my as any).createPage = createPage
;(my as any).createComponent = createComponent ;(my as any).createComponent = createComponent
;(my as any).createSubpackageApp = createSubpackageApp
...@@ -323,6 +323,43 @@ function initCreateApp(parseAppOptions) { ...@@ -323,6 +323,43 @@ function initCreateApp(parseAppOptions) {
return App(parseApp(vm, parseAppOptions)); return App(parseApp(vm, parseAppOptions));
}; };
} }
function initCreateSubpackageApp(parseAppOptions) {
return function createApp(vm) {
const appOptions = parseApp(vm, parseAppOptions);
const app = getApp({
allowDefault: true,
});
vm.$.ctx.$scope = app;
const globalData = app.globalData;
if (globalData) {
Object.keys(appOptions.globalData).forEach((name) => {
if (!hasOwn(globalData, name)) {
globalData[name] = appOptions.globalData[name];
}
});
}
Object.keys(appOptions).forEach((name) => {
if (!hasOwn(app, name)) {
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);
};
}
function initLocale(appVm) { function initLocale(appVm) {
const locale = ref(swan.getSystemInfoSync().language || 'zh-Hans'); const locale = ref(swan.getSystemInfoSync().language || 'zh-Hans');
Object.defineProperty(appVm, '$locale', { Object.defineProperty(appVm, '$locale', {
...@@ -979,9 +1016,11 @@ var parsePageOptions = /*#__PURE__*/Object.freeze({ ...@@ -979,9 +1016,11 @@ var parsePageOptions = /*#__PURE__*/Object.freeze({
const createApp = initCreateApp(parseAppOptions); const createApp = initCreateApp(parseAppOptions);
const createPage = initCreatePage(parsePageOptions); const createPage = initCreatePage(parsePageOptions);
const createComponent = initCreateComponent(parseComponentOptions); const createComponent = initCreateComponent(parseComponentOptions);
const createSubpackageApp = initCreateSubpackageApp(parseAppOptions);
swan.EventChannel = EventChannel; swan.EventChannel = EventChannel;
swan.createApp = global.createApp = createApp; swan.createApp = global.createApp = createApp;
swan.createPage = createPage; swan.createPage = createPage;
swan.createComponent = createComponent; swan.createComponent = createComponent;
swan.createSubpackageApp = createSubpackageApp;
export { createApp, createComponent, createPage }; export { createApp, createComponent, createPage, createSubpackageApp };
...@@ -3,6 +3,7 @@ import { ...@@ -3,6 +3,7 @@ import {
initCreateApp, initCreateApp,
initCreatePage, initCreatePage,
initCreateComponent, initCreateComponent,
initCreateSubpackageApp,
} from '@dcloudio/uni-mp-core' } from '@dcloudio/uni-mp-core'
import '@dcloudio/uni-mp-polyfill' import '@dcloudio/uni-mp-polyfill'
...@@ -14,7 +15,9 @@ import * as parseComponentOptions from './parseComponentOptions' ...@@ -14,7 +15,9 @@ import * as parseComponentOptions from './parseComponentOptions'
export const createApp = initCreateApp(parseAppOptions) export const createApp = initCreateApp(parseAppOptions)
export const createPage = initCreatePage(parsePageOptions) export const createPage = initCreatePage(parsePageOptions)
export const createComponent = initCreateComponent(parseComponentOptions) export const createComponent = initCreateComponent(parseComponentOptions)
export const createSubpackageApp = initCreateSubpackageApp(parseAppOptions)
;(swan as any).EventChannel = EventChannel ;(swan as any).EventChannel = EventChannel
;(swan as any).createApp = (global as any).createApp = createApp ;(swan as any).createApp = (global as any).createApp = createApp
;(swan as any).createPage = createPage ;(swan as any).createPage = createPage
;(swan as any).createComponent = createComponent ;(swan as any).createComponent = createComponent
;(swan as any).createSubpackageApp = createSubpackageApp
...@@ -7,7 +7,7 @@ declare module '@vue/runtime-core' { ...@@ -7,7 +7,7 @@ declare module '@vue/runtime-core' {
} }
} }
export { initCreateApp } from './runtime/app' export { initCreateApp, initCreateSubpackageApp } from './runtime/app'
export { initCreatePage } from './runtime/page' export { initCreatePage } from './runtime/page'
export { initCreateComponent } from './runtime/component' export { initCreateComponent } from './runtime/component'
......
import { extend } from '@vue/shared' import { extend, hasOwn, isFunction } from '@vue/shared'
import { ComponentPublicInstance, ComponentOptions, ref } from 'vue' import { ComponentPublicInstance, ComponentOptions, ref } from 'vue'
import { initBaseInstance } from './componentInstance' import { initBaseInstance } from './componentInstance'
...@@ -95,6 +95,45 @@ export function initCreateApp(parseAppOptions?: ParseAppOptions) { ...@@ -95,6 +95,45 @@ export function initCreateApp(parseAppOptions?: ParseAppOptions) {
} }
} }
export function initCreateSubpackageApp(parseAppOptions?: ParseAppOptions) {
return function createApp(vm: ComponentPublicInstance) {
const appOptions = parseApp(vm, parseAppOptions)
const app = getApp({
allowDefault: true,
})
;(vm.$ as any).ctx.$scope = app
const globalData = app.globalData
if (globalData) {
Object.keys(appOptions.globalData).forEach((name) => {
if (!hasOwn(globalData, name)) {
globalData[name] = appOptions.globalData[name]
}
})
}
Object.keys(appOptions).forEach((name) => {
if (!hasOwn(app, name)) {
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)
}
}
function initLocale(appVm: ComponentPublicInstance) { function initLocale(appVm: ComponentPublicInstance) {
const locale = ref<string>( const locale = ref<string>(
__GLOBAL__.getSystemInfoSync().language || 'zh-Hans' __GLOBAL__.getSystemInfoSync().language || 'zh-Hans'
......
...@@ -323,6 +323,43 @@ function initCreateApp(parseAppOptions) { ...@@ -323,6 +323,43 @@ function initCreateApp(parseAppOptions) {
return App(parseApp(vm, parseAppOptions)); return App(parseApp(vm, parseAppOptions));
}; };
} }
function initCreateSubpackageApp(parseAppOptions) {
return function createApp(vm) {
const appOptions = parseApp(vm, parseAppOptions);
const app = getApp({
allowDefault: true,
});
vm.$.ctx.$scope = app;
const globalData = app.globalData;
if (globalData) {
Object.keys(appOptions.globalData).forEach((name) => {
if (!hasOwn(globalData, name)) {
globalData[name] = appOptions.globalData[name];
}
});
}
Object.keys(appOptions).forEach((name) => {
if (!hasOwn(app, name)) {
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);
};
}
function initLocale(appVm) { function initLocale(appVm) {
const locale = ref(ks.getSystemInfoSync().language || 'zh-Hans'); const locale = ref(ks.getSystemInfoSync().language || 'zh-Hans');
Object.defineProperty(appVm, '$locale', { Object.defineProperty(appVm, '$locale', {
...@@ -886,9 +923,11 @@ var parseComponentOptions = extend({}, baseParseOptions, { ...@@ -886,9 +923,11 @@ var parseComponentOptions = extend({}, baseParseOptions, {
const createApp = initCreateApp(); const createApp = initCreateApp();
const createPage = initCreatePage(parsePageOptions); const createPage = initCreatePage(parsePageOptions);
const createComponent = initCreateComponent(parseComponentOptions); const createComponent = initCreateComponent(parseComponentOptions);
const createSubpackageApp = initCreateSubpackageApp();
ks.EventChannel = EventChannel; ks.EventChannel = EventChannel;
ks.createApp = global.createApp = createApp; ks.createApp = global.createApp = createApp;
ks.createPage = createPage; ks.createPage = createPage;
ks.createComponent = createComponent; ks.createComponent = createComponent;
ks.createSubpackageApp = createSubpackageApp;
export { createApp, createComponent, createPage }; export { createApp, createComponent, createPage, createSubpackageApp };
...@@ -3,6 +3,7 @@ import { ...@@ -3,6 +3,7 @@ import {
initCreateApp, initCreateApp,
initCreatePage, initCreatePage,
initCreateComponent, initCreateComponent,
initCreateSubpackageApp,
} from '@dcloudio/uni-mp-core' } from '@dcloudio/uni-mp-core'
import '@dcloudio/uni-mp-polyfill' import '@dcloudio/uni-mp-polyfill'
...@@ -12,7 +13,9 @@ import parseComponentOptions from './parseComponentOptions' ...@@ -12,7 +13,9 @@ import parseComponentOptions from './parseComponentOptions'
export const createApp = initCreateApp() export const createApp = initCreateApp()
export const createPage = initCreatePage(parsePageOptions) export const createPage = initCreatePage(parsePageOptions)
export const createComponent = initCreateComponent(parseComponentOptions) export const createComponent = initCreateComponent(parseComponentOptions)
export const createSubpackageApp = initCreateSubpackageApp()
;(ks as any).EventChannel = EventChannel ;(ks as any).EventChannel = EventChannel
;(ks as any).createApp = (global as any).createApp = createApp ;(ks as any).createApp = (global as any).createApp = createApp
;(ks as any).createPage = createPage ;(ks as any).createPage = createPage
;(ks as any).createComponent = createComponent ;(ks as any).createComponent = createComponent
;(ks as any).createSubpackageApp = createSubpackageApp
...@@ -322,6 +322,43 @@ function initCreateApp(parseAppOptions) { ...@@ -322,6 +322,43 @@ function initCreateApp(parseAppOptions) {
return App(parseApp(vm, parseAppOptions)); return App(parseApp(vm, parseAppOptions));
}; };
} }
function initCreateSubpackageApp(parseAppOptions) {
return function createApp(vm) {
const appOptions = parseApp(vm, parseAppOptions);
const app = getApp({
allowDefault: true,
});
vm.$.ctx.$scope = app;
const globalData = app.globalData;
if (globalData) {
Object.keys(appOptions.globalData).forEach((name) => {
if (!hasOwn(globalData, name)) {
globalData[name] = appOptions.globalData[name];
}
});
}
Object.keys(appOptions).forEach((name) => {
if (!hasOwn(app, name)) {
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);
};
}
function initLocale(appVm) { function initLocale(appVm) {
const locale = ref(tt.getSystemInfoSync().language || 'zh-Hans'); const locale = ref(tt.getSystemInfoSync().language || 'zh-Hans');
Object.defineProperty(appVm, '$locale', { Object.defineProperty(appVm, '$locale', {
...@@ -961,9 +998,11 @@ var parsePageOptions = /*#__PURE__*/Object.freeze({ ...@@ -961,9 +998,11 @@ var parsePageOptions = /*#__PURE__*/Object.freeze({
const createApp = initCreateApp(); const createApp = initCreateApp();
const createPage = initCreatePage(parsePageOptions); const createPage = initCreatePage(parsePageOptions);
const createComponent = initCreateComponent(parseComponentOptions); const createComponent = initCreateComponent(parseComponentOptions);
const createSubpackageApp = initCreateSubpackageApp();
tt.EventChannel = EventChannel; tt.EventChannel = EventChannel;
tt.createApp = global.createApp = createApp; tt.createApp = global.createApp = createApp;
tt.createPage = createPage; tt.createPage = createPage;
tt.createComponent = createComponent; tt.createComponent = createComponent;
tt.createSubpackageApp = createSubpackageApp;
export { createApp, createComponent, createPage }; export { createApp, createComponent, createPage, createSubpackageApp };
...@@ -3,6 +3,7 @@ import { ...@@ -3,6 +3,7 @@ import {
initCreateApp, initCreateApp,
initCreatePage, initCreatePage,
initCreateComponent, initCreateComponent,
initCreateSubpackageApp,
} from '@dcloudio/uni-mp-core' } from '@dcloudio/uni-mp-core'
import '@dcloudio/uni-mp-polyfill' import '@dcloudio/uni-mp-polyfill'
...@@ -13,7 +14,9 @@ import * as parseComponentOptions from '@dcloudio/uni-mp-toutiao/src/runtime/par ...@@ -13,7 +14,9 @@ import * as parseComponentOptions from '@dcloudio/uni-mp-toutiao/src/runtime/par
export const createApp = initCreateApp() export const createApp = initCreateApp()
export const createPage = initCreatePage(parsePageOptions) export const createPage = initCreatePage(parsePageOptions)
export const createComponent = initCreateComponent(parseComponentOptions) export const createComponent = initCreateComponent(parseComponentOptions)
export const createSubpackageApp = initCreateSubpackageApp()
;(tt as any).EventChannel = EventChannel ;(tt as any).EventChannel = EventChannel
;(tt as any).createApp = (global as any).createApp = createApp ;(tt as any).createApp = (global as any).createApp = createApp
;(tt as any).createPage = createPage ;(tt as any).createPage = createPage
;(tt as any).createComponent = createComponent ;(tt as any).createComponent = createComponent
;(tt as any).createSubpackageApp = createSubpackageApp
...@@ -319,6 +319,43 @@ function initCreateApp(parseAppOptions) { ...@@ -319,6 +319,43 @@ function initCreateApp(parseAppOptions) {
return App(parseApp(vm, parseAppOptions)); return App(parseApp(vm, parseAppOptions));
}; };
} }
function initCreateSubpackageApp(parseAppOptions) {
return function createApp(vm) {
const appOptions = parseApp(vm, parseAppOptions);
const app = getApp({
allowDefault: true,
});
vm.$.ctx.$scope = app;
const globalData = app.globalData;
if (globalData) {
Object.keys(appOptions.globalData).forEach((name) => {
if (!hasOwn(globalData, name)) {
globalData[name] = appOptions.globalData[name];
}
});
}
Object.keys(appOptions).forEach((name) => {
if (!hasOwn(app, name)) {
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);
};
}
function initLocale(appVm) { function initLocale(appVm) {
const locale = ref(qq.getSystemInfoSync().language || 'zh-Hans'); const locale = ref(qq.getSystemInfoSync().language || 'zh-Hans');
Object.defineProperty(appVm, '$locale', { Object.defineProperty(appVm, '$locale', {
...@@ -781,13 +818,16 @@ var parseOptions = /*#__PURE__*/Object.freeze({ ...@@ -781,13 +818,16 @@ var parseOptions = /*#__PURE__*/Object.freeze({
const createApp = initCreateApp(); const createApp = initCreateApp();
const createPage = initCreatePage(parseOptions); const createPage = initCreatePage(parseOptions);
const createComponent = initCreateComponent(parseOptions); const createComponent = initCreateComponent(parseOptions);
const createSubpackageApp = initCreateSubpackageApp();
wx.createApp = global.createApp = createApp; wx.createApp = global.createApp = createApp;
wx.createPage = createPage; wx.createPage = createPage;
wx.createComponent = createComponent; wx.createComponent = createComponent;
wx.createSubpackageApp = createSubpackageApp;
qq.EventChannel = EventChannel; qq.EventChannel = EventChannel;
qq.createApp = global.createApp = createApp; qq.createApp = global.createApp = createApp;
qq.createPage = createPage; qq.createPage = createPage;
qq.createComponent = createComponent; qq.createComponent = createComponent;
qq.createSubpackageApp = createSubpackageApp;
export { createApp, createComponent, createPage }; export { createApp, createComponent, createPage, createSubpackageApp };
...@@ -3,6 +3,7 @@ import { ...@@ -3,6 +3,7 @@ import {
createApp, createApp,
createPage, createPage,
createComponent, createComponent,
createSubpackageApp,
} from '@dcloudio/uni-mp-weixin/src/runtime' } from '@dcloudio/uni-mp-weixin/src/runtime'
export * from '@dcloudio/uni-mp-weixin/src/runtime' export * from '@dcloudio/uni-mp-weixin/src/runtime'
...@@ -10,3 +11,4 @@ export * from '@dcloudio/uni-mp-weixin/src/runtime' ...@@ -10,3 +11,4 @@ export * from '@dcloudio/uni-mp-weixin/src/runtime'
;(qq as any).createApp = (global as any).createApp = createApp ;(qq as any).createApp = (global as any).createApp = createApp
;(qq as any).createPage = createPage ;(qq as any).createPage = createPage
;(qq as any).createComponent = createComponent ;(qq as any).createComponent = createComponent
;(qq as any).createSubpackageApp = createSubpackageApp
...@@ -322,6 +322,43 @@ function initCreateApp(parseAppOptions) { ...@@ -322,6 +322,43 @@ function initCreateApp(parseAppOptions) {
return App(parseApp(vm, parseAppOptions)); return App(parseApp(vm, parseAppOptions));
}; };
} }
function initCreateSubpackageApp(parseAppOptions) {
return function createApp(vm) {
const appOptions = parseApp(vm, parseAppOptions);
const app = getApp({
allowDefault: true,
});
vm.$.ctx.$scope = app;
const globalData = app.globalData;
if (globalData) {
Object.keys(appOptions.globalData).forEach((name) => {
if (!hasOwn(globalData, name)) {
globalData[name] = appOptions.globalData[name];
}
});
}
Object.keys(appOptions).forEach((name) => {
if (!hasOwn(app, name)) {
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);
};
}
function initLocale(appVm) { function initLocale(appVm) {
const locale = ref(tt.getSystemInfoSync().language || 'zh-Hans'); const locale = ref(tt.getSystemInfoSync().language || 'zh-Hans');
Object.defineProperty(appVm, '$locale', { Object.defineProperty(appVm, '$locale', {
...@@ -977,9 +1014,11 @@ var parsePageOptions = /*#__PURE__*/Object.freeze({ ...@@ -977,9 +1014,11 @@ var parsePageOptions = /*#__PURE__*/Object.freeze({
const createApp = initCreateApp(); const createApp = initCreateApp();
const createPage = initCreatePage(parsePageOptions); const createPage = initCreatePage(parsePageOptions);
const createComponent = initCreateComponent(parseComponentOptions); const createComponent = initCreateComponent(parseComponentOptions);
const createSubpackageApp = initCreateSubpackageApp();
tt.EventChannel = EventChannel; tt.EventChannel = EventChannel;
tt.createApp = global.createApp = createApp; tt.createApp = global.createApp = createApp;
tt.createPage = createPage; tt.createPage = createPage;
tt.createComponent = createComponent; tt.createComponent = createComponent;
tt.createSubpackageApp = createSubpackageApp;
export { createApp, createComponent, createPage }; export { createApp, createComponent, createPage, createSubpackageApp };
...@@ -3,6 +3,7 @@ import { ...@@ -3,6 +3,7 @@ import {
initCreateApp, initCreateApp,
initCreatePage, initCreatePage,
initCreateComponent, initCreateComponent,
initCreateSubpackageApp,
} from '@dcloudio/uni-mp-core' } from '@dcloudio/uni-mp-core'
import '@dcloudio/uni-mp-polyfill' import '@dcloudio/uni-mp-polyfill'
...@@ -13,7 +14,9 @@ import * as parseComponentOptions from './parseComponentOptions' ...@@ -13,7 +14,9 @@ import * as parseComponentOptions from './parseComponentOptions'
export const createApp = initCreateApp() export const createApp = initCreateApp()
export const createPage = initCreatePage(parsePageOptions) export const createPage = initCreatePage(parsePageOptions)
export const createComponent = initCreateComponent(parseComponentOptions) export const createComponent = initCreateComponent(parseComponentOptions)
export const createSubpackageApp = initCreateSubpackageApp()
;(tt as any).EventChannel = EventChannel ;(tt as any).EventChannel = EventChannel
;(tt as any).createApp = (global as any).createApp = createApp ;(tt as any).createApp = (global as any).createApp = createApp
;(tt as any).createPage = createPage ;(tt as any).createPage = createPage
;(tt as any).createComponent = createComponent ;(tt as any).createComponent = createComponent
;(tt as any).createSubpackageApp = createSubpackageApp
import { extend } from '@vue/shared' import { extend } from '@vue/shared'
import type { SFCScriptCompileOptions } from '@vue/compiler-sfc'
import { initProvide, uniViteInjectPlugin } from '@dcloudio/uni-cli-shared' import { initProvide, uniViteInjectPlugin } from '@dcloudio/uni-cli-shared'
import { uniMiniProgramPlugin, UniMiniProgramPluginOptions } from './plugin' import { uniMiniProgramPlugin, UniMiniProgramPluginOptions } from './plugin'
import { uniUsingComponentsPlugin } from './plugins/usingComponents' import { uniUsingComponentsPlugin } from './plugins/usingComponents'
import { uniMainJsPlugin } from './plugins/mainJs' import { uniMainJsPlugin } from './plugins/mainJs'
import { uniManifestJsonPlugin } from './plugins/manifestJson' import { uniManifestJsonPlugin } from './plugins/manifestJson'
import { uniPagesJsonPlugin } from './plugins/pagesJson' import { uniPagesJsonPlugin } from './plugins/pagesJson'
import { uniEntryPlugin } from './plugins/entry' import { uniEntryPlugin } from './plugins/entry'
import type { SFCScriptCompileOptions } from '@vue/compiler-sfc'
import { uniRenderjsPlugin } from './plugins/renderjs' import { uniRenderjsPlugin } from './plugins/renderjs'
import { uniSubpackagePlugin } from './plugins/subpackage'
export { UniMiniProgramPluginOptions } from './plugin' export { UniMiniProgramPluginOptions } from './plugin'
export default (options: UniMiniProgramPluginOptions) => { export default (options: UniMiniProgramPluginOptions) => {
if (!options.app.subpackages) {
delete process.env.UNI_SUBPACKAGE
}
return [ return [
(options: { (options: {
vueOptions?: { script?: Partial<SFCScriptCompileOptions> } vueOptions?: { script?: Partial<SFCScriptCompileOptions> }
...@@ -28,5 +35,6 @@ export default (options: UniMiniProgramPluginOptions) => { ...@@ -28,5 +35,6 @@ export default (options: UniMiniProgramPluginOptions) => {
}) => { }) => {
return uniUsingComponentsPlugin(options.vueOptions?.script) return uniUsingComponentsPlugin(options.vueOptions?.script)
}, },
...(process.env.UNI_SUBPACKAGE ? [uniSubpackagePlugin(options)] : []),
] ]
} }
...@@ -10,6 +10,7 @@ import { ...@@ -10,6 +10,7 @@ import {
removeExt, removeExt,
transformScopedCss, transformScopedCss,
normalizeMiniProgramFilename, normalizeMiniProgramFilename,
relativeFile,
} from '@dcloudio/uni-cli-shared' } from '@dcloudio/uni-cli-shared'
import { UniMiniProgramPluginOptions } from '.' import { UniMiniProgramPluginOptions } from '.'
import { getNVueCssPaths } from '../plugins/pagesJson' import { getNVueCssPaths } from '../plugins/pagesJson'
...@@ -67,7 +68,10 @@ export function createConfigResolved({ ...@@ -67,7 +68,10 @@ export function createConfigResolved({
const normalized = normalizePath(filename) const normalized = normalizePath(filename)
if (nvueCssPaths.find((pageCssPath) => pageCssPath === normalized)) { if (nvueCssPaths.find((pageCssPath) => pageCssPath === normalized)) {
debugNVueCss(normalized) debugNVueCss(normalized)
return `@import "/nvue${extname}";\n` + cssCode return (
`@import "${relativeFile(normalized, 'nvue' + extname)}";\n` +
cssCode
)
} }
return cssCode return cssCode
}, },
......
import { OutputAsset, OutputChunk } from 'rollup'
import type { Plugin } from 'vite'
import { isPageFile, relativeFile } from '@dcloudio/uni-cli-shared'
import { UniMiniProgramPluginOptions } from '../plugin'
export function uniSubpackagePlugin({
style: { extname },
}: UniMiniProgramPluginOptions): Plugin {
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 (!isPageFile(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
}
})
},
}
}
...@@ -4898,12 +4898,15 @@ var plugin = { ...@@ -4898,12 +4898,15 @@ var plugin = {
}, },
}; };
function getCreateApp() { function getCreateApp() {
const method = process.env.UNI_SUBPACKAGE
? 'createSubpackageApp'
: 'createApp';
if (typeof global !== 'undefined') { if (typeof global !== 'undefined') {
return global.createApp; return global[method];
} }
else if (typeof my !== 'undefined') { else if (typeof my !== 'undefined') {
// 支付宝小程序没有global // 支付宝小程序没有global
return my.createApp; return my[method];
} }
} }
......
...@@ -28,10 +28,13 @@ export default { ...@@ -28,10 +28,13 @@ export default {
} }
function getCreateApp() { function getCreateApp() {
const method = process.env.UNI_SUBPACKAGE
? 'createSubpackageApp'
: 'createApp'
if (typeof global !== 'undefined') { if (typeof global !== 'undefined') {
return (global as any).createApp return (global as any)[method]
} else if (typeof my !== 'undefined') { } else if (typeof my !== 'undefined') {
// 支付宝小程序没有global // 支付宝小程序没有global
return (my as any).createApp return (my as any)[method]
} }
} }
...@@ -256,6 +256,43 @@ function initCreateApp(parseAppOptions) { ...@@ -256,6 +256,43 @@ function initCreateApp(parseAppOptions) {
return App(parseApp(vm, parseAppOptions)); return App(parseApp(vm, parseAppOptions));
}; };
} }
function initCreateSubpackageApp(parseAppOptions) {
return function createApp(vm) {
const appOptions = parseApp(vm, parseAppOptions);
const app = getApp({
allowDefault: true,
});
vm.$.ctx.$scope = app;
const globalData = app.globalData;
if (globalData) {
Object.keys(appOptions.globalData).forEach((name) => {
if (!hasOwn(globalData, name)) {
globalData[name] = appOptions.globalData[name];
}
});
}
Object.keys(appOptions).forEach((name) => {
if (!hasOwn(app, name)) {
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);
};
}
function initLocale(appVm) { function initLocale(appVm) {
const locale = ref(wx.getSystemInfoSync().language || 'zh-Hans'); const locale = ref(wx.getSystemInfoSync().language || 'zh-Hans');
Object.defineProperty(appVm, '$locale', { Object.defineProperty(appVm, '$locale', {
...@@ -721,8 +758,10 @@ var parseOptions = /*#__PURE__*/Object.freeze({ ...@@ -721,8 +758,10 @@ var parseOptions = /*#__PURE__*/Object.freeze({
const createApp = initCreateApp(); const createApp = initCreateApp();
const createPage = initCreatePage(parseOptions); const createPage = initCreatePage(parseOptions);
const createComponent = initCreateComponent(parseOptions); const createComponent = initCreateComponent(parseOptions);
const createSubpackageApp = initCreateSubpackageApp();
wx.createApp = global.createApp = createApp; wx.createApp = global.createApp = createApp;
wx.createPage = createPage; wx.createPage = createPage;
wx.createComponent = createComponent; wx.createComponent = createComponent;
wx.createSubpackageApp = createSubpackageApp;
export { createApp, createComponent, createPage }; export { createApp, createComponent, createPage, createSubpackageApp };
...@@ -2,6 +2,7 @@ import { ...@@ -2,6 +2,7 @@ import {
initCreateApp, initCreateApp,
initCreatePage, initCreatePage,
initCreateComponent, initCreateComponent,
initCreateSubpackageApp,
} from '@dcloudio/uni-mp-core' } from '@dcloudio/uni-mp-core'
import '@dcloudio/uni-mp-polyfill' import '@dcloudio/uni-mp-polyfill'
...@@ -11,6 +12,8 @@ import * as parseOptions from './parseOptions' ...@@ -11,6 +12,8 @@ import * as parseOptions from './parseOptions'
export const createApp = initCreateApp() export const createApp = initCreateApp()
export const createPage = initCreatePage(parseOptions) export const createPage = initCreatePage(parseOptions)
export const createComponent = initCreateComponent(parseOptions) export const createComponent = initCreateComponent(parseOptions)
export const createSubpackageApp = initCreateSubpackageApp()
;(wx as any).createApp = (global as any).createApp = createApp ;(wx as any).createApp = (global as any).createApp = createApp
;(wx as any).createPage = createPage ;(wx as any).createPage = createPage
;(wx as any).createComponent = createComponent ;(wx as any).createComponent = createComponent
;(wx as any).createSubpackageApp = createSubpackageApp
...@@ -319,6 +319,43 @@ function initCreateApp(parseAppOptions) { ...@@ -319,6 +319,43 @@ function initCreateApp(parseAppOptions) {
return App(parseApp(vm, parseAppOptions)); return App(parseApp(vm, parseAppOptions));
}; };
} }
function initCreateSubpackageApp(parseAppOptions) {
return function createApp(vm) {
const appOptions = parseApp(vm, parseAppOptions);
const app = getApp({
allowDefault: true,
});
vm.$.ctx.$scope = app;
const globalData = app.globalData;
if (globalData) {
Object.keys(appOptions.globalData).forEach((name) => {
if (!hasOwn(globalData, name)) {
globalData[name] = appOptions.globalData[name];
}
});
}
Object.keys(appOptions).forEach((name) => {
if (!hasOwn(app, name)) {
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);
};
}
function initLocale(appVm) { function initLocale(appVm) {
const locale = ref(qa.getSystemInfoSync().language || 'zh-Hans'); const locale = ref(qa.getSystemInfoSync().language || 'zh-Hans');
Object.defineProperty(appVm, '$locale', { Object.defineProperty(appVm, '$locale', {
...@@ -941,9 +978,11 @@ var parsePageOptions = /*#__PURE__*/Object.freeze({ ...@@ -941,9 +978,11 @@ var parsePageOptions = /*#__PURE__*/Object.freeze({
const createApp = initCreateApp(); const createApp = initCreateApp();
const createPage = initCreatePage(parsePageOptions); const createPage = initCreatePage(parsePageOptions);
const createComponent = initCreateComponent(parseComponentOptions); const createComponent = initCreateComponent(parseComponentOptions);
const createSubpackageApp = initCreateSubpackageApp();
qa.EventChannel = EventChannel; qa.EventChannel = EventChannel;
qa.createApp = global.createApp = createApp; qa.createApp = global.createApp = createApp;
qa.createPage = createPage; qa.createPage = createPage;
qa.createComponent = createComponent; qa.createComponent = createComponent;
qa.createSubpackageApp = createSubpackageApp;
export { createApp, createComponent, createPage }; export { createApp, createComponent, createPage, createSubpackageApp };
...@@ -3,6 +3,7 @@ import { ...@@ -3,6 +3,7 @@ import {
initCreateApp, initCreateApp,
initCreatePage, initCreatePage,
initCreateComponent, initCreateComponent,
initCreateSubpackageApp,
} from '@dcloudio/uni-mp-core' } from '@dcloudio/uni-mp-core'
import '@dcloudio/uni-mp-polyfill' import '@dcloudio/uni-mp-polyfill'
...@@ -13,7 +14,9 @@ import * as parseComponentOptions from './parseComponentOptions' ...@@ -13,7 +14,9 @@ import * as parseComponentOptions from './parseComponentOptions'
export const createApp = initCreateApp() export const createApp = initCreateApp()
export const createPage = initCreatePage(parsePageOptions) export const createPage = initCreatePage(parsePageOptions)
export const createComponent = initCreateComponent(parseComponentOptions) export const createComponent = initCreateComponent(parseComponentOptions)
export const createSubpackageApp = initCreateSubpackageApp()
;(qa as any).EventChannel = EventChannel ;(qa as any).EventChannel = EventChannel
;(qa as any).createApp = (global as any).createApp = createApp ;(qa as any).createApp = (global as any).createApp = createApp
;(qa as any).createPage = createPage ;(qa as any).createPage = createPage
;(qa as any).createComponent = createComponent ;(qa as any).createComponent = createComponent
;(qa as any).createSubpackageApp = createSubpackageApp
...@@ -23,6 +23,7 @@ export interface CliOptions { ...@@ -23,6 +23,7 @@ export interface CliOptions {
clearScreen?: boolean clearScreen?: boolean
autoHost?: string autoHost?: string
autoPort?: number autoPort?: number
subpackage?: string
} }
cli cli
...@@ -36,8 +37,9 @@ cli ...@@ -36,8 +37,9 @@ cli
.option('--clearScreen', `[boolean] allow/disable clear screen when logging`) .option('--clearScreen', `[boolean] allow/disable clear screen when logging`)
.option('-d, --debug [feat]', `[string | boolean] show debug logs`) .option('-d, --debug [feat]', `[string | boolean] show debug logs`)
.option('-f, --filter <filter>', `[string] filter debug logs`) .option('-f, --filter <filter>', `[string] filter debug logs`)
.option('--autoHost', `[string] specify automator hostname`) .option('--autoHost [autoHost]', `[string] specify automator hostname`)
.option('--autoPort', `[number] specify automator port`) .option('--autoPort [autoPort]', `[number] specify automator port`)
.option('--subpackage [subpackage]', `[string] specify subpackage to build`)
cli cli
.command('') .command('')
......
...@@ -89,6 +89,12 @@ export function initEnv(type: 'dev' | 'build', options: CliOptions) { ...@@ -89,6 +89,12 @@ export function initEnv(type: 'dev' | 'build', options: CliOptions) {
process.env.UNI_OUTPUT_DIR = (options as BuildOptions).outDir! process.env.UNI_OUTPUT_DIR = (options as BuildOptions).outDir!
} }
if (options.subpackage) {
process.env.UNI_SUBPACKAGE = options.subpackage
process.env.UNI_OUTPUT_DIR = (options as BuildOptions).outDir =
path.resolve(process.env.UNI_OUTPUT_DIR, options.subpackage)
}
initAutomator(options) initAutomator(options)
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
...@@ -122,7 +128,8 @@ export function initEnv(type: 'dev' | 'build', options: CliOptions) { ...@@ -122,7 +128,8 @@ export function initEnv(type: 'dev' | 'build', options: CliOptions) {
} }
function initAutomator({ autoHost, autoPort }: CliOptions) { function initAutomator({ autoHost, autoPort }: CliOptions) {
if (!autoPort) { // 发行分包也不需要自动化测试
if (!autoPort || process.env.UNI_SUBPACKAGE) {
return return
} }
process.env.UNI_AUTOMATOR_WS_ENDPOINT = process.env.UNI_AUTOMATOR_WS_ENDPOINT =
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册