From bfb52502f8a1c6bb89dc9cf57cb70f1fc2c056ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Wed, 1 May 2024 20:01:57 +0900 Subject: [PATCH] feat(runtime-shared): init --- package.json | 2 +- .../transformElement.spec.ts.snap | 3 +- .../src/generators/component.ts | 3 +- packages/runtime-core/package.json | 3 +- .../runtime-core/src/helpers/toHandlers.ts | 25 +--------- packages/runtime-shared/LICENSE | 21 +++++++++ packages/runtime-shared/README.md | 1 + packages/runtime-shared/index.js | 7 +++ packages/runtime-shared/package.json | 47 +++++++++++++++++++ packages/runtime-shared/src/index.ts | 1 + packages/runtime-shared/src/toHandlers.ts | 25 ++++++++++ packages/runtime-vapor/package.json | 3 +- .../runtime-vapor/src/helpers/toHandlers.ts | 4 ++ packages/runtime-vapor/src/index.ts | 1 + pnpm-lock.yaml | 8 ++++ scripts/utils.js | 4 ++ 16 files changed, 129 insertions(+), 29 deletions(-) create mode 100644 packages/runtime-shared/LICENSE create mode 100644 packages/runtime-shared/README.md create mode 100644 packages/runtime-shared/index.js create mode 100644 packages/runtime-shared/package.json create mode 100644 packages/runtime-shared/src/index.ts create mode 100644 packages/runtime-shared/src/toHandlers.ts create mode 100644 packages/runtime-vapor/src/helpers/toHandlers.ts diff --git a/package.json b/package.json index 973d4cccf..27c380ae2 100644 --- a/package.json +++ b/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 .", diff --git a/packages/compiler-vapor/__tests__/transforms/__snapshots__/transformElement.spec.ts.snap b/packages/compiler-vapor/__tests__/transforms/__snapshots__/transformElement.spec.ts.snap index ed6a96272..025c945ec 100644 --- a/packages/compiler-vapor/__tests__/transforms/__snapshots__/transformElement.spec.ts.snap +++ b/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") diff --git a/packages/compiler-vapor/src/generators/component.ts b/packages/compiler-vapor/src/generators/component.ts index fba45ab1e..3256e514c 100644 --- a/packages/compiler-vapor/src/generators/component.ts +++ b/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, ')'] } diff --git a/packages/runtime-core/package.json b/packages/runtime-core/package.json index 66f99cd94..f1edc34a8 100644 --- a/packages/runtime-core/package.json +++ b/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:*" } } diff --git a/packages/runtime-core/src/helpers/toHandlers.ts b/packages/runtime-core/src/helpers/toHandlers.ts index 3e7dcb327..748a6dce3 100644 --- a/packages/runtime-core/src/helpers/toHandlers.ts +++ b/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, - preserveCaseIfNecessary?: boolean, -): Record { - const ret: Record = {} - 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) diff --git a/packages/runtime-shared/LICENSE b/packages/runtime-shared/LICENSE new file mode 100644 index 000000000..15f1f7e7a --- /dev/null +++ b/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. diff --git a/packages/runtime-shared/README.md b/packages/runtime-shared/README.md new file mode 100644 index 000000000..82da84bc1 --- /dev/null +++ b/packages/runtime-shared/README.md @@ -0,0 +1 @@ +# @vue/runtime-shared diff --git a/packages/runtime-shared/index.js b/packages/runtime-shared/index.js new file mode 100644 index 000000000..2cd4f6df8 --- /dev/null +++ b/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') +} diff --git a/packages/runtime-shared/package.json b/packages/runtime-shared/package.json new file mode 100644 index 000000000..cc3c35296 --- /dev/null +++ b/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" +} diff --git a/packages/runtime-shared/src/index.ts b/packages/runtime-shared/src/index.ts new file mode 100644 index 000000000..a5c47e5b7 --- /dev/null +++ b/packages/runtime-shared/src/index.ts @@ -0,0 +1 @@ +export { toHandlers } from './toHandlers' diff --git a/packages/runtime-shared/src/toHandlers.ts b/packages/runtime-shared/src/toHandlers.ts new file mode 100644 index 000000000..bad1d665e --- /dev/null +++ b/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, + preserveCaseIfNecessary?: boolean, +): Record { + const ret: Record = {} + 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 +} diff --git a/packages/runtime-vapor/package.json b/packages/runtime-vapor/package.json index 44f90721f..1c9ba45c3 100644 --- a/packages/runtime-vapor/package.json +++ b/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:*" } } diff --git a/packages/runtime-vapor/src/helpers/toHandlers.ts b/packages/runtime-vapor/src/helpers/toHandlers.ts new file mode 100644 index 000000000..748a6dce3 --- /dev/null +++ b/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) diff --git a/packages/runtime-vapor/src/index.ts b/packages/runtime-vapor/src/index.ts index 88ec0ea90..b5f4235c4 100644 --- a/packages/runtime-vapor/src/index.ts +++ b/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 { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0e2c0396f..dd05464bd 100644 --- a/pnpm-lock.yaml +++ b/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 diff --git a/scripts/utils.js b/scripts/utils.js index 6c3844dd1..c8bf37d1e 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -28,6 +28,10 @@ export function fuzzyMatchTarget(partialTargets, includeAllMatching) { /** @type {Array} */ 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)