├── .editorconfig ├── .eslintignore ├── .eslintrc ├── .github └── dependabot.yml ├── .gitignore ├── CHANGELOG.md ├── README.md ├── package-lock.json ├── package.json ├── playground ├── app.vue ├── nuxt.config.ts └── package.json ├── src ├── gen.ts ├── module.ts └── runtime │ └── plugin.ts └── tsconfig.json /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_size = 2 5 | indent_style = space 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules 3 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "@nuxtjs/eslint-config-typescript" 4 | ], 5 | "rules": { 6 | "@typescript-eslint/no-unused-vars": [ 7 | "off" 8 | ] 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "npm" 9 | directory: "/" # Location of package manifests 10 | schedule: 11 | interval: "weekly" 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Dependencies 2 | node_modules 3 | 4 | # Logs 5 | *.log* 6 | 7 | # Temp directories 8 | .temp 9 | .tmp 10 | .cache 11 | 12 | # Yarn 13 | **/.yarn/cache 14 | **/.yarn/*state* 15 | 16 | # Generated dirs 17 | dist 18 | 19 | # Nuxt 20 | .nuxt 21 | .output 22 | .vercel_build_output 23 | .build-* 24 | .env 25 | .netlify 26 | 27 | # Env 28 | .env 29 | 30 | # Testing 31 | reports 32 | coverage 33 | *.lcov 34 | .nyc_output 35 | 36 | # VSCode 37 | .vscode 38 | 39 | # Intellij idea 40 | *.iml 41 | .idea 42 | 43 | # OSX 44 | .DS_Store 45 | .AppleDouble 46 | .LSOverride 47 | .AppleDB 48 | .AppleDesktop 49 | Network Trash Folder 50 | Temporary Items 51 | .apdisk 52 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | #### 1.0.11 (2022-09-06) 2 | 3 | - set prerelease compat constraint to ^3.0.0-rc.9 4 | 5 | ##### Chores 6 | 7 | * version bump & changelog ([be386ed6](https://github.com/nathanchase/dayjs/commit/be386ed6732f2f9fe30aa85303b6556f1a37205b)) 8 | * lockfile ([efd1dfb2](https://github.com/nathanchase/dayjs/commit/efd1dfb2948732d7851fe8b53ac264d76333a1f5)) 9 | * **deps:** update dependencies ([51c239ce](https://github.com/nathanchase/dayjs/commit/51c239ce3cb9e61d64fb52826ed29919ea37a367)) 10 | 11 | ##### Other Changes 12 | 13 | * //github.com/nathanchase/dayjs ([8d510d47](https://github.com/nathanchase/dayjs/commit/8d510d47757352c1cdbad1330a1d29cafbfdb748)) 14 | 15 | #### 1.0.10 (2022-08-16) 16 | 17 | - remove unneeded #app interface 18 | 19 | ##### Chores 20 | 21 | * lockfile ([efd1dfb2](https://github.com/nathanchase/dayjs/commit/efd1dfb2948732d7851fe8b53ac264d76333a1f5)) 22 | * **deps:** update dependencies ([51c239ce](https://github.com/nathanchase/dayjs/commit/51c239ce3cb9e61d64fb52826ed29919ea37a367)) 23 | 24 | ##### Other Changes 25 | 26 | * //github.com/nathanchase/dayjs ([8d510d47](https://github.com/nathanchase/dayjs/commit/8d510d47757352c1cdbad1330a1d29cafbfdb748)) 27 | 28 | #### 1.0.9 (2022-08-16) 29 | 30 | - explicitly import esm module 31 | 32 | ##### Chores 33 | 34 | * lockfile ([efd1dfb2](https://github.com/nathanchase/dayjs/commit/efd1dfb2948732d7851fe8b53ac264d76333a1f5)) 35 | * **deps:** update dependencies ([51c239ce](https://github.com/nathanchase/dayjs/commit/51c239ce3cb9e61d64fb52826ed29919ea37a367)) 36 | 37 | ##### Other Changes 38 | 39 | * //github.com/nathanchase/dayjs ([8d510d47](https://github.com/nathanchase/dayjs/commit/8d510d47757352c1cdbad1330a1d29cafbfdb748)) 40 | 41 | #### 1.0.8 (2022-08-16) 42 | 43 | ##### Chores 44 | 45 | * lockfile ([efd1dfb2](https://github.com/nathanchase/dayjs/commit/efd1dfb2948732d7851fe8b53ac264d76333a1f5)) 46 | * **deps:** update dependencies ([51c239ce](https://github.com/nathanchase/dayjs/commit/51c239ce3cb9e61d64fb52826ed29919ea37a367)) 47 | 48 | ##### Other Changes 49 | 50 | * //github.com/nathanchase/dayjs ([8d510d47](https://github.com/nathanchase/dayjs/commit/8d510d47757352c1cdbad1330a1d29cafbfdb748)) 51 | 52 | #### 1.0.7 (2022-07-25) 53 | 54 | - ensure plugins imported from *.js files 55 | 56 | #### 1.0.6 (2022-07-25) 57 | 58 | - created configurability for dayjs 59 | 60 | #### 1.0.5 (2022-07-25) 61 | 62 | - WIP - resolving issues 63 | 64 | #### 1.0.3 (2022-07-25) 65 | 66 | - forgot to comment out plugins everywhere 67 | 68 | #### 1.0.2 (2022-07-25) 69 | 70 | - comment out plugins that aren't exporting default 71 | 72 | #### 1.0.1 (2022-07-25) 73 | 74 | - move dayjs to dependencies 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @nathanchase/nuxt-dayjs-module 2 | 3 | This is a Nuxt 3 compatible module built with the official Nuxt 3 [module-builder](https://github.com/nuxt/module-builder) for [dayjs](https://github.com/iamkun/dayjs/). This is meant as a Nuxt 3 version of [dayjs-module](https://github.com/nuxt-community/dayjs-module) to satisfy [Nuxt 3 support](https://github.com/nuxt-community/dayjs-module/issues/376). 4 | 5 | Usage: 6 | ```js 7 | // ./nuxt.config.ts 8 | export default defineNuxtConfig({ 9 | modules: [ 10 | '@nathanchase/nuxt-dayjs-module' 11 | ] 12 | ... 13 | }); 14 | ``` 15 | 16 | Add dayjs plugins (ex: [duration](https://day.js.org/docs/en/plugin/duration)) via configuration in nuxt.config like so: 17 | ```js 18 | dayjs: { 19 | plugins: [ 20 | 'duration', 21 | 'relativeTime', 22 | 'advancedFormat', 23 | 'weekday' 24 | ] 25 | } 26 | ``` 27 | 28 | See [/playground/app.vue](https://github.com/nathanchase/dayjs/blob/master/playground/app.vue) for working examples. 29 | 30 | On npm: [https://www.npmjs.com/package/@nathanchase/nuxt-dayjs-module](https://www.npmjs.com/package/@nathanchase/nuxt-dayjs-module) 31 | 32 | ## Development 33 | 34 | - Run 'npm install' to install required packages. 35 | - Run `npm run dev:prepare` to generate type stubs. 36 | - Use `npm run dev` to start [playground](./playground) in development mode. 37 | ``` 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@nathanchase/nuxt-dayjs-module", 3 | "version": "1.0.14", 4 | "private": false, 5 | "description": "This is a Nuxt 3 compatible module built with the official Nuxt 3 (module-builder)(https://github.com/nuxt/module-builder) for (dayjs)(https://github.com/iamkun/dayjs/).", 6 | "keywords": [ 7 | "dayjs", 8 | "nuxt", 9 | "nuxt3" 10 | ], 11 | "homepage": "https://github.com/nathanchase/dayjs#readme", 12 | "bugs": { 13 | "url": "https://github.com/nathanchase/dayjs/issues" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "git+https://github.com/nathanchase/dayjs.git" 18 | }, 19 | "license": "MIT", 20 | "author": "Nathan Chase", 21 | "type": "module", 22 | "exports": { 23 | ".": { 24 | "import": "./dist/module.mjs", 25 | "require": "./dist/module.cjs" 26 | } 27 | }, 28 | "main": "./dist/module.cjs", 29 | "types": "./dist/types.d.ts", 30 | "files": [ 31 | "dist" 32 | ], 33 | "scripts": { 34 | "dev": "nuxi dev playground", 35 | "dev:build": "nuxi build playground", 36 | "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground", 37 | "prepack": "nuxt-module-build" 38 | }, 39 | "dependencies": { 40 | "@nuxt/kit": "3.15.4", 41 | "dayjs": "^1.11.5" 42 | }, 43 | "devDependencies": { 44 | "@nuxt/module-builder": "latest", 45 | "@nuxtjs/eslint-config-typescript": "latest", 46 | "@types/node": "22.13.1", 47 | "eslint": "latest", 48 | "nuxt": "^3.0.0-rc.9" 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /playground/app.vue: -------------------------------------------------------------------------------- 1 | 72 | 73 | 98 | -------------------------------------------------------------------------------- /playground/nuxt.config.ts: -------------------------------------------------------------------------------- 1 | import dayjsModule from '..' 2 | 3 | export default defineNuxtConfig({ 4 | modules: [ 5 | dayjsModule 6 | ], 7 | dayjs: { 8 | locales: ['en', 'es', 'pt'], 9 | 10 | defaultLocale: 'en', 11 | plugins: [ 12 | 'advancedFormat', 13 | 'customParseFormat', 14 | 'utc', 15 | 'timezone', 16 | 'relativeTime', 17 | 'localizedFormat', 18 | 'localeData', 19 | 'isToday', 20 | 'updateLocale', 21 | 'weekday', 22 | 'dayOfYear', 23 | 'duration', 24 | 'isBetween', 25 | 'isLeapYear', 26 | 'isSameOrAfter', 27 | 'isSameOrBefore' 28 | ] 29 | } 30 | }) 31 | -------------------------------------------------------------------------------- /playground/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "name": "my-module-playground" 4 | } 5 | -------------------------------------------------------------------------------- /src/gen.ts: -------------------------------------------------------------------------------- 1 | import { ModuleOptions } from './module' 2 | 3 | export const generateConfigContents = ({ plugins, defaultLocale, locales, defaultTimeZone }: ModuleOptions) => { 4 | const shouldSetDefaultLocale = !!defaultLocale 5 | const shouldSetDefaultTimeZone = !!defaultTimeZone 6 | const shouldImportDayJs = !!plugins.length || shouldSetDefaultLocale || shouldSetDefaultTimeZone 7 | 8 | return `// Generated by @nuxtjs/dayjs 9 | ${shouldImportDayJs ? "import dayjs from 'dayjs/esm/index.js'" : ''} 10 | ${locales.map(l => `import 'dayjs/esm/locale/${l}'`).join('\n')} 11 | ${shouldSetDefaultLocale ? `dayjs.locale('${defaultLocale}')` : ''} 12 | ${plugins.map((p) => { 13 | const _p = renamePlugin(p) 14 | return `import ${_p} from 'dayjs/esm/plugin/${p}/index.js'\ndayjs.extend(${_p})` 15 | }).join('\n')} 16 | ${shouldSetDefaultTimeZone ? `dayjs.tz.setDefault('${defaultTimeZone}')` : ''} 17 | ` 18 | } 19 | 20 | function renamePlugin (plugin: string) { 21 | return plugin.replace(/[^A-Za-z]/g, '_') 22 | } 23 | -------------------------------------------------------------------------------- /src/module.ts: -------------------------------------------------------------------------------- 1 | import { resolve } from 'node:path' 2 | import { fileURLToPath } from 'node:url' 3 | import { defineNuxtModule, addPlugin, addTemplate } from '@nuxt/kit' 4 | import { generateConfigContents } from './gen' 5 | 6 | export interface ModuleOptions { 7 | locales: string[]; 8 | defaultLocale: string | null; 9 | plugins: string[]; 10 | defaultTimeZone: string | null; 11 | } 12 | 13 | export default defineNuxtModule({ 14 | meta: { 15 | name: '@nathanchase/nuxt-dayjs-module', 16 | configKey: 'dayjs', 17 | compatibility: { 18 | nuxt: '^3.0.0-rc.9' 19 | } 20 | }, 21 | 22 | defaults: { 23 | locales: [], 24 | defaultLocale: null, 25 | plugins: [], 26 | defaultTimeZone: null 27 | }, 28 | 29 | async setup (options, nuxt) { 30 | const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url)) 31 | await nuxt.options.build.transpile.push(runtimeDir) 32 | await addPlugin(resolve(runtimeDir, 'plugin')) 33 | await addTemplate({ 34 | filename: 'dayjs.config.mjs', 35 | getContents: () => generateConfigContents(options) 36 | }) 37 | } 38 | }) 39 | -------------------------------------------------------------------------------- /src/runtime/plugin.ts: -------------------------------------------------------------------------------- 1 | import { defineNuxtPlugin } from '#app' 2 | import '#build/dayjs.config.mjs' 3 | import dayjs from 'dayjs/esm/index.js' 4 | 5 | declare module '#app' { 6 | interface NuxtApp { 7 | $dayjs: typeof dayjs 8 | } 9 | } 10 | declare module 'vue' { 11 | interface ComponentCustomProperties { 12 | $dayjs: typeof dayjs 13 | } 14 | } 15 | declare module '@vue/runtime-core' { 16 | interface ComponentCustomProperties { 17 | $dayjs: typeof dayjs 18 | } 19 | } 20 | 21 | export default defineNuxtPlugin((nuxtApp) => { 22 | nuxtApp.provide('dayjs', dayjs); 23 | }) 24 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./playground/.nuxt/tsconfig.json", 3 | "compilerOptions": { 4 | "types": [ 5 | "dayjs", 6 | "dayjs/plugin/duration", 7 | "dayjs/plugin/relativeTime", 8 | "dayjs/plugin/advancedFormat", 9 | "dayjs/plugin/weekday" 10 | ] 11 | } 12 | } 13 | --------------------------------------------------------------------------------