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

feat(mp): support build plugin

上级 43c94c49
......@@ -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)\"",
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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} 文件不存在',
}
......@@ -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'
......@@ -3,4 +3,5 @@ export * from './event'
export * from './style'
export * from './template'
export * from './constants'
export { copyMiniProgramPluginJson } from './plugin'
export { transformVueComponentImports } from './transformImports'
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)
},
}
......@@ -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,
......
......@@ -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 };
......@@ -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,
......
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
......@@ -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', {
......
......@@ -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'
......
......@@ -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 || {})
}
}
......
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)
}
}
......@@ -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', {
......
......@@ -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', {
......
......@@ -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 };
......@@ -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', {
......
......@@ -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<SFCScriptCompileOptions> }
......@@ -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)] : []),
]
}
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<string, string> = {
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)
}
......@@ -43,8 +43,18 @@ export interface UniMiniProgramPluginOptions {
) => void
}
app: {
darkmode: boolean
subpackages: boolean
/**
* 是否支持darkmode
*/
darkmode?: boolean
/**
* 是否支持subpackages
*/
subpackages?: boolean
/**
* 是否支持发行插件
*/
plugins?: boolean
}
project?: {
filename: string
......
......@@ -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,
}
)
......
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<string, string> | 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,
}
}
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
}
})
}
}
......@@ -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];
}
......
......@@ -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') {
......
......@@ -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,
......
......@@ -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 };
......@@ -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,
......
......@@ -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
......@@ -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', {
......
......@@ -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('')
......
......@@ -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 =
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册