Skip to content

Commit

Permalink
generate api manifest.json
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Jul 27, 2019
1 parent ec06666 commit 158000c
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 27 deletions.
17 changes: 9 additions & 8 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module.exports = {
ignore: [
"./packages",
],
presets: [
["@vue/app", {
useBuiltIns: "entry"
}]
]
ignore: [
"./packages",
],
presets: [
["@vue/app", {
useBuiltIns: "entry"
}]
],
plugins: [require('./lib/babel-plugin-uni-api/index.js')]
}
14 changes: 12 additions & 2 deletions build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@ const copy = require('copy')
const path = require('path')
const jsonfile = require('jsonfile')

const {
generateApiManifest
} = require('./manifest')

const service = new Service(process.env.VUE_CLI_CONTEXT || process.cwd(), {
inlineOptions: require('./vue.config.js')
})
// 删除 cache 目录
del.sync(['node_modules/.cache'])

service.run('build', {
name: 'index',
watch: process.env.UNI_WATCH === 'true',
target: 'lib',
formats: process.env.UNI_WATCH === 'true' ? 'umd' : 'umd-min',
entry: './lib/' + process.env.UNI_PLATFORM + '/main.js'
}).then(function () {
generateApiManifest(process.UNI_SERVICE_API_MANIFEST)
}).catch(err => {
error(err)
process.exit(1)
Expand All @@ -44,9 +52,11 @@ if (process.env.UNI_WATCH === 'false') {
})
})
.then(obj => {
return jsonfile.writeFile(packageJsonPath, obj, { spaces: 2 })
return jsonfile.writeFile(packageJsonPath, obj, {
spaces: 2
})
})
.catch(err => {
throw err
})
}
}
46 changes: 46 additions & 0 deletions build/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fs = require('fs')
const path = require('path')

const apis = require('../src/core/helpers/apis')

function parseApiManifestDeps (manifest) {
Object.keys(manifest).forEach(name => {
const deps = manifest[name][1]
if (deps && deps.length) {
deps.forEach(dep => {
if (manifest[dep[1]]) {
dep[0] = manifest[dep[1]][0]
} else {
console.warn(`依赖模块[${dep[1]}]不存在`)
}
})
}
})
}

module.exports = {
generateApiManifest (manifest) {
parseApiManifestDeps(manifest)

const manifestJson = Object.create(null)
const todoApis = []
apis.forEach(name => {
if (manifest[name]) {
manifestJson[name] = manifest[name]
} else {
todoApis.push(name)
}
})

if (todoApis.length) {
console.warn(`${process.env.UNI_PLATFORM} 平台缺少以下 API 实现`)
todoApis.forEach(name => {
console.warn(name)
})
}

fs.writeFileSync(path.resolve(__dirname, '../packages/uni-h5/manifest.json'),
JSON.stringify(manifestJson, null, 4)
)
}
}
85 changes: 85 additions & 0 deletions lib/babel-plugin-uni-api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const path = require('path')

const isWin = /^win/.test(process.platform)

const normalizePath = path => (isWin ? path.replace(/\\/g, '/') : path)

function resolve(...args) {
return normalizePath(path.resolve.apply(path, [__dirname, ...args]))
}

const srcPath = resolve('../../src/')
const coreApiPath = resolve('../../src/core/service/api')
const platformApiPath = resolve('../../src/platforms/' + process.env.UNI_PLATFORM + '/service/api')

const apis = require('../../src/core/helpers/apis')

process.UNI_SERVICE_API_MANIFEST = Object.create(null)

module.exports = function({
types: t
}) {
return {
visitor: {
Program: {
enter(path, state) {
state.file.opts.file = normalizePath(state.file.opts.filename)
state.file.opts.isCore = state.file.opts.file.indexOf(coreApiPath) === 0
state.file.opts.isPlatform = state.file.opts.file.indexOf(platformApiPath) === 0
},
exit(path, state) {
const {
file,
methods,
exports,
isPlatform
} = state.file.opts
if (exports && exports.length) {
const deps = []
methods && methods.forEach(method => {
deps.push(['', method])
})
const filepath = file.replace(srcPath, '')
exports.forEach(exportName => {
if (process.UNI_SERVICE_API_MANIFEST[exportName]) {
console.warn(`API[${exportName}] 冲突:`)
console.warn(process.UNI_SERVICE_API_MANIFEST[exportName][0])
console.warn(filepath)
if (isPlatform) { // 优先使用 platform
process.UNI_SERVICE_API_MANIFEST[exportName] = [filepath, deps]
console.warn(`优先使用` + filepath)
}
} else {
process.UNI_SERVICE_API_MANIFEST[exportName] = [filepath, deps]
}
})
}
}
},
ExportNamedDeclaration(path, state) {
const {
file,
isCore,
isPlatform
} = state.file.opts
if ((isCore || isPlatform) && t.isFunctionDeclaration(path.node.declaration)) {
(state.file.opts.exports || (state.file.opts.exports = [])).push(path.node.declaration.id.name)
}
},
CallExpression(path, state) {
const {
file,
isCore,
isPlatform
} = state.file.opts

if (
isCore &&
path.node.callee.name === 'invokeMethod'
) {
(state.file.opts.methods || (state.file.opts.methods = new Set())).add(path.node.arguments[0].value)
}
}
}
}
}
84 changes: 71 additions & 13 deletions packages/uni-app-plus-nvue/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1556,9 +1556,9 @@ var serviceContext = (function () {
'unsubscribePush',
'onPush',
'offPush',
'requireNativePlugin',
'requireNativePlugin',
'upx2px'
];
];

