Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
zhetengbiji committed Aug 7, 2019
2 parents 5a549d1 + 37c99d8 commit 98d7eb0
Show file tree
Hide file tree
Showing 23 changed files with 1,431 additions and 103 deletions.
1 change: 1 addition & 0 deletions docs/collocation/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* [pages.json](collocation/pages.md)
* [manifest.json](collocation/manifest.md)
* [uni.scss](collocation/uni-scss.md)
* [vue-config.js](collocation/vue-config.md)
* 框架接口
* [日志打印](collocation/frame/log.md)
* [定时器](collocation/frame/timer.md)
Expand Down
27 changes: 26 additions & 1 deletion docs/collocation/manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ splash(启动封面)是App必然存在的、不可取消的。
|async|Object|参考:[async](collocation/manifest?id=h5-async)|
|devServer|Object|开发环境 server 配置,参考:[devServer](collocation/manifest?id=devserver)|
|publicPath|String|引用资源的地址前缀,仅发布时生效。参考:[publicPath](collocation/manifest?id=publicPath)|
|sdkConfigs|String|SDK配置,例如地图... 参考:[sdkConfigs](collocation/manifest?id=h5sdkconfig)|
|sdkConfigs|String|SDK配置,例如地图... 参考:[sdkConfigs](collocation/manifest?id=h5sdkconfig)|
|optimization|Object|打包优化配置(HBuilderX 2.1.5 以上支持),参考[optimization](collocation/manifest?id=optimization)|


