From d7bde145d02565ae0a30fab1689acffbcc061a2e Mon Sep 17 00:00:00 2001 From: Jessica Sachs Date: Sat, 16 May 2020 00:44:43 -0400 Subject: [PATCH] fix: esm build support by inlining @vue/shared utility functions --- src/stubs.ts | 2 +- src/utils/matchName.ts | 2 +- src/utils/vueShared.ts | 35 +++++++++++++++++++++++++++++++++++ src/vue-wrapper.ts | 2 +- 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/utils/vueShared.ts diff --git a/src/stubs.ts b/src/stubs.ts index f36857831..0b0bfd621 100644 --- a/src/stubs.ts +++ b/src/stubs.ts @@ -1,5 +1,5 @@ import { transformVNodeArgs, h, createVNode } from 'vue' -import { hyphenate } from '@vue/shared' +import { hyphenate } from './utils/vueShared' import { MOUNT_COMPONENT_REF, MOUNT_PARENT_NAME } from './constants' import { config } from './config' import { matchName } from './utils/matchName' diff --git a/src/utils/matchName.ts b/src/utils/matchName.ts index be2a4395c..f1b54ac39 100644 --- a/src/utils/matchName.ts +++ b/src/utils/matchName.ts @@ -1,4 +1,4 @@ -import { camelize, capitalize } from '@vue/shared' +import { camelize, capitalize } from './vueShared' export function matchName(target, sourceName) { const camelized = camelize(target) diff --git a/src/utils/vueShared.ts b/src/utils/vueShared.ts new file mode 100644 index 000000000..25d7ac8cf --- /dev/null +++ b/src/utils/vueShared.ts @@ -0,0 +1,35 @@ +const cacheStringFunction = string>(fn: T): T => { + const cache: Record = Object.create(null) + return ((str: string) => { + const hit = cache[str] + return hit || (cache[str] = fn(str)) + }) as any +} + +const camelizeRE = /-(\w)/g +export const camelize = cacheStringFunction((str: string): string => { + return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : '')) +}) + +export const capitalize = cacheStringFunction((str: string): string => { + return str.charAt(0).toUpperCase() + str.slice(1) +}) + +const hyphenateRE = /\B([A-Z])/g +export const hyphenate = cacheStringFunction((str: string): string => { + return str.replace(hyphenateRE, '-$1').toLowerCase() +}) + +export const enum ShapeFlags { + ELEMENT = 1, + FUNCTIONAL_COMPONENT = 1 << 1, + STATEFUL_COMPONENT = 1 << 2, + TEXT_CHILDREN = 1 << 3, + ARRAY_CHILDREN = 1 << 4, + SLOTS_CHILDREN = 1 << 5, + TELEPORT = 1 << 6, + SUSPENSE = 1 << 7, + COMPONENT_SHOULD_KEEP_ALIVE = 1 << 8, + COMPONENT_KEPT_ALIVE = 1 << 9, + COMPONENT = ShapeFlags.STATEFUL_COMPONENT | ShapeFlags.FUNCTIONAL_COMPONENT +} diff --git a/src/vue-wrapper.ts b/src/vue-wrapper.ts index 1fda76870..e4f0f6f4d 100644 --- a/src/vue-wrapper.ts +++ b/src/vue-wrapper.ts @@ -1,5 +1,5 @@ import { ComponentPublicInstance, nextTick, App } from 'vue' -import { ShapeFlags } from '@vue/shared' +import { ShapeFlags } from './utils/vueShared' import { config } from './config' import { DOMWrapper } from './dom-wrapper'