From 352cf035e5c41c2c73d840733e8230568bc4886d Mon Sep 17 00:00:00 2001 From: justintaddei <=> Date: Tue, 7 Nov 2023 03:07:53 -0700 Subject: [PATCH] feat: add support for nuxt3 via postinstall script --- nuxt/v3/postinstall.js | 29 +++++++++++++++++++++++++++++ nuxt/v3/v-wave.client.ts | 9 +++++++++ nuxt/v3/v-wave.server.ts | 6 ++++++ package.json | 3 ++- src/index.ts | 1 + tsconfig.json | 2 +- 6 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 nuxt/v3/postinstall.js create mode 100644 nuxt/v3/v-wave.client.ts create mode 100644 nuxt/v3/v-wave.server.ts diff --git a/nuxt/v3/postinstall.js b/nuxt/v3/postinstall.js new file mode 100644 index 0000000..a928aba --- /dev/null +++ b/nuxt/v3/postinstall.js @@ -0,0 +1,29 @@ +const fs = require('fs') +const path = require('path') + +const pkg = require(path.join(process.env.INIT_CWD, 'package.json')) + +const nuxtVersion = pkg.dependencies.nuxt ?? pkg.devDependencies.nuxt + +if (!nuxtVersion || /2\.\d+\.\d+/.test(nuxtVersion)) { + process.exit(0) +} + +if (!fs.existsSync(path.join(process.env.INIT_CWD, 'plugins'))) { + fs.mkdirSync(path.join(process.env.INIT_CWD, 'plugins')) +} + +try { + fs.copyFileSync( + path.join(__dirname, 'v-wave.client.ts'), + path.join(process.env.INIT_CWD, 'plugins', 'v-wave.client.ts'), + fs.constants.COPYFILE_EXCL + ) + fs.copyFileSync( + path.join(__dirname, 'v-wave.server.ts'), + path.join(process.env.INIT_CWD, 'plugins', 'v-wave.server.ts'), + fs.constants.COPYFILE_EXCL + ) +} catch (e) { + process.exit(0) +} diff --git a/nuxt/v3/v-wave.client.ts b/nuxt/v3/v-wave.client.ts new file mode 100644 index 0000000..79bfe1b --- /dev/null +++ b/nuxt/v3/v-wave.client.ts @@ -0,0 +1,9 @@ +import vWave, { type IVWavePluginOptions } from 'v-wave' + +export const options: Partial = { + // Place any global v-wave options here +} + +export default defineNuxtPlugin((nuxtApp) => { + nuxtApp.vueApp.use(vWave, options) +}) diff --git a/nuxt/v3/v-wave.server.ts b/nuxt/v3/v-wave.server.ts new file mode 100644 index 0000000..0248b8a --- /dev/null +++ b/nuxt/v3/v-wave.server.ts @@ -0,0 +1,6 @@ +import { options } from './v-wave.client'; + +export default defineNuxtPlugin(({ vueApp }) => { + vueApp.directive(options.directive ?? 'wave', {}); + vueApp.directive(`${options.directive ?? 'wave'}-trigger`, {}); +}); diff --git a/package.json b/package.json index 976da10..a17b5f6 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "lint": "tslint -p tsconfig.json", "prepare": "npm run build && husky install", "prepublishOnly": "npm test && npm run lint", - "release": "npm run lint && npm run format && npm run test && standard-version" + "release": "npm run lint && npm run format && npm run test && standard-version", + "postinstall": "node ./nuxt/v3/postinstall.js" }, "husky": { "hooks": { diff --git a/src/index.ts b/src/index.ts index c150bdf..742cbc4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -105,3 +105,4 @@ const VWave: VWaveInstallObject & { createLocalWaveDirective: typeof createDirec } export default VWave +export { IVWaveDirectiveOptions, IVWavePluginOptions } diff --git a/tsconfig.json b/tsconfig.json index 4553af4..91e5682 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,5 +12,5 @@ "esModuleInterop": true }, "include": ["src"], - "exclude": ["node_modules", "**/__tests__/*"] + "exclude": ["node_modules", "**/__tests__/*", "nuxt"] }