#### 自定义模板@h5-template
需要使用自定义模板的场景,通常有以下几种情况:
Expand Down Expand Up @@ -254,7 +256,30 @@ Tips:`uni-app` 中 `manifest.json->h5->devServer` 实际上对应 `webpack`
}
```
#### optimization
|属性|类型|默认值|说明|
|:-|:-|:-|:-|
|prefetch|Boolean|false|资源预取|
|preload|Boolean|false|资源预加载|
|treeShaking|Object||摇树优化,根据项目需求,动态打包框架所需的组件及API,保持框架代码最精简化,参考[treeShaking](collocation/manifest?id=treeshaking)|
##### treeShaking
|属性|类型|默认值|说明|
|:-|:-|:-|:-|
|enable|Boolean|false|是否启用摇树优化|
**示例:**
```json
"h5": {
"optimization": {
"treeShaking": {
"enable": true
}
}
}
```
### mp-weixin
Expand Down
36 changes: 36 additions & 0 deletions docs/collocation/package.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package.json 是一个可选的配置文件,如果项目的根目录中存在这个文件,且根节点下含有`uni-app`节点,则支持自定义条件编译平台(如钉钉小程序、H5-weixin等)。

如下是一个自定义微信服务号平台(H5-WEIXIN)的示例配置:

```json
"uni-app": {
"scripts": {
"h5-weixin": { //自定义编译平台配置,可通过cli方式调用
"env": {//环境变量
"UNI_PLATFORM": "h5" //基准平台
},
"define": { //自定义条件编译
"H5-WEIXIN": true //自定义常量,建议为大写
}
}
}
}
```

开发者可在代码块中使用`H5-WEIXIN`变量,如下:

```
// #ifdef H5-WEIXIN
微信服务号特有代码
// #endif
```

开发者可通过如下命令,启动微信服务号平台(H5-WEIXIN)平台的编译:
```
npm run dev:custom h5-weixin
npm run build:custom h5-weixin
```

Tips:

- `UNI_PLATFORM`仅支持填写`uni-app`默认支持的基准平台,目前仅限如下枚举值:`app-plus``h5``mp-weixin``mp-alipay``mp-baidu``mp-toutiao``mp-qq`
18 changes: 18 additions & 0 deletions docs/collocation/vue-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
vue.config.js 是一个可选的配置文件,如果项目的根目录中存在这个文件,那么它会被自动加载,具体规范参考:[vue-config.js](https://cli.vuejs.org/zh/config/#vue-config-js)

**支持情况**
* CLI 工程
* HBuilderX 2.1.5 及以上版本

**注意事项**

部分配置项会被编译配置覆盖,例如:

* publicPath 不支持,如果需要配置,请在 manifest.json->h5->router->base 中配置,参考文档:[h5-router](collocation/manifest?id=h5-router)
* outputDir 不支持
* assetsDir 固定 static
* pages 不支持
* runtimeCompiler 固定 false
* productionSourceMap 固定 false
* css.extract H5 平台固定 false,其他平台固定 true
* parallel 固定 false
228 changes: 217 additions & 11 deletions packages/uni-app-plus/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,198 @@ const camelize = cached((str) => {
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
});

const SYNC_API_RE = /^\$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;
const HOOKS = [
'invoke',
'success',
'fail',
'complete',
'returnValue'
];

const globalInterceptors = {};
const scopedInterceptors = {};

function mergeHook (parentVal, childVal) {
const res = childVal
? parentVal
? parentVal.concat(childVal)
: Array.isArray(childVal)
? childVal : [childVal]
: parentVal;
return res
? dedupeHooks(res)
: res
}

function dedupeHooks (hooks) {
const res = [];
for (let i = 0; i < hooks.length; i++) {
if (res.indexOf(hooks[i]) === -1) {
res.push(hooks[i]);
}
}
return res
}

function removeHook (hooks, hook) {
const index = hooks.indexOf(hook);
if (index !== -1) {
hooks.splice(index, 1);
}
}

function mergeInterceptorHook (interceptor, option) {
Object.keys(option).forEach(hook => {
if (HOOKS.indexOf(hook) !== -1 && isFn(option[hook])) {
interceptor[hook] = mergeHook(interceptor[hook], option[hook]);
}
});
}

function removeInterceptorHook (interceptor, option) {
if (!interceptor || !option) {
return
}
Object.keys(option).forEach(hook => {
if (HOOKS.indexOf(hook) !== -1 && isFn(option[hook])) {
removeHook(interceptor[hook], option[hook]);
}
});
}

function addInterceptor (method, option) {
if (typeof method === 'string' && isPlainObject(option)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), option);
} else if (isPlainObject(method)) {
mergeInterceptorHook(globalInterceptors, method);
}
}

function removeInterceptor (method, option) {
if (typeof method === 'string') {
if (isPlainObject(option)) {
removeInterceptorHook(scopedInterceptors[method], option);
} else {
delete scopedInterceptors[method];
}
} else if (isPlainObject(method)) {
removeInterceptorHook(globalInterceptors, method);
}
}

function wrapperHook (hook) {
return function (data) {
return hook(data) || data
}
}

function isPromise (obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
}

function queue (hooks, data) {
let promise = false;
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
if (promise) {
promise = Promise.then(wrapperHook(hook));
} else {
const res = hook(data);
if (isPromise(res)) {
promise = Promise.resolve(res);
}
if (res === false) {
return {
then () {}
}
}
}
}
return promise || {
then (callback) {
return callback(data)
}
}
}

function wrapperOptions (interceptor, options = {}) {
['success', 'fail', 'complete'].forEach(name => {
if (Array.isArray(interceptor[name])) {
const oldCallback = options[name];
options[name] = function callbackInterceptor (res) {
queue(interceptor[name], res).then((res) => {
/* eslint-disable no-mixed-operators */
return isFn(oldCallback) && oldCallback(res) || res
});
};
}
});
return options
}

function wrapperReturnValue (method, returnValue) {
const returnValueHooks = [];
if (Array.isArray(globalInterceptors.returnValue)) {
returnValueHooks.push(...globalInterceptors.returnValue);
}
const interceptor = scopedInterceptors[method];
if (interceptor && Array.isArray(interceptor.returnValue)) {
returnValueHooks.push(...interceptor.returnValue);
}
returnValueHooks.forEach(hook => {
returnValue = hook(returnValue) || returnValue;
});
return returnValue
}

function getApiInterceptorHooks (method) {
const interceptor = Object.create(null);
Object.keys(globalInterceptors).forEach(hook => {
if (hook !== 'returnValue') {
interceptor[hook] = globalInterceptors[hook].slice();
}
});
const scopedInterceptor = scopedInterceptors[method];
if (scopedInterceptor) {
Object.keys(scopedInterceptor).forEach(hook => {
if (hook !== 'returnValue') {
interceptor[hook] = (interceptor[hook] || []).concat(scopedInterceptor[hook]);
}
});
}
return interceptor
}

function invokeApi (method, api, options, ...params) {
const interceptor = getApiInterceptorHooks(method);
if (interceptor && Object.keys(interceptor).length) {
if (Array.isArray(interceptor.invoke)) {
const res = queue(interceptor.invoke, options);
return res.then((options) => {
return api(wrapperOptions(interceptor, options), ...params)
})
} else {
return api(wrapperOptions(interceptor, options), ...params)
}
}
return api(options, ...params)
}

const promiseInterceptor = {
returnValue (res) {
if (!isPromise(res)) {
return res
}
return res.then(res => {
return res[1]
}).catch(res => {
return res[0]
})
}
};

const SYNC_API_RE =
/^\$|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;

const CONTEXT_API_RE = /^create|Manager$/;

Expand Down Expand Up @@ -81,10 +272,10 @@ function promisify (name, api) {
}
return function promiseApi (options = {}, ...params) {
if (isFn(options.success) || isFn(options.fail) || isFn(options.complete)) {
return api(options, ...params)
return wrapperReturnValue(name, invokeApi(name, api, options, ...params))
}
return handlePromise(new Promise((resolve, reject) => {
api(Object.assign({}, options, {
return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => {
invokeApi(name, api, Object.assign({}, options, {
success: resolve,
fail: reject
}), ...params);
Expand All @@ -100,7 +291,7 @@ function promisify (name, api) {
)
};
}
}))
})))
}
}

Expand Down Expand Up @@ -146,6 +337,19 @@ function upx2px (number, newDeviceWidth) {
return number < 0 ? -result : result
}

const interceptors = {
promiseInterceptor
};



var baseApi = /*#__PURE__*/Object.freeze({
upx2px: upx2px,
interceptors: interceptors,
addInterceptor: addInterceptor,
removeInterceptor: removeInterceptor
});

const protocols = {};
const todos = [];
const canIUses = [];
Expand Down Expand Up @@ -1224,8 +1428,8 @@ let uni = {};
if (typeof Proxy !== 'undefined' && "app-plus" !== 'app-plus') {
uni = new Proxy({}, {
get (target, name) {
if (name === 'upx2px') {
return upx2px
if (baseApi[name]) {
return baseApi[name]
}
if (api[name]) {
return promisify(name, api[name])
Expand All @@ -1239,8 +1443,10 @@ if (typeof Proxy !== 'undefined' && "app-plus" !== 'app-plus') {
return promisify(name, wrapper(name, wx[name]))
}
});
} else {
uni.upx2px = upx2px;
} else {
Object.keys(baseApi).forEach(name => {
uni[name] = baseApi[name];
});

Object.keys(eventApi).forEach(name => {
uni[name] = eventApi[name];
Expand All @@ -1258,12 +1464,12 @@ if (typeof Proxy !== 'undefined' && "app-plus" !== 'app-plus') {
}

{
if (typeof global !== 'undefined') {
if (typeof global !== 'undefined') {
global.uni = uni;
global.UniEmitter = eventApi;
}
}


wx.createApp = createApp;
wx.createPage = createPage;
wx.createComponent = createComponent;
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-app-plus/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dcloudio/uni-app-plus",
"version": "0.0.247",
"version": "0.0.248",
"description": "uni-app app-plus",
"main": "dist/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-h5/dist/index.umd.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 98d7eb0

Please sign in to comment.