.
This commit is contained in:
parent
8ad68103bc
commit
21a1b27d35
8 changed files with 238 additions and 5 deletions
108
src/dayjs26.ts
Executable file
108
src/dayjs26.ts
Executable file
|
|
@ -0,0 +1,108 @@
|
|||
/**
|
||||
* 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 "dayjs/locale/pt-br"; // Importante: importar o locale!
|
||||
*
|
||||
* import { defineDayjsBr } from "@comuns/src/dayjs26";
|
||||
*
|
||||
* export const dayjsbr = defineDayjsBr({
|
||||
* dayjs,
|
||||
* duration,
|
||||
* isSameOrAfter,
|
||||
* isSameOrBefore,
|
||||
* minMax,
|
||||
* relativeTime,
|
||||
* timezone,
|
||||
* utc,
|
||||
* weekOfYear,
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
const defineDayjsBr = ({
|
||||
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
|
||||
}
|
||||
|
||||
export { defineDayjsBr }
|
||||
|
||||
export type { Dayjs }
|
||||
|
|
@ -3,6 +3,7 @@ export * from "./cacheMemoria"
|
|||
export * from "./constantes"
|
||||
export * from "./consulta"
|
||||
export * from "./dayjs"
|
||||
export * from "./dayjs26"
|
||||
export * from "./ecosistema"
|
||||
export * from "./extensoes"
|
||||
export * from "./extensoes"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue