dayjs-module
dayjs-module copied to clipboard
Nuxt3 Support
If I get more time I will make this a PR.
I have written a DayJS plugin for nuxt3 and so far its working pretty well:
plugnis/dayjs.ts
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime.js'
import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin((nuxtApp) => {
dayjs.extend(relativeTime)
nuxtApp.provide('dayjs', dayjs)
})
declare module '#app' {
interface NuxtApp {
$dayjs: dayjs.Dayjs
}
}
I assume next steps are to transfer this into the template/runtime format and plug in the locale config.
Very much looking forward to you sending me your PR!
we also need to declare:
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$dayjs(date?: dayjs.ConfigType): dayjs.Dayjs
}
}
in order to use it in templates without errors.
Well if you really need to use it now you can just create a plugin
// ~/plugins/dayjs.ts
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import localizedFormat from 'dayjs/plugin/localizedFormat';
import updateLocale from 'dayjs/plugin/updateLocale';
dayjs.extend(relativeTime);
dayjs.extend(localizedFormat);
dayjs.extend(updateLocale);
// OTHER PLUGINS
export default defineNuxtPlugin(() => {
return {
provide: {
dayjs,
},
};
});
<script setup lang="ts">
const { $dayjs } = useNuxtApp();
</script>
<template>
<div>
Now: {{ $dayjs() }}
</div>
</template>
so when using this plugin, when i try to run yarn build
or yarn generate
then yarn preview
i get this error:
[nuxt] [request error] Cannot find module '/Users/thejacer87/Development/safecom-fmeuc/.output/server/node_modules/dayjs/plugin/utc' imported from /Users/thejacer87/Development/safecom-fmeuc/.output/server/chunks/app/server.mjs
Did you mean to import dayjs/plugin/utc.js?
at new NodeError (node:internal/errors:371:5)
Not sure if it's a nuxt issue or dayjs?
so when using this plugin, when i try to run
yarn build
oryarn generate
thenyarn preview
i get this error:[nuxt] [request error] Cannot find module '/Users/thejacer87/Development/safecom-fmeuc/.output/server/node_modules/dayjs/plugin/utc' imported from /Users/thejacer87/Development/safecom-fmeuc/.output/server/chunks/app/server.mjs Did you mean to import dayjs/plugin/utc.js? at new NodeError (node:internal/errors:371:5)
Not sure if it's a nuxt issue or dayjs?
I haven't tested my version of the nuxt3 plugin with a static-compiled version yet - maybe try adding the .js
?
We need it.thanks!
I've created a Nuxt 3 module using the official Nuxt 3 module-builder: https://github.com/nathanchase/dayjs/
It's on npm currently at: https://www.npmjs.com/package/@nathanchase/nuxt-dayjs-module
It's barebones, and some of the plugins I use are hardcoded into it. Someone else might be able to make it more modular to use the same dayjs
configuration object that the Nuxt 2 dayjs-module uses to specify plugins and other options?
I wasn't sure the protocol of how to submit this as a PR for this module, since it's vastly different. You can check the playground for examples of the module working.
Take a look @potato4d, and see what you think.
I've created a Nuxt 3 module using the official Nuxt 3 module-builder: https://github.com/nathanchase/dayjs/
Well, I'm having some issue when actually using the module in Nuxt 3 where I get an error:
Uncaught SyntaxError: The requested module '/_nuxt/node_modules/.pnpm/[email protected]/node_modules/dayjs/dayjs.min.js?v=e84ee87d' does not provide an export named 'default' (at plugin.mjs:1:8)
, although the module's playground itself works perfectly fine...
OK! Got it working! v1.0.7 - Now also supports configuration in nuxt.config, like:
dayjs: {
plugins: [
'duration',
'relativeTime',
'advancedFormat',
'weekday'
]
}
working at https://github.com/nuxt-community/dayjs-module/tree/next
Hi @potato4d! I was working on a Nuxt 3 version of this module a while ago. Would you be interested in taking a look?
It has useDayjs()
composable and auto register of dayjs types.
@potato4d I sure am interested. Any status on this ? The doc does not mention anything yet so i assume it is still in the making
working at https://github.com/nuxt-community/dayjs-module/tree/next
9 month without any update in the branch. Is it still maintained?
I am working on an official nuxt3 module that will be live next week @Dodje, i will close this issue with its URL ASAP
@Dodje @Developer27149 @safejace @avxkim
https://github.com/fumeapp/dayjs