From 975761f01674cad09982ffcf374ac1f187c6a47b Mon Sep 17 00:00:00 2001 From: Julian Wowra Date: Sat, 12 Oct 2024 17:12:29 +0200 Subject: [PATCH 1/2] feat: `dayjs()` composable --- src/module.ts | 6 ++++++ src/runtime/composables/dayjs.d.ts | 5 +++-- src/runtime/composables/dayjs.ts | 8 ++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/module.ts b/src/module.ts index f84412c..be412e8 100644 --- a/src/module.ts +++ b/src/module.ts @@ -114,6 +114,12 @@ export default defineNuxtModule({ from: nuxt.options.alias['#dayjs'], }) + addImports({ + name: 'dayjs', + as: 'dayjs', + from: nuxt.options.alias['#dayjs'], + }) + addTemplate({ filename: 'dayjs.imports.mjs', getContents: () => generateImports(options), diff --git a/src/runtime/composables/dayjs.d.ts b/src/runtime/composables/dayjs.d.ts index 741ed14..0d1b174 100644 --- a/src/runtime/composables/dayjs.d.ts +++ b/src/runtime/composables/dayjs.d.ts @@ -1,3 +1,4 @@ -import type dayjs from '#build/dayjs.imports.mjs' +import type dayjsImport from '#build/dayjs.imports.mjs'; -export declare function useDayjs(): typeof dayjs +export declare function dayjs(): typeof dayjsImport.Dayjs +export declare function useDayjs(): typeof dayjsImport diff --git a/src/runtime/composables/dayjs.ts b/src/runtime/composables/dayjs.ts index 60483ae..8982bd3 100644 --- a/src/runtime/composables/dayjs.ts +++ b/src/runtime/composables/dayjs.ts @@ -1,5 +1,9 @@ -import dayjs from '#build/dayjs.imports.mjs' +import dayjsImport from '#build/dayjs.imports.mjs' + +export function dayjs(...args: Parameters) { + return dayjsImport(...args) +} export function useDayjs() { - return dayjs + return dayjsImport } From eb42b2bf3741eb9b46bc8725fcb0631279791fa4 Mon Sep 17 00:00:00 2001 From: Julian Wowra Date: Sat, 12 Oct 2024 17:19:16 +0200 Subject: [PATCH 2/2] docs: show `dayjs()` composable examples --- README.md | 16 ++++++++++------ playground/app.vue | 8 +++++--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 58f24a6..3423b1b 100644 --- a/README.md +++ b/README.md @@ -55,14 +55,18 @@ You can use the provided `$dayjs` to access Day.js in template. ## Composables -You can use the useDayjs composable to access Day.js anywhere. +You can use the `useDayjs()` and `dayjs()` composables to access Day.js anywhere. -```js +```vue ``` diff --git a/playground/app.vue b/playground/app.vue index 1b13943..1e5e09c 100644 --- a/playground/app.vue +++ b/playground/app.vue @@ -1,12 +1,14 @@