From 89c7fe9d186d1354f7c6a37ec9791463845e77f0 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Mon, 20 Dec 2021 20:27:50 +0800 Subject: [PATCH] fix: wxs (question/136633) --- .../dist/service.runtime.esm.dev.js | 2 +- .../dist/service.runtime.esm.prod.js | 2 +- packages/uni-h5/dist/uni-h5.es.js | 4 ++-- packages/uni-shared/dist/uni-shared.cjs.js | 2 +- packages/uni-shared/dist/uni-shared.es.js | 2 +- packages/uni-shared/src/vue.ts | 4 ++-- packages/vite-plugin-uni/src/cli/build.ts | 17 ++++++++++++-- packages/vite-plugin-uni/src/cli/nvue.ts | 6 ----- packages/vite-plugin-uni/src/index.ts | 8 ++++++- packages/vite-plugin-uni/src/utils/index.ts | 1 + .../vite-plugin-uni/src/utils/polyfill.ts | 22 +++++++++++++++++++ 11 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 packages/vite-plugin-uni/src/utils/polyfill.ts diff --git a/packages/uni-app-vue/dist/service.runtime.esm.dev.js b/packages/uni-app-vue/dist/service.runtime.esm.dev.js index 540573e7eb..0f73a9f225 100644 --- a/packages/uni-app-vue/dist/service.runtime.esm.dev.js +++ b/packages/uni-app-vue/dist/service.runtime.esm.dev.js @@ -79,7 +79,7 @@ export default function vueFactory(exports) { } = instance; // ShapeFlags.ARRAY_CHILDREN = 1<<4 if (subTree.shapeFlag & 16) { - var elemVNode = subTree.children.find(vnode => isElement(vnode.el)); + var elemVNode = subTree.children.find(vnode => vnode.el && isElement(vnode.el)); if (elemVNode) { return elemVNode.el; diff --git a/packages/uni-app-vue/dist/service.runtime.esm.prod.js b/packages/uni-app-vue/dist/service.runtime.esm.prod.js index cec3e0f0b4..97e01803bc 100644 --- a/packages/uni-app-vue/dist/service.runtime.esm.prod.js +++ b/packages/uni-app-vue/dist/service.runtime.esm.prod.js @@ -77,7 +77,7 @@ export default function vueFactory(exports) { } = instance; // ShapeFlags.ARRAY_CHILDREN = 1<<4 if (subTree.shapeFlag & 16) { - var elemVNode = subTree.children.find(vnode => isElement(vnode.el)); + var elemVNode = subTree.children.find(vnode => vnode.el && isElement(vnode.el)); if (elemVNode) { return elemVNode.el; diff --git a/packages/uni-h5/dist/uni-h5.es.js b/packages/uni-h5/dist/uni-h5.es.js index 6fced783fd..365b20a888 100644 --- a/packages/uni-h5/dist/uni-h5.es.js +++ b/packages/uni-h5/dist/uni-h5.es.js @@ -4626,11 +4626,11 @@ const ChooseLocationProtocol = { longitude: Number }; const API_GET_LOCATION = "getLocation"; -const coordTypes = ["WGS84", "GCJ02"]; +const coordTypes = ["wgs84", "gcj02"]; const GetLocationOptions = { formatArgs: { type(value, params) { - value = (value || "").toUpperCase(); + value = (value || "").toLowerCase(); if (coordTypes.indexOf(value) === -1) { params.type = coordTypes[0]; } else { diff --git a/packages/uni-shared/dist/uni-shared.cjs.js b/packages/uni-shared/dist/uni-shared.cjs.js index 10d887cb6d..4c9b78f175 100644 --- a/packages/uni-shared/dist/uni-shared.cjs.js +++ b/packages/uni-shared/dist/uni-shared.cjs.js @@ -184,7 +184,7 @@ function resolveOwnerEl(instance) { const { subTree } = instance; // ShapeFlags.ARRAY_CHILDREN = 1<<4 if (subTree.shapeFlag & 16) { - const elemVNode = subTree.children.find((vnode) => isElement(vnode.el)); + const elemVNode = subTree.children.find((vnode) => vnode.el && isElement(vnode.el)); if (elemVNode) { return elemVNode.el; } diff --git a/packages/uni-shared/dist/uni-shared.es.js b/packages/uni-shared/dist/uni-shared.es.js index e177e32ba3..4bdddc3dc7 100644 --- a/packages/uni-shared/dist/uni-shared.es.js +++ b/packages/uni-shared/dist/uni-shared.es.js @@ -180,7 +180,7 @@ function resolveOwnerEl(instance) { const { subTree } = instance; // ShapeFlags.ARRAY_CHILDREN = 1<<4 if (subTree.shapeFlag & 16) { - const elemVNode = subTree.children.find((vnode) => isElement(vnode.el)); + const elemVNode = subTree.children.find((vnode) => vnode.el && isElement(vnode.el)); if (elemVNode) { return elemVNode.el; } diff --git a/packages/uni-shared/src/vue.ts b/packages/uni-shared/src/vue.ts index ab83844bbc..6d5f9efe93 100644 --- a/packages/uni-shared/src/vue.ts +++ b/packages/uni-shared/src/vue.ts @@ -30,8 +30,8 @@ export function resolveOwnerEl(instance: ComponentInternalInstance) { const { subTree } = instance // ShapeFlags.ARRAY_CHILDREN = 1<<4 if (subTree.shapeFlag & 16) { - const elemVNode = (subTree.children as VNode[]).find((vnode) => - isElement(vnode.el as Element) + const elemVNode = (subTree.children as VNode[]).find( + (vnode) => vnode.el && isElement(vnode.el as Element) ) if (elemVNode) { return elemVNode.el diff --git a/packages/vite-plugin-uni/src/cli/build.ts b/packages/vite-plugin-uni/src/cli/build.ts index 962f8ec360..518e09139d 100644 --- a/packages/vite-plugin-uni/src/cli/build.ts +++ b/packages/vite-plugin-uni/src/cli/build.ts @@ -1,6 +1,7 @@ import path from 'path' import fs from 'fs-extra' import { build as buildByVite, BuildOptions, InlineConfig } from 'vite' +import { extend } from '@vue/shared' import { initPreContext, normalizeAppManifestJson, @@ -11,8 +12,20 @@ import { CliOptions } from '.' import { addConfigFile, cleanOptions } from './utils' export async function build(options: CliOptions) { - if (options.platform === 'app' && (options as BuildOptions).manifest) { - return buildManifestJson() + if (options.platform === 'app') { + if ((options as BuildOptions).manifest) { + return buildManifestJson() + } + if (process.env.UNI_RENDERER === 'native') { + return buildByVite( + addConfigFile( + extend( + { nvue: true }, + initBuildOptions(options, cleanOptions(options) as BuildOptions) + ) + ) + ) + } } return buildByVite( addConfigFile( diff --git a/packages/vite-plugin-uni/src/cli/nvue.ts b/packages/vite-plugin-uni/src/cli/nvue.ts index fd765d1eb7..974ee6c176 100644 --- a/packages/vite-plugin-uni/src/cli/nvue.ts +++ b/packages/vite-plugin-uni/src/cli/nvue.ts @@ -10,12 +10,6 @@ export function initNVueEnv() { const manifestJson = parseManifestJsonOnce(process.env.UNI_INPUT_DIR) if (getAppRenderer(manifestJson) === 'native') { process.env.UNI_RENDERER = 'native' - return ( - console.error( - `当前项目启用了纯原生渲染,请在manifest.json中切换vue版本为2之后,再重新运行。` - ), - process.exit(1) - ) } const nvueCompiler = getNVueCompiler(manifestJson) if (nvueCompiler === 'uni-app') { diff --git a/packages/vite-plugin-uni/src/index.ts b/packages/vite-plugin-uni/src/index.ts index 97fa7d0f9b..94b0138830 100644 --- a/packages/vite-plugin-uni/src/index.ts +++ b/packages/vite-plugin-uni/src/index.ts @@ -17,7 +17,11 @@ import { import { createConfig } from './config' import { createConfigResolved } from './configResolved' import { uniCopyPlugin } from './plugins/copy' -import { initExtraPlugins, initPluginUniOptions } from './utils' +import { + initExtraPlugins, + initPluginUniOptions, + rewriteCompilerSfcParse, +} from './utils' import { initPluginViteLegacyOptions, initPluginVueJsxOptions, @@ -31,6 +35,8 @@ const pkg = require(path.resolve(__dirname, '../package.json')) initModuleAlias() +rewriteCompilerSfcParse() + process.env.UNI_COMPILER_VERSION = pkg['uni-app']?.['compilerVersion'] || '' process.env.UNI_COMPILER_VERSION_TYPE = pkg.version.includes('alpha') ? 'a' diff --git a/packages/vite-plugin-uni/src/utils/index.ts b/packages/vite-plugin-uni/src/utils/index.ts index 171a5da392..a863fe0193 100644 --- a/packages/vite-plugin-uni/src/utils/index.ts +++ b/packages/vite-plugin-uni/src/utils/index.ts @@ -1,2 +1,3 @@ export * from './filter' export * from './plugin' +export * from './polyfill' diff --git a/packages/vite-plugin-uni/src/utils/polyfill.ts b/packages/vite-plugin-uni/src/utils/polyfill.ts new file mode 100644 index 0000000000..866953b043 --- /dev/null +++ b/packages/vite-plugin-uni/src/utils/polyfill.ts @@ -0,0 +1,22 @@ +import path from 'path' +import { EXTNAME_VUE, resolveBuiltIn } from '@dcloudio/uni-cli-shared' +import { SFCParseOptions } from '@vue/compiler-sfc' +/** + * TODO 临时重写,解决 @vitejs/plugin-vue 的 Bug + */ +export function rewriteCompilerSfcParse() { + // @ts-ignore + const compilerSfc = require(resolveBuiltIn('@vue/compiler-sfc')) + const { parse } = compilerSfc + compilerSfc.parse = (source: string, options: SFCParseOptions) => { + if (options.filename) { + const extname = path.extname(options.filename) + // wxs、filter、renderjs + if (extname && !EXTNAME_VUE.includes(extname)) { + const tag = extname.slice(1) + source = `<${tag}>` + source + `` + } + } + return parse(source, options) + } +} -- GitLab