From 6acc08f7388fbdc13519c76dc38a8df56abc3c37 Mon Sep 17 00:00:00 2001 From: Marek Vospel <73162071+marekvospel@users.noreply.github.com> Date: Tue, 31 May 2022 12:53:10 +0200 Subject: [PATCH 1/7] docs: add pnpm install instructions to README (#159) Co-authored-by: Anthony Fu --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 9dcec78..c424d1b 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,11 @@ Install this as your plugin's dependency: npm i vue-demi # or yarn add vue-demi +# or +pnpm i vue-demi ``` + Add `vue` and `@vue/composition-api` to your plugin's peer dependencies to specify what versions you support. ```jsonc From f2f64e3ce01b6561302fc2867a74e4f08f2622e7 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 31 May 2022 20:17:16 +0800 Subject: [PATCH 2/7] feat: support vue 2.7 (#162) --- .github/test.js | 59 +++++++++++++++++++++++++++++++------- .github/workflows/test.yml | 12 ++++---- lib/index.iife.js | 29 ++++++++++++++++++- lib/v2.7/index.cjs | 29 +++++++++++++++++++ lib/v2.7/index.d.ts | 31 ++++++++++++++++++++ lib/v2.7/index.mjs | 34 ++++++++++++++++++++++ scripts/postinstall.js | 3 ++ scripts/switch-cli.js | 7 +++-- 8 files changed, 184 insertions(+), 20 deletions(-) create mode 100644 lib/v2.7/index.cjs create mode 100644 lib/v2.7/index.d.ts create mode 100644 lib/v2.7/index.mjs diff --git a/.github/test.js b/.github/test.js index 017f52c..320b989 100644 --- a/.github/test.js +++ b/.github/test.js @@ -8,7 +8,8 @@ const [agent, version, type = 'commonjs'] = process.argv.slice(2) const ROOT = resolve(__dirname, '..') const DIR = resolve(ROOT, `../vue-demi-test-${type}`) -const isVue2 = version === '2' +const isVue2 = version.startsWith('2') +const isVue27 = version.startsWith('2.7') const isCjs = type === 'commonjs' function pack() { @@ -21,20 +22,24 @@ function installDeps() { let installCmd = agent === 'yarn' ? `${agent} add` : `${agent} i` - execSync(`${installCmd} ${isVue2 ? 'vue@2 @vue/composition-api' : 'vue@3'}`, { cwd: DIR, stdio: 'inherit' }) + const packages = isVue27 ? 'vue@v2-alpha' : isVue2 ? `vue@2.6 @vue/composition-api` : 'vue@3' + execSync(`${installCmd} ${packages}`, { cwd: DIR, stdio: 'inherit' }) execSync(`${installCmd} ${agent === 'yarn' ? `file:${tarball}` : tarball} --force`, { cwd: DIR, stdio: 'inherit' }) } function prepareTestPackage(type = 'commonjs') { - if (fs.existsSync(DIR)) - fs.rmSync(DIR, { recursive: true }) + if (fs.existsSync(DIR)) fs.rmSync(DIR, { recursive: true }) fs.mkdirSync(DIR) - fs.writeFileSync(join(DIR, 'package.json'), JSON.stringify({ - name: `vue-demi-test-${type}`, - version: packageVersion, - type, - }), 'utf-8') + fs.writeFileSync( + join(DIR, 'package.json'), + JSON.stringify({ + name: `vue-demi-test-${type}`, + version: packageVersion, + type, + }), + 'utf-8' + ) installDeps() } @@ -56,21 +61,53 @@ if (!isCjs && !/export\s\{\n\s\sVue,\n\s\sVue2,\n\s\sisVue2/gm.test(mod)) { failed = true } +const outputVersion = execSync(`node -e "console.log(require('vue-demi').version)"`, { cwd: DIR }).toString().trim() +console.log('version: ' + outputVersion) + // isVue2 const is2 = execSync(`node -e "console.log(require('vue-demi').isVue2)"`, { cwd: DIR }).toString().trim() if (is2 !== `${isVue2}`) { - console.log(`isVue2: ${is2} === ${isVue2}`) + console.log(`isVue2: ${is2} !== ${isVue2}`) failed = true } const hasVue2 = execSync(`node -e "console.log(require('vue-demi').Vue2 !== undefined)"`, { cwd: DIR }).toString().trim() if (hasVue2 !== `${isVue2}`) { - console.log(`hasVue2: ${hasVue2} === ${isVue2}`) + console.log(`hasVue2: ${hasVue2} !== ${isVue2}`) + failed = true +} + +const importCJS = `const { ref, computed } = require('vue-demi');` +const importESM = `const { ref, computed } = await import('vue-demi');` + +const snippet = ` +let a = ref(12) +let b = computed(() => a.value * 2) +console.log(b.value) +a.value += 1 +console.log(b.value) +` + .replace(/\n/g, ';') + .trim() + +// ref +const refCJS = execSync(`node -e "${importCJS}${snippet}"`, { cwd: DIR }).toString().trim() +if (refCJS !== `24\n26`) { + console.log(`ref(cjs): ${refCJS} !== 24\n26`) failed = true } +// TODO: 2.7's ESM can't runs in Node currently +if (!isVue27) { + const refESM = execSync(`node -e "(async ()=>{${importESM}${snippet}})()"`, { cwd: DIR }).toString().trim() + if (refESM !== `24\n26`) { + console.log(`ref(esm): ${refESM} !== 24\n26`) + failed = true + } +} + if (failed) { setTimeout(() => { process.exit(1) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c74fb43..659089c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,9 +10,9 @@ jobs: npm: strategy: matrix: - node: [12.x, 15.x] + node: [14.x, 16.x] os: [ubuntu-latest] - vue: [2, 3] + vue: [2.6, 2.7, 3] fail-fast: false runs-on: ${{ matrix.os }} @@ -30,9 +30,9 @@ jobs: yarn: strategy: matrix: - node: [12.x, 14.x] + node: [14.x, 16.x] os: [ubuntu-latest] - vue: [2, 3] + vue: [2.6, 2.7, 3] yarn: [latest, berry] type: [commonjs, module] @@ -52,9 +52,9 @@ jobs: pnpm: strategy: matrix: - node: [12.x, 14.x] + node: [14.x, 16.x] os: [ubuntu-latest] - vue: [2, 3] + vue: [2.6, 2.7, 3] runs-on: ${{ matrix.os }} diff --git a/lib/index.iife.js b/lib/index.iife.js index 6df76f8..338f6a1 100644 --- a/lib/index.iife.js +++ b/lib/index.iife.js @@ -3,7 +3,34 @@ return VueDemi } if (Vue) { - if (Vue.version.slice(0, 2) === '2.') { + if (Vue.version.slice(0, 4) === '2.7.') { + for (var key in Vue) { + VueDemi[key] = Vue[key] + } + VueDemi.isVue2 = true + VueDemi.isVue3 = false + VueDemi.install = function (){} + VueDemi.Vue = Vue + VueDemi.Vue2 = Vue + VueDemi.version = Vue.version + VueDemi.set = function(target, key, val) { + if (Array.isArray(target)) { + target.length = Math.max(target.length, key) + target.splice(key, 1, val) + return val + } + Vue.set(target, key, val) + return val + } + VueDemi.del = function(target, key) { + if (Array.isArray(target)) { + target.splice(key, 1) + return + } + Vue.delete(target, key) + } + } + else if (Vue.version.slice(0, 2) === '2.') { if (VueCompositionAPI) { for (var key in VueCompositionAPI) { VueDemi[key] = VueCompositionAPI[key] diff --git a/lib/v2.7/index.cjs b/lib/v2.7/index.cjs new file mode 100644 index 0000000..65a81d3 --- /dev/null +++ b/lib/v2.7/index.cjs @@ -0,0 +1,29 @@ +var Vue = require('vue') + +Object.keys(Vue).forEach(function(key) { + exports[key] = Vue[key] +}) + +exports.set = function(target, key, val) { + if (Array.isArray(target)) { + target.length = Math.max(target.length, key) + target.splice(key, 1, val) + return val + } + Vue.set(target, key, val) + return val +} + +exports.del = function(target, key) { + if (Array.isArray(target)) { + target.splice(key, 1) + return + } + Vue.delete(target, key) +} + +exports.Vue = Vue +exports.Vue2 = Vue +exports.isVue2 = true +exports.isVue3 = false +exports.install = function(){} diff --git a/lib/v2.7/index.d.ts b/lib/v2.7/index.d.ts new file mode 100644 index 0000000..9b34339 --- /dev/null +++ b/lib/v2.7/index.d.ts @@ -0,0 +1,31 @@ +import Vue from 'vue' +import type { PluginFunction, PluginObject } from 'vue' +declare const isVue2: boolean +declare const isVue3: boolean +declare const Vue2: Vue | undefined +declare const version: string +declare const install: (vue?: Vue) => void +/** + * @deprecated To avoid bringing in all the tree-shakable modules, this API has been deprecated. Use `Vue2` or named exports instead. + * Refer to https://github.com/vueuse/vue-demi/issues/41 + */ +declare const V: Vue + +/** + * DebuggerEvent is a Vue 3 development only feature. This type cannot exist in Vue 2. + */ +export declare type DebuggerEvent = never + +// accept no generic because Vue 3 doesn't accept any +// https://github.com/vuejs/vue-next/pull/2758/ +export declare type Plugin = PluginObject | PluginFunction +export type { VNode } from 'vue' +export * from 'vue' +export { + V as Vue, + Vue2, + isVue2, + isVue3, + version, + install, +} diff --git a/lib/v2.7/index.mjs b/lib/v2.7/index.mjs new file mode 100644 index 0000000..cbb72ae --- /dev/null +++ b/lib/v2.7/index.mjs @@ -0,0 +1,34 @@ +import Vue from 'vue' + +var isVue2 = true +var isVue3 = false +var Vue2 = Vue + +function install() {} + +export function set(target, key, val) { + if (Array.isArray(target)) { + target.length = Math.max(target.length, key) + target.splice(key, 1, val) + return val + } + Vue.set(target, key, val) + return val +} + +export function del(target, key) { + if (Array.isArray(target)) { + target.splice(key, 1) + return + } + Vue.delete(target, key) +} + +export * from 'vue' +export { + Vue, + Vue2, + isVue2, + isVue3, + install, +} diff --git a/scripts/postinstall.js b/scripts/postinstall.js index 100283e..62484ec 100644 --- a/scripts/postinstall.js +++ b/scripts/postinstall.js @@ -5,6 +5,9 @@ const Vue = loadModule('vue') if (!Vue || typeof Vue.version !== 'string') { console.warn('[vue-demi] Vue is not found. Please run "npm install vue" to install.') } +else if (Vue.version.startsWith('2.7.')) { + switchVersion(2.7) +} else if (Vue.version.startsWith('2.')) { switchVersion(2) } diff --git a/scripts/switch-cli.js b/scripts/switch-cli.js index 0155c6f..4aaac51 100644 --- a/scripts/switch-cli.js +++ b/scripts/switch-cli.js @@ -3,10 +3,13 @@ const { switchVersion } = require('./utils') const version = process.argv[2] const vueEntry = process.argv[3] || 'vue' -if (version == '2') { +if (version === '2.7') { + switchVersion(2.7, vueEntry) + console.log(`[vue-demi] Switched for Vue 2.7 (entry: "${vueEntry}")`) +} else if (version === '2') { switchVersion(2, vueEntry) console.log(`[vue-demi] Switched for Vue 2 (entry: "${vueEntry}")`) -} else if (version == '3') { +} else if (version === '3') { switchVersion(3, vueEntry) console.log(`[vue-demi] Switched for Vue 3 (entry: "${vueEntry}")`) } else { From 52ea4bbb08010526762a99fb9e43edb58a97162d Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 31 May 2022 20:22:32 +0800 Subject: [PATCH 3/7] chore: swtich to main --- .github/workflows/test.yml | 4 ++-- README.md | 2 +- examples/README.md | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 659089c..b20d26d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,9 +2,9 @@ name: Test on: push: - branches: [ master ] + branches: [ main ] pull_request: - branches: [ master ] + branches: [ main ] jobs: npm: diff --git a/README.md b/README.md index c424d1b..df91818 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@

- +
npm

diff --git a/examples/README.md b/examples/README.md index 291b9c5..25bd132 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,10 +1,10 @@ # Examples of Vue Demi -A demo Vue library is published as [`@vue-demi/use-mouse`](https://github.com/antfu/vue-demi/blob/master/examples/%40vue-demi/use-mouse/src/index.ts) [![npm](https://img.shields.io/npm/v/@vue-demi/use-mouse)](https://www.npmjs.com/package/@vue-demi/use-mouse) +A demo Vue library is published as [`@vue-demi/use-mouse`](https://github.com/antfu/vue-demi/blob/main/examples/%40vue-demi/use-mouse/src/index.ts) [![npm](https://img.shields.io/npm/v/@vue-demi/use-mouse)](https://www.npmjs.com/package/@vue-demi/use-mouse) You can see it being imported by - Vue 2 ([Vue CLI Project](./demo-vue-2-cli)) - Vue 3 ([Vite Project](./demo-vue-3-vite)) -And things just works. \ No newline at end of file +And things just works. From 82bccb7e2d6e22b9786f1a2f49ad841eddbd888a Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 31 May 2022 20:23:02 +0800 Subject: [PATCH 4/7] release v0.13.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1bafef3..0c1ed12 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-demi", - "version": "0.12.5", + "version": "0.13.0", "engines": { "node": ">=12" }, From 7b929a70617516c378400a5abb7a6fc11d8c71b6 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 31 May 2022 21:12:08 +0800 Subject: [PATCH 5/7] fix: remove `DebuggerEvent` for vue 2.7 --- lib/v2.7/index.d.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/v2.7/index.d.ts b/lib/v2.7/index.d.ts index 9b34339..6a12502 100644 --- a/lib/v2.7/index.d.ts +++ b/lib/v2.7/index.d.ts @@ -11,11 +11,6 @@ declare const install: (vue?: Vue) => void */ declare const V: Vue -/** - * DebuggerEvent is a Vue 3 development only feature. This type cannot exist in Vue 2. - */ -export declare type DebuggerEvent = never - // accept no generic because Vue 3 doesn't accept any // https://github.com/vuejs/vue-next/pull/2758/ export declare type Plugin = PluginObject | PluginFunction From 6abe38c545396fd81732d11b1108ed9284eb54ac Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 1 Jun 2022 16:11:41 +0800 Subject: [PATCH 6/7] fix(2.7): remove `set` and `del` polyfill as Vue 2.7.0-alpha.2 --- lib/index.iife.js | 16 ---------------- lib/v2.7/index.cjs | 18 ------------------ lib/v2.7/index.mjs | 18 ------------------ 3 files changed, 52 deletions(-) diff --git a/lib/index.iife.js b/lib/index.iife.js index 338f6a1..724e3a8 100644 --- a/lib/index.iife.js +++ b/lib/index.iife.js @@ -13,22 +13,6 @@ VueDemi.Vue = Vue VueDemi.Vue2 = Vue VueDemi.version = Vue.version - VueDemi.set = function(target, key, val) { - if (Array.isArray(target)) { - target.length = Math.max(target.length, key) - target.splice(key, 1, val) - return val - } - Vue.set(target, key, val) - return val - } - VueDemi.del = function(target, key) { - if (Array.isArray(target)) { - target.splice(key, 1) - return - } - Vue.delete(target, key) - } } else if (Vue.version.slice(0, 2) === '2.') { if (VueCompositionAPI) { diff --git a/lib/v2.7/index.cjs b/lib/v2.7/index.cjs index 65a81d3..0c9b3b2 100644 --- a/lib/v2.7/index.cjs +++ b/lib/v2.7/index.cjs @@ -4,24 +4,6 @@ Object.keys(Vue).forEach(function(key) { exports[key] = Vue[key] }) -exports.set = function(target, key, val) { - if (Array.isArray(target)) { - target.length = Math.max(target.length, key) - target.splice(key, 1, val) - return val - } - Vue.set(target, key, val) - return val -} - -exports.del = function(target, key) { - if (Array.isArray(target)) { - target.splice(key, 1) - return - } - Vue.delete(target, key) -} - exports.Vue = Vue exports.Vue2 = Vue exports.isVue2 = true diff --git a/lib/v2.7/index.mjs b/lib/v2.7/index.mjs index cbb72ae..6ec1976 100644 --- a/lib/v2.7/index.mjs +++ b/lib/v2.7/index.mjs @@ -6,24 +6,6 @@ var Vue2 = Vue function install() {} -export function set(target, key, val) { - if (Array.isArray(target)) { - target.length = Math.max(target.length, key) - target.splice(key, 1, val) - return val - } - Vue.set(target, key, val) - return val -} - -export function del(target, key) { - if (Array.isArray(target)) { - target.splice(key, 1) - return - } - Vue.delete(target, key) -} - export * from 'vue' export { Vue, From 0146526f17ee4b7f985572b1e306508526d543fc Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 1 Jun 2022 16:12:04 +0800 Subject: [PATCH 7/7] release v0.13.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0c1ed12..63a3b56 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-demi", - "version": "0.13.0", + "version": "0.13.1", "engines": { "node": ">=12" },