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..b20d26d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,17 +2,17 @@ name: Test on: push: - branches: [ master ] + branches: [ main ] pull_request: - branches: [ master ] + branches: [ main ] 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/README.md b/README.md index 9dcec78..df91818 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@

- +
npm

@@ -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 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. diff --git a/lib/index.iife.js b/lib/index.iife.js index 6df76f8..724e3a8 100644 --- a/lib/index.iife.js +++ b/lib/index.iife.js @@ -3,7 +3,18 @@ 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 + } + 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..0c9b3b2 --- /dev/null +++ b/lib/v2.7/index.cjs @@ -0,0 +1,11 @@ +var Vue = require('vue') + +Object.keys(Vue).forEach(function(key) { + exports[key] = Vue[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..6a12502 --- /dev/null +++ b/lib/v2.7/index.d.ts @@ -0,0 +1,26 @@ +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 + +// 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..6ec1976 --- /dev/null +++ b/lib/v2.7/index.mjs @@ -0,0 +1,16 @@ +import Vue from 'vue' + +var isVue2 = true +var isVue3 = false +var Vue2 = Vue + +function install() {} + +export * from 'vue' +export { + Vue, + Vue2, + isVue2, + isVue3, + install, +} diff --git a/package.json b/package.json index 1bafef3..63a3b56 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-demi", - "version": "0.12.5", + "version": "0.13.1", "engines": { "node": ">=12" }, 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 {