提交 fb2e5277 编写于 作者: Q qiang

feat: App、H5 新增 rpx 计算相关配置

上级 211d4749
...@@ -17,7 +17,7 @@ const { ...@@ -17,7 +17,7 @@ const {
} = require('@dcloudio/uni-cli-shared/lib/cache') } = require('@dcloudio/uni-cli-shared/lib/cache')
const { const {
initTheme, initTheme,
parseTheme parseTheme
} = require('@dcloudio/uni-cli-shared/lib/theme') } = require('@dcloudio/uni-cli-shared/lib/theme')
...@@ -95,7 +95,7 @@ module.exports = function (content, map) { ...@@ -95,7 +95,7 @@ module.exports = function (content, map) {
}) })
} }
const jsonFiles = require('./platforms/' + process.env.UNI_PLATFORM)(pagesJson, manifestJson) const jsonFiles = require('./platforms/' + process.env.UNI_PLATFORM)(pagesJson, manifestJson, isAppView)
if (jsonFiles && jsonFiles.length) { if (jsonFiles && jsonFiles.length) {
if (process.env.UNI_USING_V3) { if (process.env.UNI_USING_V3) {
...@@ -144,4 +144,4 @@ module.exports = function (content, map) { ...@@ -144,4 +144,4 @@ module.exports = function (content, map) {
} }
this.callback(null, '', map) this.callback(null, '', map)
} }
...@@ -7,10 +7,14 @@ function generatePageCode (pages, pageOptions) { ...@@ -7,10 +7,14 @@ function generatePageCode (pages, pageOptions) {
}).join('\n') }).join('\n')
} }
module.exports = function definePages (appJson) { function generateUniConfig (appJson, isAppView) {
return isAppView ? `window.__uniConfig = ${JSON.stringify({ window: appJson.window }, null)};` : ''
}
module.exports = function definePages (appJson, isAppView) {
return { return {
name: 'define-pages.js', name: 'define-pages.js',
content: ` content: `
if (typeof Promise !== 'undefined' && !Promise.prototype.finally) { if (typeof Promise !== 'undefined' && !Promise.prototype.finally) {
Promise.prototype.finally = function(callback) { Promise.prototype.finally = function(callback) {
const promise = this.constructor const promise = this.constructor
...@@ -22,10 +26,11 @@ if (typeof Promise !== 'undefined' && !Promise.prototype.finally) { ...@@ -22,10 +26,11 @@ if (typeof Promise !== 'undefined' && !Promise.prototype.finally) {
) )
} }
} }
${generateUniConfig(appJson, isAppView)}
if(uni.restoreGlobal){ if(uni.restoreGlobal){
uni.restoreGlobal(weex,plus,setTimeout,clearTimeout,setInterval,clearInterval) uni.restoreGlobal(weex,plus,setTimeout,clearTimeout,setInterval,clearInterval)
} }
${generatePageCode(appJson.pages, appJson.page)} ${generatePageCode(appJson.pages, appJson.page)}
` `
} }
} }
...@@ -91,7 +91,7 @@ function updateFileFlag (appJson) { ...@@ -91,7 +91,7 @@ function updateFileFlag (appJson) {
} }
} }
module.exports = function (pagesJson, userManifestJson) { module.exports = function (pagesJson, userManifestJson, isAppView) {
const { const {
app app
} = require('../mp')(pagesJson, userManifestJson) } = require('../mp')(pagesJson, userManifestJson)
...@@ -179,7 +179,7 @@ module.exports = function (pagesJson, userManifestJson) { ...@@ -179,7 +179,7 @@ module.exports = function (pagesJson, userManifestJson) {
if (!manifestJson.permissions) { if (!manifestJson.permissions) {
manifestJson.permissions = {} manifestJson.permissions = {}
} }
const nvuePages = process.env.UNI_USING_V3_NATIVE ? pagesJson.pages : (pagesJson.nvue && pagesJson.nvue.pages) const nvuePages = process.env.UNI_USING_V3_NATIVE ? pagesJson.pages : (pagesJson.nvue && pagesJson.nvue.pages)
if (nvuePages && nvuePages.length) { if (nvuePages && nvuePages.length) {
...@@ -197,12 +197,12 @@ module.exports = function (pagesJson, userManifestJson) { ...@@ -197,12 +197,12 @@ module.exports = function (pagesJson, userManifestJson) {
appJson.nvue = { appJson.nvue = {
pages pages
} }
if (process.env.UNI_USING_V3_NATIVE) { if (process.env.UNI_USING_V3_NATIVE) {
appJson.nvue.entryPagePath = nvuePages[0] appJson.nvue.entryPagePath = nvuePages[0]
} else if (pagesJson.nvue.entryPagePath) { } else if (pagesJson.nvue.entryPagePath) {
appJson.nvue.entryPagePath = pagesJson.nvue.entryPagePath appJson.nvue.entryPagePath = pagesJson.nvue.entryPagePath
} }
// nvue 权限 // nvue 权限
manifestJson.permissions.UniNView = { manifestJson.permissions.UniNView = {
...@@ -347,10 +347,10 @@ module.exports = function (pagesJson, userManifestJson) { ...@@ -347,10 +347,10 @@ module.exports = function (pagesJson, userManifestJson) {
resources[key.replace(/\.nvue$/, '.js')] = confusion.resources[key] resources[key.replace(/\.nvue$/, '.js')] = confusion.resources[key]
} }
if (!Object.keys(nvuePages).find(path => { if (!Object.keys(nvuePages).find(path => {
const subNVues = nvuePages[path].window.subNVues || [] const subNVues = nvuePages[path].window.subNVues || []
// TODO // TODO
return (path.replace(/\.html$/, '.nvue') === key || path.replace(/\.html$/, '.nvue') + '.nvue' === key) || subNVues.find(({ return (path.replace(/\.html$/, '.nvue') === key || path.replace(/\.html$/, '.nvue') + '.nvue' === key) || subNVues.find(({
path path
}) => path === key.replace(/\.nvue$/, '')) }) => path === key.replace(/\.nvue$/, ''))
}) && !pagesJson.pages.find(({ }) && !pagesJson.pages.find(({
style = {} style = {}
...@@ -514,7 +514,7 @@ module.exports = function (pagesJson, userManifestJson) { ...@@ -514,7 +514,7 @@ module.exports = function (pagesJson, userManifestJson) {
manifest, manifest,
pagesJson, pagesJson,
normalizeNetworkTimeout normalizeNetworkTimeout
}) }, isAppView)
} }
return [app, manifest] return [app, manifest]
} }
...@@ -52,7 +52,7 @@ module.exports = function (appJson, manifestJson, { ...@@ -52,7 +52,7 @@ module.exports = function (appJson, manifestJson, {
pagesJson, pagesJson,
manifest, manifest,
normalizeNetworkTimeout normalizeNetworkTimeout
}) { }, isAppView) {
parseEntryPagePath(appJson, manifestJson) parseEntryPagePath(appJson, manifestJson)
// timeout // timeout
...@@ -82,12 +82,12 @@ module.exports = function (appJson, manifestJson, { ...@@ -82,12 +82,12 @@ module.exports = function (appJson, manifestJson, {
// 删除首页 style 中的 uni-app 配置(不注入 app-view.js) // 删除首页 style 中的 uni-app 配置(不注入 app-view.js)
delete manifestJson.plus.launchwebview['uni-app'] delete manifestJson.plus.launchwebview['uni-app']
const entryPagePath = appJson.entryPagePath const entryPagePath = appJson.entryPagePath
if (!appJson.page[entryPagePath]) { if (!appJson.page[entryPagePath]) {
console.error( console.error(
`pages.json condition['list'][current]['path']: ${entryPagePath} 需在 pages 数组中` `pages.json condition['list'][current]['path']: ${entryPagePath} 需在 pages 数组中`
) )
process.exit(0) process.exit(0)
} }
if (appJson.page[entryPagePath].nvue) { // 首页是 nvue if (appJson.page[entryPagePath].nvue) { // 首页是 nvue
manifestJson.launch_path = '' // 首页地址为空 manifestJson.launch_path = '' // 首页地址为空
...@@ -109,5 +109,5 @@ module.exports = function (appJson, manifestJson, { ...@@ -109,5 +109,5 @@ module.exports = function (appJson, manifestJson, {
manifest.name = 'manifest.json' manifest.name = 'manifest.json'
manifest.content = JSON.stringify(manifest.content) manifest.content = JSON.stringify(manifest.content)
delete appJson.nvue delete appJson.nvue
return [manifest, definePages(appJson), appConfigService(appJson)] return [manifest, definePages(appJson, isAppView), appConfigService(appJson)]
} }
...@@ -16,6 +16,11 @@ function checkDeviceWidth () { ...@@ -16,6 +16,11 @@ function checkDeviceWidth () {
isIOS = platform === 'ios' isIOS = platform === 'ios'
} }
function checkValue (value, defaultValue) {
value = Number(value)
return isNaN(value) ? defaultValue : value
}
export function upx2px (number, newDeviceWidth) { export function upx2px (number, newDeviceWidth) {
if (deviceWidth === 0) { if (deviceWidth === 0) {
checkDeviceWidth() checkDeviceWidth()
...@@ -25,7 +30,13 @@ export function upx2px (number, newDeviceWidth) { ...@@ -25,7 +30,13 @@ export function upx2px (number, newDeviceWidth) {
if (number === 0) { if (number === 0) {
return 0 return 0
} }
let result = (number / BASE_DEVICE_WIDTH) * (newDeviceWidth || deviceWidth) const config = __uniConfig.globalStyle || __uniConfig.window || {}
const maxWidth = checkValue(config.rpxCalcMaxDeviceWidth, 960)
const baseWidth = checkValue(config.rpxCalcBaseDeviceWidth, 375)
const includeWidth = checkValue(config.rpxCalcIncludeWidth, 750)
let width = newDeviceWidth || deviceWidth
width = number === includeWidth || width <= maxWidth ? width : baseWidth
let result = (number / BASE_DEVICE_WIDTH) * width
if (result < 0) { if (result < 0) {
result = -result result = -result
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册