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

feat(cli): add --subpackage

上级 e932de17
......@@ -625,16 +625,19 @@ function initHook (name, options) {
};
}
}
if (!MPPage.__$wrappered) {
MPPage.__$wrappered = true;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Page.after = MPPage.after;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
}
class EventChannel {
constructor (id, events) {
......@@ -1213,7 +1216,7 @@ function handleEvent (event) {
}
handler.once = true;
}
const params = processEventArgs(
let params = processEventArgs(
this.$vm,
event,
eventArray[1],
......@@ -1221,9 +1224,13 @@ function handleEvent (event) {
isCustom,
methodName
);
params = Array.isArray(params) ? params : [];
// 参数尾部增加原始事件对象用于复杂表达式内获取额外数据
// eslint-disable-next-line no-sparse-arrays
ret.push(handler.apply(handlerCtx, (Array.isArray(params) ? params : []).concat([, , , , , , , , , , event])));
if (/=\s*\S+\.eventParams\s*\|\|\s*\S+\[['"]event-params['"]\]/.test(handler.toString())) {
// eslint-disable-next-line no-sparse-arrays
params = params.concat([, , , , , , , , , , event]);
}
ret.push(handler.apply(handlerCtx, params));
}
});
}
......
......@@ -1857,7 +1857,7 @@ function handleEvent (event) {
}
handler.once = true;
}
const params = processEventArgs(
let params = processEventArgs(
this.$vm,
event,
eventArray[1],
......@@ -1865,9 +1865,13 @@ function handleEvent (event) {
isCustom,
methodName
);
params = Array.isArray(params) ? params : [];
// 参数尾部增加原始事件对象用于复杂表达式内获取额外数据
// eslint-disable-next-line no-sparse-arrays
ret.push(handler.apply(handlerCtx, (Array.isArray(params) ? params : []).concat([, , , , , , , , , , event])));
if (/=\s*\S+\.eventParams\s*\|\|\s*\S+\[['"]event-params['"]\]/.test(handler.toString())) {
// eslint-disable-next-line no-sparse-arrays
params = params.concat([, , , , , , , , , , event]);
}
ret.push(handler.apply(handlerCtx, params));
}
});
}
......@@ -2152,6 +2156,36 @@ function handleRef (ref) {
if (!ref) {
return
}
if (ref.props['data-com-type'] === 'wx') {
const eventProps = {};
let refProps = ref.props;
// 初始化支付宝小程序组件事件
Object.keys(refProps).forEach(key => {
const handler = refProps[key];
const res = key.match(/^on([A-Z])(\S*)/);
if (res && typeof handler === 'function' && handler.name === 'bound handleEvent') {
const event = res && (res[1].toLowerCase() + res[2]);
refProps[key] = eventProps[key] = function () {
const props = Object.assign({}, refProps);
props[key] = handler;
// 由于支付宝事件可能包含多个参数,不使用微信小程序事件格式
delete props['data-com-type'];
triggerEvent.bind({ props })(event, {
__args__: [...arguments]
});
};
}
});
// 处理 props 重写
Object.defineProperty(ref, 'props', {
get () {
return refProps
},
set (value) {
refProps = Object.assign(value, eventProps);
}
});
}
const refName = ref.props['data-ref'];
const refInForName = ref.props['data-ref-in-for'];
if (refName) {
......@@ -2162,18 +2196,20 @@ function handleRef (ref) {
}
function triggerEvent (type, detail, options) {
const handler = this.props[customize('on-' + type)];
const handler = this.props && this.props[customize('on-' + type)];
if (!handler) {
return
}
const eventOpts = this.props['data-event-opts'];
const eventParams = this.props['data-event-params'];
const comType = this.props['data-com-type'];
const target = {
dataset: {
eventOpts,
eventParams
eventParams,
comType
}
};
......@@ -2405,7 +2441,8 @@ function parsePage (vuePageOptions) {
},
__r: handleRef,
__e: handleEvent,
__l: handleLink$1
__l: handleLink$1,
triggerEvent
};
initHooks(pageOptions, hooks$1, vuePageOptions);
......
......@@ -976,16 +976,19 @@ function initHook (name, options) {
};
}
}
if (!MPPage.__$wrappered) {
MPPage.__$wrappered = true;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Page.after = MPPage.after;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
}
const PAGE_EVENT_HOOKS = [
'onPullDownRefresh',
......@@ -1522,7 +1525,7 @@ function handleEvent (event) {
}
handler.once = true;
}
const params = processEventArgs(
let params = processEventArgs(
this.$vm,
event,
eventArray[1],
......@@ -1530,9 +1533,13 @@ function handleEvent (event) {
isCustom,
methodName
);
params = Array.isArray(params) ? params : [];
// 参数尾部增加原始事件对象用于复杂表达式内获取额外数据
// eslint-disable-next-line no-sparse-arrays
ret.push(handler.apply(handlerCtx, (Array.isArray(params) ? params : []).concat([, , , , , , , , , , event])));
if (/=\s*\S+\.eventParams\s*\|\|\s*\S+\[['"]event-params['"]\]/.test(handler.toString())) {
// eslint-disable-next-line no-sparse-arrays
params = params.concat([, , , , , , , , , , event]);
}
ret.push(handler.apply(handlerCtx, params));
}
});
}
......@@ -1891,10 +1898,17 @@ function parseComponent (vueOptions) {
// 关于百度小程序生命周期的说明(组件作为页面时):
// lifetimes:attached --> methods:onShow --> methods:onLoad --> methods:onReady
// 这里在强制将onShow挪到onLoad之后触发,另外一处修改在page-parser.js
const oldAttached = componentOptions.lifetimes.attached;
let oldAttached = componentOptions.lifetimes.attached;
// 百度小程序基础库 3.260 以上支持页面 onInit 生命周期,提前创建 vm 实例
componentOptions.lifetimes.onInit = function onInit (query) {
oldAttached.call(this);
oldAttached = noop;
this.pageinstance.$vm = this.$vm;
this.$vm.__call_hook('onInit', query);
};
componentOptions.lifetimes.attached = function attached () {
oldAttached.call(this);
if (isPage.call(this)) { // 百度 onLoad 在 attached 之前触发
if (isPage.call(this)) { // 百度 onLoad 在 attached 之前触发(基础库小于 3.70)
// 百度 当组件作为页面时 pageinstancce 不是原来组件的 instance
this.pageinstance.$vm = this.$vm;
if (hasOwn(this.pageinstance, '_$args')) {
......@@ -1994,7 +2008,7 @@ function parsePage (vuePageOptions) {
};
pageOptions.methods.onLoad = function onLoad (query) {
// 百度 onLoad 在 attached 之前触发,先存储 args, 在 attached 里边触发 onLoad
// 百度 onLoad 在 attached 之前触发(基础库小于 3.70),先存储 args, 在 attached 里边触发 onLoad
if (this.$vm) {
const copyQuery = Object.assign({}, query);
delete copyQuery.__id__;
......
......@@ -754,16 +754,19 @@ function initHook (name, options) {
};
}
}
if (!MPPage.__$wrappered) {
MPPage.__$wrappered = true;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Page.after = MPPage.after;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
}
const PAGE_EVENT_HOOKS = [
'onPullDownRefresh',
......@@ -1274,7 +1277,7 @@ function handleEvent (event) {
}
handler.once = true;
}
const params = processEventArgs(
let params = processEventArgs(
this.$vm,
event,
eventArray[1],
......@@ -1282,9 +1285,13 @@ function handleEvent (event) {
isCustom,
methodName
);
params = Array.isArray(params) ? params : [];
// 参数尾部增加原始事件对象用于复杂表达式内获取额外数据
// eslint-disable-next-line no-sparse-arrays
ret.push(handler.apply(handlerCtx, (Array.isArray(params) ? params : []).concat([, , , , , , , , , , event])));
if (/=\s*\S+\.eventParams\s*\|\|\s*\S+\[['"]event-params['"]\]/.test(handler.toString())) {
// eslint-disable-next-line no-sparse-arrays
params = params.concat([, , , , , , , , , , event]);
}
ret.push(handler.apply(handlerCtx, params));
}
});
}
......
......@@ -915,16 +915,19 @@ function initHook (name, options) {
};
}
}
if (!MPPage.__$wrappered) {
MPPage.__$wrappered = true;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Page.after = MPPage.after;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
}
const PAGE_EVENT_HOOKS = [
'onPullDownRefresh',
......@@ -1435,7 +1438,7 @@ function handleEvent (event) {
}
handler.once = true;
}
const params = processEventArgs(
let params = processEventArgs(
this.$vm,
event,
eventArray[1],
......@@ -1443,9 +1446,13 @@ function handleEvent (event) {
isCustom,
methodName
);
params = Array.isArray(params) ? params : [];
// 参数尾部增加原始事件对象用于复杂表达式内获取额外数据
// eslint-disable-next-line no-sparse-arrays
ret.push(handler.apply(handlerCtx, (Array.isArray(params) ? params : []).concat([, , , , , , , , , , event])));
if (/=\s*\S+\.eventParams\s*\|\|\s*\S+\[['"]event-params['"]\]/.test(handler.toString())) {
// eslint-disable-next-line no-sparse-arrays
params = params.concat([, , , , , , , , , , event]);
}
ret.push(handler.apply(handlerCtx, params));
}
});
}
......
......@@ -1007,16 +1007,19 @@ function initHook (name, options) {
};
}
}
if (!MPPage.__$wrappered) {
MPPage.__$wrappered = true;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Page.after = MPPage.after;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
}
const PAGE_EVENT_HOOKS = [
'onPullDownRefresh',
......@@ -1527,7 +1530,7 @@ function handleEvent (event) {
}
handler.once = true;
}
const params = processEventArgs(
let params = processEventArgs(
this.$vm,
event,
eventArray[1],
......@@ -1535,9 +1538,13 @@ function handleEvent (event) {
isCustom,
methodName
);
params = Array.isArray(params) ? params : [];
// 参数尾部增加原始事件对象用于复杂表达式内获取额外数据
// eslint-disable-next-line no-sparse-arrays
ret.push(handler.apply(handlerCtx, (Array.isArray(params) ? params : []).concat([, , , , , , , , , , event])));
if (/=\s*\S+\.eventParams\s*\|\|\s*\S+\[['"]event-params['"]\]/.test(handler.toString())) {
// eslint-disable-next-line no-sparse-arrays
params = params.concat([, , , , , , , , , , event]);
}
ret.push(handler.apply(handlerCtx, params));
}
});
}
......
......@@ -676,16 +676,19 @@ function initHook (name, options) {
};
}
}
if (!MPPage.__$wrappered) {
MPPage.__$wrappered = true;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Page.after = MPPage.after;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
}
const PAGE_EVENT_HOOKS = [
'onPullDownRefresh',
......@@ -1196,7 +1199,7 @@ function handleEvent (event) {
}
handler.once = true;
}
const params = processEventArgs(
let params = processEventArgs(
this.$vm,
event,
eventArray[1],
......@@ -1204,9 +1207,13 @@ function handleEvent (event) {
isCustom,
methodName
);
params = Array.isArray(params) ? params : [];
// 参数尾部增加原始事件对象用于复杂表达式内获取额外数据
// eslint-disable-next-line no-sparse-arrays
ret.push(handler.apply(handlerCtx, (Array.isArray(params) ? params : []).concat([, , , , , , , , , , event])));
if (/=\s*\S+\.eventParams\s*\|\|\s*\S+\[['"]event-params['"]\]/.test(handler.toString())) {
// eslint-disable-next-line no-sparse-arrays
params = params.concat([, , , , , , , , , , event]);
}
ret.push(handler.apply(handlerCtx, params));
}
});
}
......
......@@ -764,16 +764,19 @@ function initHook (name, options) {
};
}
}
if (!MPPage.__$wrappered) {
MPPage.__$wrappered = true;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Page.after = MPPage.after;
Page = function (options = {}) {
initHook('onLoad', options);
return MPPage(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
Component = function (options = {}) {
initHook('created', options);
return MPComponent(options)
};
}
const PAGE_EVENT_HOOKS = [
'onPullDownRefresh',
......@@ -1284,7 +1287,7 @@ function handleEvent (event) {
}
handler.once = true;
}
const params = processEventArgs(
let params = processEventArgs(
this.$vm,
event,
eventArray[1],
......@@ -1292,9 +1295,13 @@ function handleEvent (event) {
isCustom,
methodName
);
params = Array.isArray(params) ? params : [];
// 参数尾部增加原始事件对象用于复杂表达式内获取额外数据
// eslint-disable-next-line no-sparse-arrays
ret.push(handler.apply(handlerCtx, (Array.isArray(params) ? params : []).concat([, , , , , , , , , , event])));
if (/=\s*\S+\.eventParams\s*\|\|\s*\S+\[['"]event-params['"]\]/.test(handler.toString())) {
// eslint-disable-next-line no-sparse-arrays
params = params.concat([, , , , , , , , , , event]);
}
ret.push(handler.apply(handlerCtx, params));
}
});
}
......
......@@ -4,15 +4,13 @@ const webpack = require('webpack')
const {
parseEntry,
getMainEntry,
normalizePath,
// normalizePath,
getPlatformExts,
getPlatformCssnano
} = require('@dcloudio/uni-cli-shared')
const WebpackUniAppPlugin = require('../../packages/webpack-uni-app-loader/plugin/index')
const CustomModuleIdsPlugin = require('../../packages/webpack-custom-module-ids-plugin/index')
const modifyVueLoader = require('../vue-loader')
const {
......@@ -66,86 +64,67 @@ function getProvides () {
return provides
}
function processWxss (name, assets) {
const dirname = path.dirname(name)
const mainWxssCode = `@import "${path.relative(dirname, 'common/main.wxss')}";`
const code = `${mainWxssCode}` + assets[name].source().toString()
assets[name] = {
size () {
return Buffer.byteLength(code, 'utf8')
},
source () {
return code
}
}
}
function procssJs (name, assets, hasVendor) {
const dirname = path.dirname(name)
const runtimeJsCode = `require('${path.relative(dirname, 'common/runtime.js')}');`
const vendorJsCode = hasVendor ? `require('${path.relative(dirname, 'common/vendor.js')}');` : ''
const mainJsCode = `require('${path.relative(dirname, 'common/main.js')}');`
const code = `${runtimeJsCode}${vendorJsCode}${mainJsCode}` + assets[name].source().toString()
assets[name] = {
size () {
return Buffer.byteLength(code, 'utf8')
},
source () {
return code
}
}
}
class PreprocessAssetsPlugin {
apply (compiler) {
compiler.hooks.emit.tap('PreprocessAssetsPlugin', compilation => {
const assets = compilation.assets
const hasMainWxss = assets['common/main.wxss']
const hasVendor = assets['common/vendor.js']
Object.keys(assets).forEach(name => {
const extname = path.extname(name)
if (extname !== '.js') {
return
}
if (name.startsWith('common')) {
return
}
const dirname = path.dirname(name)
const runtimeJsCode = `require('${path.relative(dirname, 'common/runtime.js')}');`
const vendorJsCode = hasVendor ? `require('${path.relative(dirname, 'common/vendor.js')}');` : ''
const code = `${runtimeJsCode}${vendorJsCode}` + assets[name].source().toString()
assets[name] = {
size () {
return Buffer.byteLength(code, 'utf8')
},
source () {
return code
}
const extname = path.extname(name)
if (extname === '.wxss' && hasMainWxss && process.UNI_ENTRY[name.replace(extname, '')]) {
processWxss(name, assets)
} else if (extname === '.js') {
procssJs(name, assets, hasVendor)
}
})
delete assets['common/main.js']
// delete assets['common/main.js']
delete assets['app.js']
delete assets['app.json']
delete assets['app.wxss']
delete assets['project.config.json']
console.log(Object.keys(assets))
})
}
}
function initSubpackageConfig (webpackConfig, vueOptions) {
webpackConfig.node.set('global', false)
webpackConfig.plugins.delete('hash-module-ids')
// 与子包共享的模块
const sharedModules = {
'uni-mp-weixin/dist/index.js': 'uniWeixin',
'mp-vue/dist/mp.runtime.esm.js': 'uniVue'
}
const sharedModulePaths = Object.keys(sharedModules)
webpackConfig
.plugin('custom-hash-module-ids')
.use(CustomModuleIdsPlugin, [{
prefix: process.env.UNI_SUBPACKGE,
custom (libIdent) {
if (!libIdent) {
return
}
const normalizedLibIdent = normalizePath(libIdent)
const name = sharedModulePaths.find(p => normalizedLibIdent.endsWith(p))
if (name) {
return sharedModules[name]
}
}
}])
if (process.env.UNI_SUBPACKGE !== 'main') { // 非主包
process.env.UNI_OUTPUT_DIR = path.resolve(process.env.UNI_OUTPUT_DIR, process.env.UNI_SUBPACKGE)
vueOptions.outputDir = process.env.UNI_OUTPUT_DIR
webpackConfig.output.path(process.env.UNI_OUTPUT_DIR)
webpackConfig.output.jsonpFunction('webpackJsonp_' + process.env.UNI_SUBPACKGE)
webpackConfig.externals([
function (context, request, callback) {
if (request === 'vue') {
return callback(null, 'root global["webpackMain"]["uniVue"]')
}
const normalizedRequest = normalizePath(request)
const name = sharedModulePaths.find(p => normalizedRequest.endsWith(p))
if (name) {
return callback(null, `root global["webpackMain"]["${sharedModules[name]}"]`)
}
callback()
}
])
}
process.env.UNI_OUTPUT_DIR = path.resolve(process.env.UNI_OUTPUT_DIR, process.env.UNI_SUBPACKGE)
vueOptions.outputDir = process.env.UNI_OUTPUT_DIR
webpackConfig.output.path(process.env.UNI_OUTPUT_DIR)
webpackConfig.output.jsonpFunction('webpackJsonp_' + process.env.UNI_SUBPACKGE)
}
module.exports = {
......@@ -169,13 +148,7 @@ module.exports = {
const statCode = process.env.UNI_USING_STAT ? 'import \'@dcloudio/uni-stat\';' : ''
let beforeCode = 'import \'uni-pages\';'
if (process.env.UNI_SUBPACKGE === 'main') {
const uniPath = require('@dcloudio/uni-cli-shared/lib/platform').getMPRuntimePath()
beforeCode +=
`import uniVue from 'vue';import * as uniWeixin from '${uniPath}';global['webpackMain']={uniVue,uniWeixin};`
}
const beforeCode = 'import \'uni-pages\';'
const plugins = [
new WebpackUniAppPlugin(),
......
......@@ -69,9 +69,7 @@ module.exports = function generateComponent (compilation, jsonpFunction = 'webpa
const modules = compilation.modules
const concatenatedModules = modules.filter(module => module.modules)
const uniModule = !process.env.UNI_SUBPACKAGE && modules.find(module => module.resource && normalizePath(module.resource) ===
uniPath)
const uniModuleId = uniModule && uniModule.id
const uniModuleId = modules.find(module => module.resource && normalizePath(module.resource) === uniPath).id
const styleImports = {}
const fixSlots = {}
......@@ -109,19 +107,13 @@ module.exports = function generateComponent (compilation, jsonpFunction = 'webpa
if (process.env.UNI_PLATFORM === 'mp-alipay') {
beforeCode = ';my.defineComponent || (my.defineComponent = Component);'
}
let requireCode =
`__webpack_require__('${uniModuleId}')['createComponent'](__webpack_require__(${JSON.stringify(moduleId)}))`
if (process.env.UNI_SUBPACKGE) {
requireCode =
`global['webpackMain']['uniWeixin']['createComponent'](__webpack_require__(${JSON.stringify(moduleId)}))`
}
const source = beforeCode + origSource +
`
;(${globalVar}["${jsonpFunction}"] = ${globalVar}["${jsonpFunction}"] || []).push([
'${chunkName}',
{
'${chunkName}':(function(module, exports, __webpack_require__){
${requireCode}
__webpack_require__('${uniModuleId}')['createComponent'](__webpack_require__(${JSON.stringify(moduleId)}))
})
},
[['${chunkName}']]
......
......@@ -179,10 +179,8 @@ module.exports = function generateJson (compilation) {
delete jsonObj.navigationBarShadow
}
if (process.env.UNI_SUBPACKGE && process.env.UNI_SUBPACKGE !== 'main') {
if (jsonObj.usingComponents) {
jsonObj.usingComponents = normalizeUsingComponents(name, jsonObj.usingComponents)
}
if (process.env.UNI_SUBPACKGE && jsonObj.usingComponents) {
jsonObj.usingComponents = normalizeUsingComponents(name, jsonObj.usingComponents)
}
const source = JSON.stringify(jsonObj, null, 2)
......
......@@ -37,14 +37,16 @@ function initHook (name, options) {
}
}
}
if (!MPPage.__$wrappered) {
MPPage.__$wrappered = true
Page = function (options = {}) {
initHook('onLoad', options)
return MPPage(options)
}
Page.after = MPPage.after
Page = function (options = {}) {
initHook('onLoad', options)
return MPPage(options)
}
Page.after = MPPage.after
Component = function (options = {}) {
initHook('created', options)
return MPComponent(options)
}
Component = function (options = {}) {
initHook('created', options)
return MPComponent(options)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册