From 72ad6aefa4f4e936d3b222df59cde5eeba186e95 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Mon, 18 Oct 2021 21:12:11 +0800 Subject: [PATCH] wip(mp): css --- packages/uni-api/src/helpers/api/promise.ts | 14 +-- .../uni-app-plus/dist/uni-app-service.es.js | 7 ++ packages/uni-cli-shared/src/json/manifest.ts | 23 +++- .../src/postcss/plugins/uniapp.ts | 2 +- packages/uni-h5-vite/src/plugins/ssr.ts | 5 +- packages/uni-h5-vite/src/utils/ssr.ts | 2 +- packages/uni-h5/dist/uni-h5.cjs.js | 5 - packages/uni-h5/dist/uni-h5.es.js | 5 - packages/uni-mp-alipay/dist/uni.api.esm.js | 14 +-- packages/uni-mp-baidu/dist/uni.api.esm.js | 14 +-- .../uni-mp-compiler/__tests__/class.spec.ts | 11 +- .../uni-mp-compiler/__tests__/style.spec.ts | 14 +++ .../uni-mp-compiler/__tests__/test.spec.ts | 6 +- .../src/transforms/transformClass.ts | 3 +- .../src/transforms/transformStyle.ts | 4 +- .../uni-mp-compiler/src/transforms/vIf.ts | 3 +- packages/uni-mp-kuaishou/dist/uni.api.esm.js | 14 +-- packages/uni-mp-qq/dist/uni.api.esm.js | 14 +-- packages/uni-mp-toutiao/dist/uni.api.esm.js | 14 +-- packages/uni-mp-vite/src/index.ts | 6 + packages/uni-mp-vite/src/plugin/index.ts | 3 + packages/uni-mp-vite/src/plugins/renderjs.ts | 33 ++++++ packages/uni-mp-vue/dist/vue.runtime.esm.js | 1 - packages/uni-mp-vue/lib/vue.runtime.esm.js | 1 - packages/uni-mp-weixin/dist/uni.api.esm.js | 14 +-- packages/uni-mp-weixin/dist/uni.compiler.js | 6 + packages/uni-mp-weixin/src/plugin/index.ts | 6 + .../uni-quickapp-webview/dist/uni.api.esm.js | 14 +-- packages/uni-shared/dist/uni-shared.cjs.js | 9 ++ packages/uni-shared/dist/uni-shared.d.ts | 6 + packages/uni-shared/dist/uni-shared.es.js | 10 +- packages/uni-shared/src/dom/style.ts | 9 ++ packages/vite-plugin-uni/src/utils/index.ts | 1 - packages/vite-plugin-uni/src/utils/postcss.ts | 110 ------------------ 34 files changed, 207 insertions(+), 196 deletions(-) create mode 100644 packages/uni-mp-vite/src/plugins/renderjs.ts delete mode 100644 packages/vite-plugin-uni/src/utils/postcss.ts diff --git a/packages/uni-api/src/helpers/api/promise.ts b/packages/uni-api/src/helpers/api/promise.ts index 35b7d8c63..368011aec 100644 --- a/packages/uni-api/src/helpers/api/promise.ts +++ b/packages/uni-api/src/helpers/api/promise.ts @@ -16,13 +16,13 @@ function hasCallback(args: unknown) { } export function handlePromise(promise: Promise) { - if (__UNI_FEATURE_PROMISE__) { - return promise - .then((data) => { - return [null, data] - }) - .catch((err) => [err]) - } + // if (__UNI_FEATURE_PROMISE__) { + // return promise + // .then((data) => { + // return [null, data] + // }) + // .catch((err) => [err]) + // } return promise } diff --git a/packages/uni-app-plus/dist/uni-app-service.es.js b/packages/uni-app-plus/dist/uni-app-service.es.js index 3dff615e2..f493a59e1 100644 --- a/packages/uni-app-plus/dist/uni-app-service.es.js +++ b/packages/uni-app-plus/dist/uni-app-service.es.js @@ -1064,6 +1064,13 @@ var serviceContext = (function (vue) { return false; } function handlePromise(promise) { + // if (false) { + // return promise + // .then((data) => { + // return [null, data] + // }) + // .catch((err) => [err]) + // } return promise; } function promisify(name, fn) { diff --git a/packages/uni-cli-shared/src/json/manifest.ts b/packages/uni-cli-shared/src/json/manifest.ts index ed2f19ff7..61d292f33 100644 --- a/packages/uni-cli-shared/src/json/manifest.ts +++ b/packages/uni-cli-shared/src/json/manifest.ts @@ -1,7 +1,11 @@ import fs from 'fs' import path from 'path' import { extend } from '@vue/shared' -import { once, defaultRpx2Unit } from '@dcloudio/uni-shared' +import { + once, + defaultRpx2Unit, + defaultMiniProgramRpx2Unit, +} from '@dcloudio/uni-shared' import { parseJson } from './json' @@ -13,10 +17,19 @@ export const parseManifestJson = (inputDir: string) => { export const parseManifestJsonOnce = once(parseManifestJson) -export const parseRpx2UnitOnce = once((inputDir: string) => { - const { h5 } = parseManifestJsonOnce(inputDir) - return extend({}, defaultRpx2Unit, (h5 && h5.rpx) || {}) -}) +export const parseRpx2UnitOnce = once( + (inputDir: string, platform: UniApp.PLATFORM) => { + const rpx2unit = + platform === 'h5' || platform === 'app' + ? defaultRpx2Unit + : defaultMiniProgramRpx2Unit + const platformOptions = parseManifestJsonOnce(inputDir)[platform] + if (platformOptions && platformOptions.rpx) { + return extend({}, rpx2unit, platformOptions) + } + return extend({}, rpx2unit) + } +) interface CompilerCompatConfig { MODE?: 2 | 3 diff --git a/packages/uni-cli-shared/src/postcss/plugins/uniapp.ts b/packages/uni-cli-shared/src/postcss/plugins/uniapp.ts index 7b041be99..4c737dce0 100644 --- a/packages/uni-cli-shared/src/postcss/plugins/uniapp.ts +++ b/packages/uni-cli-shared/src/postcss/plugins/uniapp.ts @@ -101,7 +101,7 @@ const uniapp = (opts?: UniAppCssProcessorOptions) => { return { OnceExit(root) { root.walkDecls(walkDecls(rpx2unit)) - root.walkRules(walkRules(page)) + page && root.walkRules(walkRules(page)) }, } }, diff --git a/packages/uni-h5-vite/src/plugins/ssr.ts b/packages/uni-h5-vite/src/plugins/ssr.ts index 65a31f0ee..9fc5206ad 100644 --- a/packages/uni-h5-vite/src/plugins/ssr.ts +++ b/packages/uni-h5-vite/src/plugins/ssr.ts @@ -55,7 +55,10 @@ export function uniSSRPlugin(): Plugin { chunk.code = generateSsrDefineCode( resolvedConfig, - parseRpx2UnitOnce(process.env.UNI_INPUT_DIR) + parseRpx2UnitOnce( + process.env.UNI_INPUT_DIR, + process.env.UNI_PLATFORM + ) ) + '\n' + chunk.code diff --git a/packages/uni-h5-vite/src/utils/ssr.ts b/packages/uni-h5-vite/src/utils/ssr.ts index 29948d2d0..d1c883d33 100644 --- a/packages/uni-h5-vite/src/utils/ssr.ts +++ b/packages/uni-h5-vite/src/utils/ssr.ts @@ -132,7 +132,7 @@ export function rewriteSsrNativeTag() { } export function rewriteSsrRenderStyle(inputDir: string) { - const { unit, unitRatio, unitPrecision } = parseRpx2UnitOnce(inputDir) + const { unit, unitRatio, unitPrecision } = parseRpx2UnitOnce(inputDir, 'h5') const rpx2unit = createRpx2Unit(unit, unitRatio, unitPrecision) const shared = require('@vue/shared') const oldStringifyStyle = shared.stringifyStyle diff --git a/packages/uni-h5/dist/uni-h5.cjs.js b/packages/uni-h5/dist/uni-h5.cjs.js index 7fb45b988..c0117c374 100644 --- a/packages/uni-h5/dist/uni-h5.cjs.js +++ b/packages/uni-h5/dist/uni-h5.cjs.js @@ -1243,11 +1243,6 @@ function hasCallback(args) { return false; } function handlePromise(promise) { - if (__UNI_FEATURE_PROMISE__) { - return promise.then((data) => { - return [null, data]; - }).catch((err) => [err]); - } return promise; } function promisify(name, fn) { diff --git a/packages/uni-h5/dist/uni-h5.es.js b/packages/uni-h5/dist/uni-h5.es.js index 7a6140206..bd67bcf9d 100644 --- a/packages/uni-h5/dist/uni-h5.es.js +++ b/packages/uni-h5/dist/uni-h5.es.js @@ -2522,11 +2522,6 @@ function hasCallback(args) { return false; } function handlePromise(promise) { - if (__UNI_FEATURE_PROMISE__) { - return promise.then((data) => { - return [null, data]; - }).catch((err) => [err]); - } return promise; } function promisify(name, fn) { diff --git a/packages/uni-mp-alipay/dist/uni.api.esm.js b/packages/uni-mp-alipay/dist/uni.api.esm.js index bf7bdb278..23eed943c 100644 --- a/packages/uni-mp-alipay/dist/uni.api.esm.js +++ b/packages/uni-mp-alipay/dist/uni.api.esm.js @@ -289,13 +289,13 @@ function invokeApi(method, api, options, ...params) { } function handlePromise(promise) { - if (__UNI_FEATURE_PROMISE__) { - return promise - .then((data) => { - return [null, data]; - }) - .catch((err) => [err]); - } + // if (__UNI_FEATURE_PROMISE__) { + // return promise + // .then((data) => { + // return [null, data] + // }) + // .catch((err) => [err]) + // } return promise; } diff --git a/packages/uni-mp-baidu/dist/uni.api.esm.js b/packages/uni-mp-baidu/dist/uni.api.esm.js index bdd618021..55723cbb2 100644 --- a/packages/uni-mp-baidu/dist/uni.api.esm.js +++ b/packages/uni-mp-baidu/dist/uni.api.esm.js @@ -289,13 +289,13 @@ function invokeApi(method, api, options, ...params) { } function handlePromise(promise) { - if (__UNI_FEATURE_PROMISE__) { - return promise - .then((data) => { - return [null, data]; - }) - .catch((err) => [err]); - } + // if (__UNI_FEATURE_PROMISE__) { + // return promise + // .then((data) => { + // return [null, data] + // }) + // .catch((err) => [err]) + // } return promise; } diff --git a/packages/uni-mp-compiler/__tests__/class.spec.ts b/packages/uni-mp-compiler/__tests__/class.spec.ts index 3dbbda2e6..9bcf018ab 100644 --- a/packages/uni-mp-compiler/__tests__/class.spec.ts +++ b/packages/uni-mp-compiler/__tests__/class.spec.ts @@ -58,10 +58,10 @@ describe('compiler: transform class', () => { }) test('v-bind:class object syntax', () => { assert( - ``, + ``, ``, `(_ctx, _cache) => { - return { a: _ctx.isRed } + return { a: \`\${_ctx.isRed}\` } }` ) assert( @@ -103,6 +103,13 @@ describe('compiler: transform class', () => { ) }) test('v-bind:class array syntax', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _normalizeClass(_ctx.classA), b: _normalizeClass(\`\${_ctx.classB}\`) } +}` + ) assert( ``, ``, diff --git a/packages/uni-mp-compiler/__tests__/style.spec.ts b/packages/uni-mp-compiler/__tests__/style.spec.ts index b5e7d0592..9e6dd275d 100644 --- a/packages/uni-mp-compiler/__tests__/style.spec.ts +++ b/packages/uni-mp-compiler/__tests__/style.spec.ts @@ -50,6 +50,13 @@ describe('compiler: transform style', () => { ) }) test('v-bind:style object syntax', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: \`\${_ctx.green}px\` } +}` + ) assert( ``, ``, @@ -98,6 +105,13 @@ describe('compiler: transform style', () => { ) }) test('v-bind:style array syntax', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _normalizeStyle(_ctx.styleA), b: _normalizeStyle(\`\${_ctx.styleB}\`) } +}` + ) assert( ``, ``, diff --git a/packages/uni-mp-compiler/__tests__/test.spec.ts b/packages/uni-mp-compiler/__tests__/test.spec.ts index d3faa1a44..26141157f 100644 --- a/packages/uni-mp-compiler/__tests__/test.spec.ts +++ b/packages/uni-mp-compiler/__tests__/test.spec.ts @@ -34,10 +34,10 @@ function assert( describe('compiler', () => { test('scope', () => { assert( - ``, - ``, + ``, + ``, `(_ctx, _cache) => { - return { a: _vFor(_ctx.weeks, (item, weekIndex) => { return { a: _vFor(item, (weeks, weeksIndex) => { return { a: weeksIndex, b: weeks.id }; }), b: weekIndex, c: item.id }; }) } + return { a: \`\${_ctx.green}px\` } }` ) }) diff --git a/packages/uni-mp-compiler/src/transforms/transformClass.ts b/packages/uni-mp-compiler/src/transforms/transformClass.ts index f02e2750f..2788cfbbc 100644 --- a/packages/uni-mp-compiler/src/transforms/transformClass.ts +++ b/packages/uni-mp-compiler/src/transforms/transformClass.ts @@ -18,6 +18,7 @@ import { LogicalExpression, logicalExpression, StringLiteral, + isTemplateLiteral, } from '@babel/types' import { DirectiveNode, @@ -156,7 +157,7 @@ function rewriteClassObjectExpression( key as Expression ) as Identifier } - if (isLiteral(value)) { + if (isLiteral(value) && !isTemplateLiteral(value)) { return } else { const newExpr = parseExprWithRewrite( diff --git a/packages/uni-mp-compiler/src/transforms/transformStyle.ts b/packages/uni-mp-compiler/src/transforms/transformStyle.ts index acc9c1ac5..e6e5b168e 100644 --- a/packages/uni-mp-compiler/src/transforms/transformStyle.ts +++ b/packages/uni-mp-compiler/src/transforms/transformStyle.ts @@ -13,6 +13,7 @@ import { isObjectProperty, binaryExpression, isIdentifier, + isTemplateLiteral, } from '@babel/types' import { DirectiveNode, @@ -159,7 +160,8 @@ function rewriteStyleObjectExpression( prop.key = parseStringLiteral(prop.key) prop.key.value = hyphenate(prop.key.value) + ':' } - if (isLiteral(value)) { + // {fontSize:`${fontSize}px`} => {'font-size':a} + if (isLiteral(value) && !isTemplateLiteral(value)) { return } else { const newExpr = parseExprWithRewrite( diff --git a/packages/uni-mp-compiler/src/transforms/vIf.ts b/packages/uni-mp-compiler/src/transforms/vIf.ts index f87919807..f92ae016b 100644 --- a/packages/uni-mp-compiler/src/transforms/vIf.ts +++ b/packages/uni-mp-compiler/src/transforms/vIf.ts @@ -2,6 +2,7 @@ import { ConditionalExpression, isConditionalExpression, isLiteral, + isTemplateLiteral, isSpreadElement, ObjectExpression, } from '@babel/types' @@ -55,7 +56,7 @@ export const transformIf = createStructuralDirectiveTransform( condition, }) if (condition) { - if (!isLiteral(condition)) { + if (!isLiteral(condition) || isTemplateLiteral(condition)) { ifOptions.condition = rewriteExpression( dir.exp!, context, diff --git a/packages/uni-mp-kuaishou/dist/uni.api.esm.js b/packages/uni-mp-kuaishou/dist/uni.api.esm.js index f8df3161a..d668af2a3 100644 --- a/packages/uni-mp-kuaishou/dist/uni.api.esm.js +++ b/packages/uni-mp-kuaishou/dist/uni.api.esm.js @@ -289,13 +289,13 @@ function invokeApi(method, api, options, ...params) { } function handlePromise(promise) { - if (__UNI_FEATURE_PROMISE__) { - return promise - .then((data) => { - return [null, data]; - }) - .catch((err) => [err]); - } + // if (__UNI_FEATURE_PROMISE__) { + // return promise + // .then((data) => { + // return [null, data] + // }) + // .catch((err) => [err]) + // } return promise; } diff --git a/packages/uni-mp-qq/dist/uni.api.esm.js b/packages/uni-mp-qq/dist/uni.api.esm.js index e1f7dbdd6..6f554439b 100644 --- a/packages/uni-mp-qq/dist/uni.api.esm.js +++ b/packages/uni-mp-qq/dist/uni.api.esm.js @@ -289,13 +289,13 @@ function invokeApi(method, api, options, ...params) { } function handlePromise(promise) { - if (__UNI_FEATURE_PROMISE__) { - return promise - .then((data) => { - return [null, data]; - }) - .catch((err) => [err]); - } + // if (__UNI_FEATURE_PROMISE__) { + // return promise + // .then((data) => { + // return [null, data] + // }) + // .catch((err) => [err]) + // } return promise; } diff --git a/packages/uni-mp-toutiao/dist/uni.api.esm.js b/packages/uni-mp-toutiao/dist/uni.api.esm.js index 6b43481a9..279da2ef1 100644 --- a/packages/uni-mp-toutiao/dist/uni.api.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.api.esm.js @@ -289,13 +289,13 @@ function invokeApi(method, api, options, ...params) { } function handlePromise(promise) { - if (__UNI_FEATURE_PROMISE__) { - return promise - .then((data) => { - return [null, data]; - }) - .catch((err) => [err]); - } + // if (__UNI_FEATURE_PROMISE__) { + // return promise + // .then((data) => { + // return [null, data] + // }) + // .catch((err) => [err]) + // } return promise; } diff --git a/packages/uni-mp-vite/src/index.ts b/packages/uni-mp-vite/src/index.ts index fc72aa8eb..f152283ca 100644 --- a/packages/uni-mp-vite/src/index.ts +++ b/packages/uni-mp-vite/src/index.ts @@ -1,3 +1,5 @@ +import { extend } from '@vue/shared' +import { initProvide, uniViteInjectPlugin } from '@dcloudio/uni-cli-shared' import { uniMiniProgramPlugin, UniMiniProgramPluginOptions } from './plugin' import { uniUsingComponentsPlugin } from './plugins/usingComponents' import { uniMainJsPlugin } from './plugins/mainJs' @@ -5,6 +7,8 @@ import { uniManifestJsonPlugin } from './plugins/manifestJson' import { uniPagesJsonPlugin } from './plugins/pagesJson' import { uniEntryPlugin } from './plugins/entry' import { SFCScriptCompileOptions } from '@vue/compiler-sfc' +import { uniRenderjsPlugin } from './plugins/renderjs' + export { UniMiniProgramPluginOptions } from './plugin' export default (options: UniMiniProgramPluginOptions) => { return [ @@ -12,6 +16,8 @@ export default (options: UniMiniProgramPluginOptions) => { uniManifestJsonPlugin(options), uniPagesJsonPlugin(options), uniEntryPlugin(options), + uniViteInjectPlugin(extend({}, options.vite.inject, initProvide())), + uniRenderjsPlugin(), uniMiniProgramPlugin(options), (options: { vueOptions?: { script?: Partial } diff --git a/packages/uni-mp-vite/src/plugin/index.ts b/packages/uni-mp-vite/src/plugin/index.ts index f8843e63a..ce43d5431 100644 --- a/packages/uni-mp-vite/src/plugin/index.ts +++ b/packages/uni-mp-vite/src/plugin/index.ts @@ -20,6 +20,9 @@ export interface UniMiniProgramPluginOptions { vite: { alias: AliasOptions copyOptions: CopyOptions + inject: { + [name: string]: [string, string] + } } global: string app: { diff --git a/packages/uni-mp-vite/src/plugins/renderjs.ts b/packages/uni-mp-vite/src/plugins/renderjs.ts new file mode 100644 index 000000000..4198355bf --- /dev/null +++ b/packages/uni-mp-vite/src/plugins/renderjs.ts @@ -0,0 +1,33 @@ +import debug from 'debug' +import { Plugin } from 'vite' + +import { missingModuleName, parseRenderjs } from '@dcloudio/uni-cli-shared' + +const debugRenderjs = debug('vite:uni:renderjs') + +export function uniRenderjsPlugin(): Plugin { + return { + name: 'vite:uni-mp-renderjs', + transform(code, id) { + const { type, name } = parseRenderjs(id) + if (!type) { + return + } + debugRenderjs(id) + if (!name) { + this.error(missingModuleName(type, code)) + } + if (type === 'wxs') { + console.log('wxs', id, code) + // this.emitFile({ + // type: 'asset', + // fileName: '', + // source: code, + // }) + } + return { + code: 'export default {}', + } + }, + } +} diff --git a/packages/uni-mp-vue/dist/vue.runtime.esm.js b/packages/uni-mp-vue/dist/vue.runtime.esm.js index 124d0814f..519373dd4 100644 --- a/packages/uni-mp-vue/dist/vue.runtime.esm.js +++ b/packages/uni-mp-vue/dist/vue.runtime.esm.js @@ -4684,7 +4684,6 @@ function setupRenderEffect(instance) { patch(instance, renderComponentRoot(instance)); } else { - instance.render && instance.render.call(instance.proxy); // updateComponent const { bu, u } = instance; effect.allowRecurse = false; diff --git a/packages/uni-mp-vue/lib/vue.runtime.esm.js b/packages/uni-mp-vue/lib/vue.runtime.esm.js index 119387ba6..769d07a99 100644 --- a/packages/uni-mp-vue/lib/vue.runtime.esm.js +++ b/packages/uni-mp-vue/lib/vue.runtime.esm.js @@ -4616,7 +4616,6 @@ function setupRenderEffect(instance) { patch(instance, renderComponentRoot(instance)); } else { - instance.render && instance.render.call(instance.proxy); // updateComponent const { bu, u } = instance; effect.allowRecurse = false; diff --git a/packages/uni-mp-weixin/dist/uni.api.esm.js b/packages/uni-mp-weixin/dist/uni.api.esm.js index 4e420c580..ad6057f56 100644 --- a/packages/uni-mp-weixin/dist/uni.api.esm.js +++ b/packages/uni-mp-weixin/dist/uni.api.esm.js @@ -253,13 +253,13 @@ function invokeApi(method, api, options, ...params) { } function handlePromise(promise) { - if (__UNI_FEATURE_PROMISE__) { - return promise - .then((data) => { - return [null, data]; - }) - .catch((err) => [err]); - } + // if (__UNI_FEATURE_PROMISE__) { + // return promise + // .then((data) => { + // return [null, data] + // }) + // .catch((err) => [err]) + // } return promise; } diff --git a/packages/uni-mp-weixin/dist/uni.compiler.js b/packages/uni-mp-weixin/dist/uni.compiler.js index 69348b24e..40d34fea1 100644 --- a/packages/uni-mp-weixin/dist/uni.compiler.js +++ b/packages/uni-mp-weixin/dist/uni.compiler.js @@ -69,6 +69,12 @@ const uniMiniProgramWeixinPlugin = { const projectConfigFilename = 'project.config.json'; const options = { vite: { + inject: { + uni: [ + uniCliShared.resolveBuiltIn('@dcloudio/uni-mp-weixin/dist/uni.api.esm.js'), + 'default', + ], + }, alias: { 'uni-mp-runtime': uniCliShared.resolveBuiltIn('@dcloudio/uni-mp-weixin/dist/uni.mp.esm.js'), }, diff --git a/packages/uni-mp-weixin/src/plugin/index.ts b/packages/uni-mp-weixin/src/plugin/index.ts index a628c1a6c..c3f0b2b79 100644 --- a/packages/uni-mp-weixin/src/plugin/index.ts +++ b/packages/uni-mp-weixin/src/plugin/index.ts @@ -21,6 +21,12 @@ const projectConfigFilename = 'project.config.json' const options: UniMiniProgramPluginOptions = { vite: { + inject: { + uni: [ + resolveBuiltIn('@dcloudio/uni-mp-weixin/dist/uni.api.esm.js'), + 'default', + ], + }, alias: { 'uni-mp-runtime': resolveBuiltIn( '@dcloudio/uni-mp-weixin/dist/uni.mp.esm.js' diff --git a/packages/uni-quickapp-webview/dist/uni.api.esm.js b/packages/uni-quickapp-webview/dist/uni.api.esm.js index d35e435b2..0fc107a86 100644 --- a/packages/uni-quickapp-webview/dist/uni.api.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.api.esm.js @@ -289,13 +289,13 @@ function invokeApi(method, api, options, ...params) { } function handlePromise(promise) { - if (__UNI_FEATURE_PROMISE__) { - return promise - .then((data) => { - return [null, data]; - }) - .catch((err) => [err]); - } + // if (__UNI_FEATURE_PROMISE__) { + // return promise + // .then((data) => { + // return [null, data] + // }) + // .catch((err) => [err]) + // } return promise; } diff --git a/packages/uni-shared/dist/uni-shared.cjs.js b/packages/uni-shared/dist/uni-shared.cjs.js index 88b2735ba..c6e80fba0 100644 --- a/packages/uni-shared/dist/uni-shared.cjs.js +++ b/packages/uni-shared/dist/uni-shared.cjs.js @@ -168,12 +168,20 @@ const defaultRpx2Unit = { unitRatio: 10 / 320, unitPrecision: 5, }; +const defaultMiniProgramRpx2Unit = { + unit: 'rpx', + unitRatio: 1, + unitPrecision: 1, +}; function createRpx2Unit(unit, unitRatio, unitPrecision) { // ignore: rpxCalcIncludeWidth return (val) => val.replace(unitRE, (m, $1) => { if (!$1) { return m; } + if (unitRatio === 1) { + return `${$1}${unit}`; + } const value = toFixed(parseFloat($1) * unitRatio, unitPrecision); return value === 0 ? '0' : `${value}${unit}`; }); @@ -1212,6 +1220,7 @@ exports.createUniEvent = createUniEvent; exports.debounce = debounce; exports.decode = decode; exports.decodedQuery = decodedQuery; +exports.defaultMiniProgramRpx2Unit = defaultMiniProgramRpx2Unit; exports.defaultRpx2Unit = defaultRpx2Unit; exports.formatAppLog = formatAppLog; exports.formatDateTime = formatDateTime; diff --git a/packages/uni-shared/dist/uni-shared.d.ts b/packages/uni-shared/dist/uni-shared.d.ts index 21eb9b73b..5e3d88811 100644 --- a/packages/uni-shared/dist/uni-shared.d.ts +++ b/packages/uni-shared/dist/uni-shared.d.ts @@ -132,6 +132,12 @@ export declare function decode(text: string | number): string; export declare function decodedQuery(query?: Record): Record; +export declare const defaultMiniProgramRpx2Unit: { + unit: string; + unitRatio: number; + unitPrecision: number; +}; + export declare const defaultRpx2Unit: { unit: string; unitRatio: number; diff --git a/packages/uni-shared/dist/uni-shared.es.js b/packages/uni-shared/dist/uni-shared.es.js index 99670f53a..b7f066360 100644 --- a/packages/uni-shared/dist/uni-shared.es.js +++ b/packages/uni-shared/dist/uni-shared.es.js @@ -164,12 +164,20 @@ const defaultRpx2Unit = { unitRatio: 10 / 320, unitPrecision: 5, }; +const defaultMiniProgramRpx2Unit = { + unit: 'rpx', + unitRatio: 1, + unitPrecision: 1, +}; function createRpx2Unit(unit, unitRatio, unitPrecision) { // ignore: rpxCalcIncludeWidth return (val) => val.replace(unitRE, (m, $1) => { if (!$1) { return m; } + if (unitRatio === 1) { + return `${$1}${unit}`; + } const value = toFixed(parseFloat($1) * unitRatio, unitPrecision); return value === 0 ? '0' : `${value}${unit}`; }); @@ -1107,4 +1115,4 @@ function getEnvLocale() { return (lang && lang.replace(/[.:].*/, '')) || 'en'; } -export { ACTION_TYPE_ADD_EVENT, ACTION_TYPE_ADD_WXS_EVENT, ACTION_TYPE_CREATE, ACTION_TYPE_EVENT, ACTION_TYPE_INSERT, ACTION_TYPE_PAGE_CREATE, ACTION_TYPE_PAGE_CREATED, ACTION_TYPE_PAGE_SCROLL, ACTION_TYPE_REMOVE, ACTION_TYPE_REMOVE_ATTRIBUTE, ACTION_TYPE_REMOVE_EVENT, ACTION_TYPE_SET_ATTRIBUTE, ACTION_TYPE_SET_TEXT, ATTR_CHANGE_PREFIX, ATTR_CLASS, ATTR_INNER_HTML, ATTR_STYLE, ATTR_TEXT_CONTENT, ATTR_V_OWNER_ID, ATTR_V_RENDERJS, ATTR_V_SHOW, BACKGROUND_COLOR, BUILT_IN_TAGS, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, DATA_RE, EventChannel, EventModifierFlags, I18N_JSON_DELIMITERS, JSON_PROTOCOL, LINEFEED, NAVBAR_HEIGHT, NODE_TYPE_COMMENT, NODE_TYPE_ELEMENT, NODE_TYPE_PAGE, NODE_TYPE_TEXT, ON_ADD_TO_FAVORITES, ON_APP_ENTER_BACKGROUND, ON_APP_ENTER_FOREGROUND, ON_BACK_PRESS, ON_ERROR, ON_HIDE, ON_KEYBOARD_HEIGHT_CHANGE, ON_LAUNCH, ON_LOAD, ON_NAVIGATION_BAR_BUTTON_TAP, ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_CLICKED, ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED, ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED, ON_PAGE_NOT_FOUND, ON_PAGE_SCROLL, ON_PULL_DOWN_REFRESH, ON_REACH_BOTTOM, ON_REACH_BOTTOM_DISTANCE, ON_READY, ON_RESIZE, ON_SHARE_APP_MESSAGE, ON_SHARE_TIMELINE, ON_SHOW, ON_TAB_ITEM_TAP, ON_THEME_CHANGE, ON_UNHANDLE_REJECTION, ON_UNLOAD, ON_WEB_INVOKE_APP_SERVICE, ON_WXS_INVOKE_CALL_METHOD, PLUS_RE, PRIMARY_COLOR, RENDERJS_MODULES, RESPONSIVE_MIN_WIDTH, SCHEME_RE, SELECTED_COLOR, TABBAR_HEIGHT, TAGS, UNI_SSR, UNI_SSR_DATA, UNI_SSR_GLOBAL_DATA, UNI_SSR_STORE, UNI_SSR_TITLE, UniBaseNode, UniCommentNode, UniElement, UniEvent, UniInputElement, UniLifecycleHooks, UniNode, UniTextAreaElement, UniTextNode, WEB_INVOKE_APPSERVICE, WXS_MODULES, WXS_PROTOCOL, addFont, cache, cacheStringFunction, callOptions, createRpx2Unit, createUniEvent, debounce, decode, decodedQuery, defaultRpx2Unit, formatAppLog, formatDateTime, formatLog, getCustomDataset, getEnvLocale, getLen, getValueByDataPath, initCustomDataset, invokeArrayFns, isBuiltInComponent, isCustomElement, isNativeTag, isRootHook, isServiceCustomElement, isServiceNativeTag, normalizeDataset, normalizeEventType, normalizeTarget, once, parseEventName, parseQuery, parseUrl, passive, plusReady, removeLeadingSlash, resolveOwnerEl, resolveOwnerVm, sanitise, scrollTo, stringifyQuery, updateElementStyle }; +export { ACTION_TYPE_ADD_EVENT, ACTION_TYPE_ADD_WXS_EVENT, ACTION_TYPE_CREATE, ACTION_TYPE_EVENT, ACTION_TYPE_INSERT, ACTION_TYPE_PAGE_CREATE, ACTION_TYPE_PAGE_CREATED, ACTION_TYPE_PAGE_SCROLL, ACTION_TYPE_REMOVE, ACTION_TYPE_REMOVE_ATTRIBUTE, ACTION_TYPE_REMOVE_EVENT, ACTION_TYPE_SET_ATTRIBUTE, ACTION_TYPE_SET_TEXT, ATTR_CHANGE_PREFIX, ATTR_CLASS, ATTR_INNER_HTML, ATTR_STYLE, ATTR_TEXT_CONTENT, ATTR_V_OWNER_ID, ATTR_V_RENDERJS, ATTR_V_SHOW, BACKGROUND_COLOR, BUILT_IN_TAGS, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, DATA_RE, EventChannel, EventModifierFlags, I18N_JSON_DELIMITERS, JSON_PROTOCOL, LINEFEED, NAVBAR_HEIGHT, NODE_TYPE_COMMENT, NODE_TYPE_ELEMENT, NODE_TYPE_PAGE, NODE_TYPE_TEXT, ON_ADD_TO_FAVORITES, ON_APP_ENTER_BACKGROUND, ON_APP_ENTER_FOREGROUND, ON_BACK_PRESS, ON_ERROR, ON_HIDE, ON_KEYBOARD_HEIGHT_CHANGE, ON_LAUNCH, ON_LOAD, ON_NAVIGATION_BAR_BUTTON_TAP, ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_CLICKED, ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED, ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED, ON_PAGE_NOT_FOUND, ON_PAGE_SCROLL, ON_PULL_DOWN_REFRESH, ON_REACH_BOTTOM, ON_REACH_BOTTOM_DISTANCE, ON_READY, ON_RESIZE, ON_SHARE_APP_MESSAGE, ON_SHARE_TIMELINE, ON_SHOW, ON_TAB_ITEM_TAP, ON_THEME_CHANGE, ON_UNHANDLE_REJECTION, ON_UNLOAD, ON_WEB_INVOKE_APP_SERVICE, ON_WXS_INVOKE_CALL_METHOD, PLUS_RE, PRIMARY_COLOR, RENDERJS_MODULES, RESPONSIVE_MIN_WIDTH, SCHEME_RE, SELECTED_COLOR, TABBAR_HEIGHT, TAGS, UNI_SSR, UNI_SSR_DATA, UNI_SSR_GLOBAL_DATA, UNI_SSR_STORE, UNI_SSR_TITLE, UniBaseNode, UniCommentNode, UniElement, UniEvent, UniInputElement, UniLifecycleHooks, UniNode, UniTextAreaElement, UniTextNode, WEB_INVOKE_APPSERVICE, WXS_MODULES, WXS_PROTOCOL, addFont, cache, cacheStringFunction, callOptions, createRpx2Unit, createUniEvent, debounce, decode, decodedQuery, defaultMiniProgramRpx2Unit, defaultRpx2Unit, formatAppLog, formatDateTime, formatLog, getCustomDataset, getEnvLocale, getLen, getValueByDataPath, initCustomDataset, invokeArrayFns, isBuiltInComponent, isCustomElement, isNativeTag, isRootHook, isServiceCustomElement, isServiceNativeTag, normalizeDataset, normalizeEventType, normalizeTarget, once, parseEventName, parseQuery, parseUrl, passive, plusReady, removeLeadingSlash, resolveOwnerEl, resolveOwnerVm, sanitise, scrollTo, stringifyQuery, updateElementStyle }; diff --git a/packages/uni-shared/src/dom/style.ts b/packages/uni-shared/src/dom/style.ts index e223c09fc..07607070c 100644 --- a/packages/uni-shared/src/dom/style.ts +++ b/packages/uni-shared/src/dom/style.ts @@ -15,6 +15,12 @@ export const defaultRpx2Unit = { unitPrecision: 5, } +export const defaultMiniProgramRpx2Unit = { + unit: 'rpx', + unitRatio: 1, + unitPrecision: 1, +} + export type Rpx2UnitOptions = typeof defaultRpx2Unit export function createRpx2Unit( @@ -28,6 +34,9 @@ export function createRpx2Unit( if (!$1) { return m } + if (unitRatio === 1) { + return `${$1}${unit}` + } const value = toFixed(parseFloat($1) * unitRatio, unitPrecision) return value === 0 ? '0' : `${value}${unit}` }) diff --git a/packages/vite-plugin-uni/src/utils/index.ts b/packages/vite-plugin-uni/src/utils/index.ts index 4393f3a07..13e3a5cdc 100644 --- a/packages/vite-plugin-uni/src/utils/index.ts +++ b/packages/vite-plugin-uni/src/utils/index.ts @@ -1,4 +1,3 @@ export * from './ast' export * from './filter' export * from './plugin' -export * from './postcss' diff --git a/packages/vite-plugin-uni/src/utils/postcss.ts b/packages/vite-plugin-uni/src/utils/postcss.ts deleted file mode 100644 index a04f516b4..000000000 --- a/packages/vite-plugin-uni/src/utils/postcss.ts +++ /dev/null @@ -1,110 +0,0 @@ -import { extend } from '@vue/shared' -import { rule, Rule, Declaration, Plugin } from 'postcss' -import selectorParser from 'postcss-selector-parser' -import { - createRpx2Unit, - defaultRpx2Unit, - isBuiltInComponent, - COMPONENT_SELECTOR_PREFIX, -} from '@dcloudio/uni-shared' - -interface UniAppCssProcessorOptions { - page?: string - unit?: string // 目标单位,默认rem - unitRatio?: number // 单位转换比例,默认10/320 - unitPrecision?: number // 单位精度,默认5 -} - -const defaultUniAppCssProcessorOptions = extend( - { - page: 'body', - }, - defaultRpx2Unit -) - -const BG_PROPS = [ - 'background', - 'background-clip', - 'background-color', - 'background-image', - 'background-origin', - 'background-position', - 'background-repeat', - 'background-size', - 'background-attachment', -] - -function transform( - selector: selectorParser.Node, - page: string, - state: { bg: boolean } -) { - if (selector.type !== 'tag') { - return - } - const { value } = selector - if (isBuiltInComponent(value)) { - selector.value = COMPONENT_SELECTOR_PREFIX + value - } else if (value === 'page') { - if (!page) { - return - } - selector.value = page - if (page !== 'body') { - state.bg = true - } - } -} - -function createBodyBackgroundRule(origRule: Rule) { - const bgDecls: Declaration[] = [] - origRule.walkDecls((decl) => { - if (BG_PROPS.indexOf(decl.prop) !== -1) { - bgDecls.push(decl.clone()) - } - }) - if (bgDecls.length) { - origRule.after(rule({ selector: 'body' }).append(bgDecls)) - } -} - -function walkRules(page: string) { - return (rule: Rule) => { - const state = { bg: false } - rule.selector = selectorParser((selectors) => - selectors.walk((selector) => transform(selector, page, state)) - ).processSync(rule.selector) - state.bg && createBodyBackgroundRule(rule) - } -} - -function walkDecls(rpx2unit: ReturnType) { - return (decl: Declaration) => { - const { value } = decl - if (value.indexOf('rpx') === -1 && value.indexOf('upx') === -1) { - return - } - decl.value = rpx2unit(decl.value) - } -} - -export const uniapp = (opts?: UniAppCssProcessorOptions) => { - const { page, unit, unitRatio, unitPrecision } = extend( - {}, - defaultUniAppCssProcessorOptions, - opts || {} - ) - const rpx2unit = createRpx2Unit(unit, unitRatio, unitPrecision) - return { - postcssPlugin: 'uni-app', - prepare() { - return { - OnceExit(root) { - root.walkDecls(walkDecls(rpx2unit)) - root.walkRules(walkRules(page)) - }, - } - }, - } as Plugin -} -uniapp.postcss = true -- GitLab