Skip to content

Commit 8ac1602

Browse files
committed
refactor: drop moment from new bundles
On the Vue 3 side lets remove the dependency on Moment as this is a pretty huge dependency. Instead use plain Intl API for formatting. This reduces the bundle size by ~1.5MiB. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent dac8fe4 commit 8ac1602

5 files changed

Lines changed: 46 additions & 38 deletions

File tree

apps/files_versions/src/components/VersionEntry.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ import type { Version } from '../utils/versions.ts'
139139
import { getCurrentUser } from '@nextcloud/auth'
140140
import { formatFileSize, Permission } from '@nextcloud/files'
141141
import { loadState } from '@nextcloud/initial-state'
142-
import { t } from '@nextcloud/l10n'
143-
import moment from '@nextcloud/moment'
142+
import { getCanonicalLocale, t } from '@nextcloud/l10n'
144143
import { getRootUrl } from '@nextcloud/router'
145144
import { computed, nextTick, ref } from 'vue'
146145
import NcActionButton from '@nextcloud/vue/components/NcActionButton'
@@ -233,7 +232,13 @@ const versionAuthor = computed(() => {
233232
})
234233
235234
const versionHumanExplicitDate = computed(() => {
236-
return moment(props.version.mtime).format('LLLL')
235+
return new Date(props.version.mtime).toLocaleString(
236+
[getCanonicalLocale(), getCanonicalLocale().split('-')[0]!],
237+
{
238+
timeStyle: 'long',
239+
dateStyle: 'long',
240+
},
241+
)
237242
})
238243
239244
const downloadURL = computed(() => {

apps/files_versions/src/utils/versions.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { FileStat, ResponseDataDetailed } from 'webdav'
99
import { getCurrentUser } from '@nextcloud/auth'
1010
import axios from '@nextcloud/axios'
1111
import { getClient } from '@nextcloud/files/dav'
12-
import moment from '@nextcloud/moment'
12+
import { getCanonicalLocale } from '@nextcloud/l10n'
1313
import { encodePath, join } from '@nextcloud/paths'
1414
import { generateRemoteUrl, generateUrl } from '@nextcloud/router'
1515
import davRequest from '../utils/davRequest.ts'
@@ -97,7 +97,7 @@ export async function restoreVersion(version: Version) {
9797
* @param node - The original node
9898
*/
9999
function formatVersion(version: Required<FileStat>, node: INode): Version {
100-
const mtime = moment(version.lastmod).unix() * 1000
100+
const mtime = Date.parse(version.lastmod)
101101
let previewUrl = ''
102102

103103
if (mtime === node.mtime?.getTime()) { // Version is the current one
@@ -119,7 +119,13 @@ function formatVersion(version: Required<FileStat>, node: INode): Version {
119119
author: version.props['version-author'] ? String(version.props['version-author']) : null,
120120
authorName: null,
121121
filename: version.filename,
122-
basename: moment(mtime).format('LLL'),
122+
basename: new Date(mtime).toLocaleString(
123+
[getCanonicalLocale(), getCanonicalLocale().split('-')[0]!],
124+
{
125+
timeStyle: 'long',
126+
dateStyle: 'medium',
127+
},
128+
),
123129
mime: version.mime,
124130
etag: `${version.props.getetag}`,
125131
size: version.size,

apps/user_status/src/services/clearAtService.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
import { t } from '@nextcloud/l10n'
7-
import moment from '@nextcloud/moment'
6+
import { formatRelativeTime, t } from '@nextcloud/l10n'
87
import { dateFactory } from './dateService.js'
98

109
/**
@@ -13,7 +12,7 @@ import { dateFactory } from './dateService.js'
1312
* @param {object | null} clearAt The clear-at config
1413
* @return {number | null}
1514
*/
16-
function getTimestampForClearAt(clearAt) {
15+
export function getTimestampForClearAt(clearAt) {
1716
if (clearAt === null) {
1817
return null
1918
}
@@ -27,8 +26,9 @@ function getTimestampForClearAt(clearAt) {
2726
if (clearAt.type === 'end-of') {
2827
switch (clearAt.time) {
2928
case 'day':
29+
return Math.floor(getEndOfDay(date).getTime() / 1000)
3030
case 'week':
31-
return Number(moment(date).endOf(clearAt.time).format('X'))
31+
return Math.floor(getEndOfWeek(date).getTime() / 1000)
3232
}
3333
}
3434
// This is not an officially supported type
@@ -47,7 +47,7 @@ function getTimestampForClearAt(clearAt) {
4747
* @param {object} clearAt The clearAt object
4848
* @return {string|null}
4949
*/
50-
function clearAtFormat(clearAt) {
50+
export function clearAtFormat(clearAt) {
5151
if (clearAt === null) {
5252
return t('user_status', 'Don\'t clear')
5353
}
@@ -65,23 +65,35 @@ function clearAtFormat(clearAt) {
6565
}
6666

6767
if (clearAt.type === 'period') {
68-
return moment.duration(clearAt.time * 1000).humanize()
68+
return formatRelativeTime(Date.now() + clearAt.time * 1000)
6969
}
7070

7171
// This is not an officially supported type
7272
// but only used internally to show the remaining time
7373
// in the Set Status Modal
7474
if (clearAt.type === '_time') {
75-
const momentNow = moment(dateFactory())
76-
const momentClearAt = moment(clearAt.time, 'X')
77-
78-
return moment.duration(momentNow.diff(momentClearAt)).humanize()
75+
return formatRelativeTime(clearAt.time * 1000)
7976
}
8077

8178
return null
8279
}
8380

84-
export {
85-
clearAtFormat,
86-
getTimestampForClearAt,
81+
/**
82+
* @param {Date} date - The date to calculate the end of the day for
83+
*/
84+
function getEndOfDay(date) {
85+
const endOfDay = new Date(date)
86+
endOfDay.setHours(23, 59, 59, 999)
87+
return endOfDay
88+
}
89+
90+
/**
91+
* Calculates the end of the week for a given date
92+
*
93+
* @param {Date} date - The date to calculate the end of the week for
94+
*/
95+
function getEndOfWeek(date) {
96+
const endOfWeek = getEndOfDay(date)
97+
endOfWeek.setDate(date.getDate() + ((endOfWeek.getFirstDay() - 1 - endOfWeek.getDay() + 7) % 7))
98+
return endOfWeek
8799
}

package-lock.json

Lines changed: 4 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
"@nextcloud/initial-state": "^3.0.0",
5252
"@nextcloud/l10n": "^3.4.1",
5353
"@nextcloud/logger": "^3.0.3",
54-
"@nextcloud/moment": "^1.3.5",
5554
"@nextcloud/password-confirmation": "^6.0.2",
5655
"@nextcloud/paths": "^3.0.0",
5756
"@nextcloud/router": "^3.1.0",

0 commit comments

Comments
 (0)