const apis = [
...base,
Expand All @@ -1570,11 +1570,13 @@ var serviceContext = (function () {
...device,
...keyboard,
...ui,
...event,
...file,
...event,
...file,
...canvas,
...third
];
];

var apis_1 = apis;

/**
* 框架内 try-catch
Expand Down Expand Up @@ -1938,7 +1940,7 @@ var serviceContext = (function () {

const upx2px = [{
name: 'upx',
type: Number,
type: [Number, String],
required: true
}];

Expand Down Expand Up @@ -3423,6 +3425,15 @@ var serviceContext = (function () {
const invokeCallback = function (res) {
res.errMsg = res.errMsg || apiName + ':ok';

// 部分 api 可能返回的 errMsg 的 api 名称部分不一致,格式化为正确的
if (res.errMsg.indexOf(':ok') !== -1) {
res.errMsg = apiName + ':ok';
} else if (res.errMsg.indexOf(':cancel') !== -1) {
res.errMsg = apiName + ':cancel';
} else if (res.errMsg.indexOf(':fail') !== -1) {
res.errMsg = apiName + ':fail';
}

const errMsg = res.errMsg;

if (errMsg.indexOf(apiName + ':ok') === 0) {
Expand Down Expand Up @@ -7685,6 +7696,52 @@ var serviceContext = (function () {
return UniServiceJSBridge.on('api.' + name, callback)
}

const callbacks$1 = [];

onMethod('onAccelerometerChange', function (res) {
callbacks$1.forEach(callbackId => {
invoke(callbackId, res);
});
});

let isEnable = false;
/**
* 监听加速度
* @param {*} callbackId
*/
function onAccelerometerChange (callbackId) {
// TODO 当没有 start 时,添加 on 需要主动 start?
callbacks$1.push(callbackId);
if (!isEnable) {
startAccelerometer();
}
}

function startAccelerometer ({
interval // TODO
} = {}) {
if (isEnable) {
return
}
isEnable = true;
return invokeMethod('enableAccelerometer', {
enable: true
})
}

function stopAccelerometer () {
isEnable = false;
return invokeMethod('enableAccelerometer', {
enable: false
})
}

var require_context_module_1_4 = /*#__PURE__*/Object.freeze({
onAccelerometerChange: onAccelerometerChange,
startAccelerometer: startAccelerometer,
stopAccelerometer: stopAccelerometer
});

const requestTasks$1 = Object.create(null);

function formatResponse (res, args) {
Expand Down Expand Up @@ -7786,7 +7843,7 @@ var serviceContext = (function () {
return new RequestTask(requestTaskId)
}

var require_context_module_1_4 = /*#__PURE__*/Object.freeze({
var require_context_module_1_5 = /*#__PURE__*/Object.freeze({
request: request$1
});

Expand Down Expand Up @@ -7895,7 +7952,7 @@ var serviceContext = (function () {
return res
}

var require_context_module_1_5 = /*#__PURE__*/Object.freeze({
var require_context_module_1_6 = /*#__PURE__*/Object.freeze({
setStorage: setStorage$1,
setStorageSync: setStorageSync$1,
getStorage: getStorage$1,
Expand All @@ -7916,7 +7973,7 @@ var serviceContext = (function () {
return {}
}

var require_context_module_1_6 = /*#__PURE__*/Object.freeze({
var require_context_module_1_7 = /*#__PURE__*/Object.freeze({
pageScrollTo: pageScrollTo$1
});

Expand All @@ -7929,9 +7986,10 @@ var serviceContext = (function () {
'./base/can-i-use.js': require_context_module_1_1,
'./base/interceptor.js': require_context_module_1_2,
'./base/upx2px.js': require_context_module_1_3,
'./network/request.js': require_context_module_1_4,
'./storage/storage.js': require_context_module_1_5,
'./ui/page-scroll-to.js': require_context_module_1_6,
'./device/accelerometer.js': require_context_module_1_4,
'./network/request.js': require_context_module_1_5,
'./storage/storage.js': require_context_module_1_6,
'./ui/page-scroll-to.js': require_context_module_1_7,

};
var req = function req(key) {
Expand All @@ -7952,7 +8010,7 @@ var serviceContext = (function () {

const uni$1 = Object.create(null);

apis.forEach(name => {
apis_1.forEach(name => {
if (api$1[name]) {
uni$1[name] = promisify(name, wrapper(name, api$1[name]));
} else {
Expand Down
16 changes: 12 additions & 4 deletions src/core/service/api/base/base64.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { encode, decode } from 'base64-arraybuffer'

export const base64ToArrayBuffer = decode
export const arrayBufferToBase64 = encode
import {
encode,
decode
} from 'base64-arraybuffer'

export function base64ToArrayBuffer (str) {
return decode(str)
}

export function arrayBufferToBase64 (buffer) {
return encode(buffer)
}

0 comments on commit 158000c

Please sign in to comment.