110 lines
3.6 KiB
TypeScript
Executable file
110 lines
3.6 KiB
TypeScript
Executable file
/**
|
|
* Utilitário de configuração do Dayjs focado em compatibilidade com SSR.
|
|
*
|
|
* PROBLEMA:
|
|
* A importação direta do `dayjs` e seus plugins frequentemente causa conflitos em ambientes
|
|
* de Renderização do Lado do Servidor (SSR), como Nuxt ou Next.js, devido a discrepâncias
|
|
* na resolução de módulos (ESM vs CJS) e instabilidades de importação.
|
|
*
|
|
* SOLUÇÃO:
|
|
* Este módulo utiliza o padrão de Injeção de Dependência. Ele expõe apenas tipagens e
|
|
* uma função de configuração (`defineDayjsBr`). A responsabilidade de importar as
|
|
* bibliotecas "vivas" é delegada à aplicação consumidora (o cliente da função).
|
|
*
|
|
* Isso permite que o bundler da aplicação principal (Vite, Webpack, etc.) gerencie as
|
|
* instâncias, garantindo consistência e evitando erros de "module not found" ou
|
|
* instâncias duplicadas/não inicializadas adequadamente.
|
|
*/
|
|
|
|
import type _dayjs from "dayjs"
|
|
import type { Dayjs } from "dayjs"
|
|
|
|
export type { ManipulateType } from "dayjs"
|
|
|
|
// Importação apenas de TIPOS para evitar bundling indesejado neste arquivo
|
|
import type _duration from "dayjs/plugin/duration"
|
|
import type _isSameOrAfter from "dayjs/plugin/isSameOrAfter"
|
|
import type _isSameOrBefore from "dayjs/plugin/isSameOrBefore"
|
|
import type _minMax from "dayjs/plugin/minMax"
|
|
import type _relativeTime from "dayjs/plugin/relativeTime"
|
|
import type _timezone from "dayjs/plugin/timezone"
|
|
import type _utc from "dayjs/plugin/utc"
|
|
import type _weekOfYear from "dayjs/plugin/weekOfYear"
|
|
|
|
/**
|
|
* Inicializa e configura o Dayjs com o locale 'pt-br' e plugins essenciais.
|
|
*
|
|
* MODO DE USO:
|
|
* Importe os pacotes reais na sua aplicação e passe-os para esta função.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* // Em seu arquivo de configuração (ex: plugins/dayjs.ts):
|
|
* import dayjs from "dayjs"
|
|
* import duration from "dayjs/plugin/duration"
|
|
* import isSameOrAfter from "dayjs/plugin/isSameOrAfter"
|
|
* import isSameOrBefore from "dayjs/plugin/isSameOrBefore"
|
|
* import minMax from "dayjs/plugin/minMax"
|
|
* import relativeTime from "dayjs/plugin/relativeTime"
|
|
* import timezone from "dayjs/plugin/timezone"
|
|
* import utc from "dayjs/plugin/utc"
|
|
* import weekOfYear from "dayjs/plugin/weekOfYear"
|
|
* import { defineDayjsBr } from "p-comuns"
|
|
* import "dayjs/locale/pt-br" // Importante: importar o locale!
|
|
|
|
* export const dayjsbr = defineDayjsBr({
|
|
* dayjs,
|
|
* duration,
|
|
* isSameOrAfter,
|
|
* isSameOrBefore,
|
|
* minMax,
|
|
* relativeTime,
|
|
* timezone,
|
|
* utc,
|
|
* weekOfYear,
|
|
* })
|
|
* ```
|
|
*/
|
|
const iniciarDayjsBr = ({
|
|
dayjs,
|
|
duration,
|
|
isSameOrAfter,
|
|
isSameOrBefore,
|
|
minMax,
|
|
relativeTime,
|
|
timezone,
|
|
utc,
|
|
weekOfYear,
|
|
}: {
|
|
dayjs: typeof _dayjs
|
|
duration: typeof _duration
|
|
isSameOrAfter: typeof _isSameOrAfter
|
|
isSameOrBefore: typeof _isSameOrBefore
|
|
minMax: typeof _minMax
|
|
relativeTime: typeof _relativeTime
|
|
timezone: typeof _timezone
|
|
utc: typeof _utc
|
|
weekOfYear: typeof _weekOfYear
|
|
}) => {
|
|
// Extensão da biblioteca com os plugins fornecidos
|
|
dayjs.extend(utc)
|
|
dayjs.extend(timezone)
|
|
dayjs.extend(weekOfYear)
|
|
dayjs.extend(isSameOrBefore)
|
|
dayjs.extend(isSameOrAfter)
|
|
dayjs.extend(minMax)
|
|
dayjs.extend(relativeTime)
|
|
dayjs.extend(duration)
|
|
|
|
// Definição do locale global
|
|
dayjs.locale("pt-br")
|
|
|
|
return dayjs
|
|
}
|
|
/**
|
|
* @deprecated Use iniciarDayjsBr em vez de definirDayjsBr. O método defineDayjsBr está obsoleto e será removido em futuras versões.
|
|
*/
|
|
const defineDayjsBr = iniciarDayjsBr
|
|
|
|
export type { Dayjs }
|
|
export { defineDayjsBr, iniciarDayjsBr }
|