Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT]: dayjs() as composable #58

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<script setup>
import { useDayjs } from '#dayjs' // not need if you are using auto import
const dayjs = useDayjs()
dayjs.locale('fr')
dayjs.extend(...)
import { useDayjs, dayjs } from '#dayjs' // not need if you are using auto import

// access for dayjs globals
useDayjs().locale('fr')
useDayjs().extend(...)

// access for dayjs instance
console.log(dayjs().format('YYYY-MM-DD HH:mm:ss'))
</script>
```

Expand Down
8 changes: 5 additions & 3 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<script lang="ts" setup>
import { useDayjs } from '#dayjs'
import { dayjs, useDayjs } from '#dayjs'

const dayjs = useDayjs()
const card = false
const icon = false

// access for dayjs instance
console.log(dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss'))
console.log(dayjs().format('YYYY-MM-DD HH:mm:ss'))

// access for dayjs globals
console.log(useDayjs().isDayjs(new Date()))
</script>

<template>
Expand Down
6 changes: 6 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ export default defineNuxtModule<ModuleOptions>({
from: nuxt.options.alias['#dayjs'],
})

addImports({
name: 'dayjs',
as: 'dayjs',
from: nuxt.options.alias['#dayjs'],
})

addTemplate({
filename: 'dayjs.imports.mjs',
getContents: () => generateImports(options),
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/composables/dayjs.d.ts
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions src/runtime/composables/dayjs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import dayjs from '#build/dayjs.imports.mjs'
import dayjsImport from '#build/dayjs.imports.mjs'

export function dayjs(...args: Parameters<typeof dayjsImport>) {
return dayjsImport(...args)
}

export function useDayjs() {
return dayjs
return dayjsImport
}