Quellcode durchsuchen

feat(runtime-shared): init

三咲智子 Kevin Deng vor 2 Jahren
Ursprung
Commit
bfb52502f8

+ 1 - 1
package.json

@@ -11,7 +11,7 @@
     "size": "run-s \"size-*\" && tsx scripts/usage-size.ts",
     "size-global": "node scripts/build.js vue vue-vapor runtime-dom runtime-vapor compiler-dom compiler-vapor -f global -p --size",
     "size-esm-runtime": "node scripts/build.js vue vue-vapor -f esm-bundler-runtime",
-    "size-esm": "node scripts/build.js runtime-dom runtime-vapor runtime-core reactivity shared -f esm-bundler",
+    "size-esm": "node scripts/build.js runtime-shared runtime-dom runtime-vapor runtime-core reactivity shared -f esm-bundler",
     "check": "tsc --incremental --noEmit",
     "lint": "eslint --cache .",
     "format": "prettier --write --cache .",

+ 1 - 2
packages/compiler-vapor/__tests__/transforms/__snapshots__/transformElement.spec.ts.snap

@@ -175,8 +175,7 @@ export function render(_ctx) {
 `;
 
 exports[`compiler: element transform > component > v-on="obj" 1`] = `
-"import { toHandlers as _toHandlers } from 'vue';
-import { resolveComponent as _resolveComponent, createComponent as _createComponent } from 'vue/vapor';
+"import { resolveComponent as _resolveComponent, toHandlers as _toHandlers, createComponent as _createComponent } from 'vue/vapor';
 
 export function render(_ctx) {
   const _component_Foo = _resolveComponent("Foo")

+ 2 - 1
packages/compiler-vapor/src/generators/component.ts

@@ -59,6 +59,7 @@ export function genCreateComponent(
 }
 
 export function genRawProps(props: IRProps[], context: CodegenContext) {
+  const { vaporHelper } = context
   const frag = props
     .map(props => {
       if (isArray(props)) {
@@ -70,7 +71,7 @@ export function genRawProps(props: IRProps[], context: CodegenContext) {
           expr = genMulti(SEGMENTS_OBJECT, genProp(props, context))
         else {
           expr = genExpression(props.value, context)
-          if (props.handler) expr = genCall(context.helper('toHandlers'), expr)
+          if (props.handler) expr = genCall(vaporHelper('toHandlers'), expr)
         }
         return ['() => (', ...expr, ')']
       }

+ 2 - 1
packages/runtime-core/package.json

@@ -47,6 +47,7 @@
   "homepage": "https://github.com/vuejs/core-vapor/tree/main/packages/runtime-core#readme",
   "dependencies": {
     "@vue/shared": "workspace:*",
-    "@vue/reactivity": "workspace:*"
+    "@vue/reactivity": "workspace:*",
+    "@vue/runtime-shared": "workspace:*"
   }
 }

+ 2 - 23
packages/runtime-core/src/helpers/toHandlers.ts

@@ -1,25 +1,4 @@
-import { isObject, toHandlerKey } from '@vue/shared'
+import { toHandlers as _toHandlers } from '@vue/runtime-shared'
 import { warn } from '../warning'
 
-/**
- * For prefixing keys in v-on="obj" with "on"
- * @private
- */
-export function toHandlers(
-  obj: Record<string, any>,
-  preserveCaseIfNecessary?: boolean,
-): Record<string, any> {
-  const ret: Record<string, any> = {}
-  if (__DEV__ && !isObject(obj)) {
-    warn(`v-on with no argument expects an object value.`)
-    return ret
-  }
-  for (const key in obj) {
-    ret[
-      preserveCaseIfNecessary && /[A-Z]/.test(key)
-        ? `on:${key}`
-        : toHandlerKey(key)
-    ] = obj[key]
-  }
-  return ret
-}
+export const toHandlers = _toHandlers.bind(undefined, warn)

+ 21 - 0
packages/runtime-shared/LICENSE

@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2018-present, Yuxi (Evan) You
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.

+ 1 - 0
packages/runtime-shared/README.md

@@ -0,0 +1 @@
+# @vue/runtime-shared

+ 7 - 0
packages/runtime-shared/index.js

@@ -0,0 +1,7 @@
+'use strict'
+
+if (process.env.NODE_ENV === 'production') {
+  module.exports = require('./dist/runtime-shared.cjs.prod.js')
+} else {
+  module.exports = require('./dist/runtime-shared.cjs.js')
+}

+ 47 - 0
packages/runtime-shared/package.json

@@ -0,0 +1,47 @@
+{
+  "name": "@vue/runtime-shared",
+  "version": "3.0.0-vapor",
+  "description": "@vue/runtime-shared",
+  "main": "index.js",
+  "module": "dist/runtime-shared.esm-bundler.js",
+  "types": "dist/runtime-shared.d.ts",
+  "files": [
+    "index.js",
+    "dist"
+  ],
+  "exports": {
+    ".": {
+      "types": "./dist/runtime-shared.d.ts",
+      "node": {
+        "production": "./dist/runtime-shared.cjs.prod.js",
+        "development": "./dist/runtime-shared.cjs.js",
+        "default": "./index.js"
+      },
+      "module": "./dist/runtime-shared.esm-bundler.js",
+      "import": "./dist/runtime-shared.esm-bundler.js",
+      "require": "./index.js"
+    },
+    "./*": "./*"
+  },
+  "sideEffects": false,
+  "buildOptions": {
+    "formats": [
+      "esm-bundler",
+      "cjs"
+    ]
+  },
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/vuejs/core-vapor.git",
+    "directory": "packages/runtime-shared"
+  },
+  "keywords": [
+    "vue"
+  ],
+  "author": "Evan You",
+  "license": "MIT",
+  "bugs": {
+    "url": "https://github.com/vuejs/core-vapor/issues"
+  },
+  "homepage": "https://github.com/vuejs/core-vapor/tree/main/packages/runtime-shared#readme"
+}

+ 1 - 0
packages/runtime-shared/src/index.ts

@@ -0,0 +1 @@
+export { toHandlers } from './toHandlers'

+ 25 - 0
packages/runtime-shared/src/toHandlers.ts

@@ -0,0 +1,25 @@
+import { isObject, toHandlerKey } from '@vue/shared'
+
+/**
+ * For prefixing keys in v-on="obj" with "on"
+ * @private
+ */
+export function toHandlers(
+  warn: (msg: string) => void,
+  obj: Record<string, any>,
+  preserveCaseIfNecessary?: boolean,
+): Record<string, any> {
+  const ret: Record<string, any> = {}
+  if (__DEV__ && !isObject(obj)) {
+    warn(`v-on with no argument expects an object value.`)
+    return ret
+  }
+  for (const key in obj) {
+    ret[
+      preserveCaseIfNecessary && /[A-Z]/.test(key)
+        ? `on:${key}`
+        : toHandlerKey(key)
+    ] = obj[key]
+  }
+  return ret
+}

+ 2 - 1
packages/runtime-vapor/package.json

@@ -36,6 +36,7 @@
   "homepage": "https://github.com/vuejs/core-vapor/tree/dev/packages/runtime-vapor#readme",
   "dependencies": {
     "@vue/shared": "workspace:*",
-    "@vue/reactivity": "workspace:*"
+    "@vue/reactivity": "workspace:*",
+    "@vue/runtime-shared": "workspace:*"
   }
 }

+ 4 - 0
packages/runtime-vapor/src/helpers/toHandlers.ts

@@ -0,0 +1,4 @@
+import { toHandlers as _toHandlers } from '@vue/runtime-shared'
+import { warn } from '../warning'
+
+export const toHandlers = _toHandlers.bind(undefined, warn)

+ 1 - 0
packages/runtime-vapor/src/index.ts

@@ -127,6 +127,7 @@ export { createFor } from './apiCreateFor'
 export { createComponent } from './apiCreateComponent'
 
 export { resolveComponent, resolveDirective } from './helpers/resolveAssets'
+export { toHandlers } from './helpers/toHandlers'
 
 // **Internal** DOM-only runtime directive helpers
 export {

+ 8 - 0
pnpm-lock.yaml

@@ -319,6 +319,9 @@ importers:
       '@vue/reactivity':
         specifier: workspace:*
         version: link:../reactivity
+      '@vue/runtime-shared':
+        specifier: workspace:*
+        version: link:../runtime-shared
       '@vue/shared':
         specifier: workspace:*
         version: link:../shared
@@ -335,6 +338,8 @@ importers:
         specifier: ^3.1.3
         version: 3.1.3
 
+  packages/runtime-shared: {}
+
   packages/runtime-test:
     dependencies:
       '@vue/runtime-core':
@@ -349,6 +354,9 @@ importers:
       '@vue/reactivity':
         specifier: workspace:*
         version: link:../reactivity
+      '@vue/runtime-shared':
+        specifier: workspace:*
+        version: link:../runtime-shared
       '@vue/shared':
         specifier: workspace:*
         version: link:../shared

+ 4 - 0
scripts/utils.js

@@ -28,6 +28,10 @@ export function fuzzyMatchTarget(partialTargets, includeAllMatching) {
   /** @type {Array<string>} */
   const matched = []
   partialTargets.forEach(partialTarget => {
+    if (!includeAllMatching && targets.includes(partialTarget)) {
+      matched.push(partialTarget)
+      return
+    }
     for (const target of targets) {
       if (target.match(partialTarget)) {
         matched.push(target)