Skip to content

Commit 29db913

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 something tiny like dayjs for date computation (end of week / end of day) and use plain Intl API for formatting. This reduces the bundle size by ~1.5MiB. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 5f33fee commit 29db913

File tree

5 files changed

+30
-34
lines changed

5 files changed

+30
-34
lines changed

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: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
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'
8+
import dayjs from 'dayjs'
99

1010
/**
1111
* Calculates the actual clearAt timestamp
@@ -18,6 +18,8 @@ function getTimestampForClearAt(clearAt) {
1818
return null
1919
}
2020

21+
const formatter = new Intl.DateTimeFormat()
22+
formatter.formatRange(new Date(), )
2123
const date = dateFactory()
2224

2325
if (clearAt.type === 'period') {
@@ -28,7 +30,7 @@ function getTimestampForClearAt(clearAt) {
2830
switch (clearAt.time) {
2931
case 'day':
3032
case 'week':
31-
return Number(moment(date).endOf(clearAt.time).format('X'))
33+
dayjs(date).endOf(clearAt.time).unix()
3234
}
3335
}
3436
// This is not an officially supported type
@@ -65,17 +67,14 @@ function clearAtFormat(clearAt) {
6567
}
6668

6769
if (clearAt.type === 'period') {
68-
return moment.duration(clearAt.time * 1000).humanize()
70+
return formatRelativeTime(Date.now() + clearAt.time * 1000)
6971
}
7072

7173
// This is not an officially supported type
7274
// but only used internally to show the remaining time
7375
// in the Set Status Modal
7476
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()
77+
return formatRelativeTime(clearAt.time * 1000)
7978
}
8079

8180
return null

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@
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",
5857
"@nextcloud/sharing": "^0.3.0",
5958
"@nextcloud/vue": "^9.3.1",
6059
"@vueuse/core": "^14.1.0",
6160
"@vueuse/integrations": "^14.1.0",
61+
"dayjs": "^1.11.19",
6262
"debounce": "^3.0.0",
6363
"pinia": "^3.0.4",
6464
"sortablejs": "^1.15.6",

0 commit comments

Comments
 (0)