From 1f174870ab3d6a13b6e8afaf884a1c7d749fb362 Mon Sep 17 00:00:00 2001 From: coderiaser Date: Mon, 2 Feb 2026 19:07:05 +0200 Subject: [PATCH] feature: client: view: migrate to ESM --- .webpack/js.mjs | 2 +- .../view/{get-type.js => get-type.mjs} | 5 +- client/modules/view/{index.js => index.mjs} | 73 +++++++++---------- .../view/{index.spec.js => index.spec.mjs} | 18 ++--- client/modules/view/{types.js => types.mjs} | 18 ++--- .../view/{types.spec.js => types.spec.mjs} | 6 +- 6 files changed, 54 insertions(+), 68 deletions(-) rename client/modules/view/{get-type.js => get-type.mjs} (92%) rename client/modules/view/{index.js => index.mjs} (87%) rename client/modules/view/{index.spec.js => index.spec.mjs} (89%) rename client/modules/view/{types.js => types.mjs} (79%) rename client/modules/view/{types.spec.js => types.spec.mjs} (89%) diff --git a/.webpack/js.mjs b/.webpack/js.mjs index 2b03fd6a..434a397c 100644 --- a/.webpack/js.mjs +++ b/.webpack/js.mjs @@ -125,7 +125,7 @@ export default { [`${modules}/edit-names`]: `${dirModules}/edit-names.js`, [`${modules}/edit-names-vim`]: `${dirModules}/edit-names-vim.js`, [`${modules}/menu`]: `${dirModules}/menu/index.mjs`, - [`${modules}/view`]: `${dirModules}/view/index.js`, + [`${modules}/view`]: `${dirModules}/view/index.mjs`, [`${modules}/help`]: `${dirModules}/help.js`, [`${modules}/markdown`]: `${dirModules}/markdown.js`, [`${modules}/config`]: `${dirModules}/config/index.mjs`, diff --git a/client/modules/view/get-type.js b/client/modules/view/get-type.mjs similarity index 92% rename from client/modules/view/get-type.js rename to client/modules/view/get-type.mjs index 9fc1df1d..a061399f 100644 --- a/client/modules/view/get-type.js +++ b/client/modules/view/get-type.mjs @@ -1,6 +1,5 @@ -'use strict'; +import currify from 'currify'; -const currify = require('currify'); const testRegExp = currify((name, reg) => reg.test(name)); const getRegExp = (ext) => RegExp(`\\.${ext}$`, 'i'); @@ -8,7 +7,7 @@ const isPDF = (a) => /\.pdf$/i.test(a); const isHTML = (a) => a.endsWith('.html'); const isMarkdown = (a) => /.\.md$/.test(a); -module.exports = (name) => { +export default (name) => { if (isPDF(name)) return 'pdf'; diff --git a/client/modules/view/index.js b/client/modules/view/index.mjs similarity index 87% rename from client/modules/view/index.js rename to client/modules/view/index.mjs index 47724798..91d9caf6 100644 --- a/client/modules/view/index.js +++ b/client/modules/view/index.mjs @@ -1,35 +1,26 @@ /* global CloudCmd, DOM */ - -'use strict'; - -const CloudCmd = globalThis.CloudCmd || {}; -const DOM = globalThis.DOM || {}; - -require('../../../css/view.css'); - -const rendy = require('rendy'); -const currify = require('currify'); -const wraptile = require('wraptile'); -const {tryToCatch} = require('try-to-catch'); -const load = require('load.js'); - -const _modal = require('@cloudcmd/modal'); -const _createElement = require('@cloudcmd/create-element'); - -const {time} = require('#common/util'); -const {FS} = require('../../../common/cloudfunc.mjs'); - -const { +import rendy from 'rendy'; +import currify from 'currify'; +import wraptile from 'wraptile'; +import {tryToCatch} from 'try-to-catch'; +import load from 'load.js'; +import * as _modal from '@cloudcmd/modal'; +import _createElement from '@cloudcmd/create-element'; +import {time} from '#common/util'; +import * as Files from '#dom/files'; +import * as Events from '#dom/events'; +import '../../../css/view.css'; +import {FS} from '../../../common/cloudfunc.mjs'; +import { isImage, isAudio, getType, -} = require('./types'); +} from './types.mjs'; +import * as Images from '../../dom/images.mjs'; +import {encode} from '../../../common/entity.js'; -const Files = require('#dom/files'); -const Events = require('#dom/events'); -const Images = require('../../dom/images.mjs'); - -const {encode} = require('../../../common/entity'); +const CloudCmd = globalThis.CloudCmd || {}; +const DOM = globalThis.DOM || {}; const isString = (a) => typeof a === 'string'; const {assign} = Object; const {isArray} = Array; @@ -47,14 +38,15 @@ const addEvent = lifo(Events.add); const loadCSS = load.css; -module.exports.show = show; -module.exports.hide = hide; - let Loading = false; const Name = 'View'; -CloudCmd[Name] = module.exports; +CloudCmd[Name] = { + init, + show, + hide, +}; const Info = DOM.CurrentInfo; const {Key} = CloudCmd; @@ -91,9 +83,9 @@ const Config = { }, }; -module.exports._Config = Config; +export const _Config = Config; -module.exports.init = async () => { +export async function init() { await loadAll(); const events = [ @@ -105,9 +97,9 @@ module.exports.init = async () => { Overlay, onOverlayClick, )); -}; +} -async function show(data, options = {}) { +export async function show(data, options = {}) { const prefixURL = CloudCmd.prefixURL + FS; if (Loading) @@ -159,7 +151,8 @@ async function show(data, options = {}) { } } -module.exports._createIframe = createIframe; +export const _createIframe = createIframe; + function createIframe(src, overrides = {}) { const { createElement = _createElement, @@ -178,7 +171,8 @@ function createIframe(src, overrides = {}) { return element; } -module.exports._viewHtml = viewHtml; +export const _viewHtml = viewHtml; + function viewHtml(src, overrides = {}) { const {modal = _modal} = overrides; modal.open(createIframe(src), Config); @@ -234,7 +228,8 @@ async function viewFile() { const copy = (a) => assign({}, a); -module.exports._initConfig = initConfig; +export const _initConfig = initConfig; + function initConfig(options) { const config = copy(Config); @@ -260,7 +255,7 @@ function initConfig(options) { return config; } -function hide() { +export function hide() { _modal.close(); } diff --git a/client/modules/view/index.spec.js b/client/modules/view/index.spec.mjs similarity index 89% rename from client/modules/view/index.spec.js rename to client/modules/view/index.spec.mjs index 673214c9..585ac3e2 100644 --- a/client/modules/view/index.spec.js +++ b/client/modules/view/index.spec.mjs @@ -1,18 +1,14 @@ -'use strict'; - -require('css-modules-require-hook/preset'); - -const autoGlobals = require('auto-globals'); -const {stub} = require('@cloudcmd/stub'); -const {test: tape} = require('supertape'); -const test = autoGlobals(tape); - -const { +import autoGlobals from 'auto-globals'; +import {stub} from '@cloudcmd/stub'; +import {test as tape} from 'supertape'; +import { _initConfig, _viewHtml, _Config, _createIframe, -} = require('.'); +} from './index.mjs'; + +const test = autoGlobals(tape); test('cloudcmd: client: view: initConfig', (t) => { let config; diff --git a/client/modules/view/types.js b/client/modules/view/types.mjs similarity index 79% rename from client/modules/view/types.js rename to client/modules/view/types.mjs index d94dd971..2e001fe1 100644 --- a/client/modules/view/types.js +++ b/client/modules/view/types.mjs @@ -1,8 +1,8 @@ -'use strict'; +import {extname} from 'node:path'; +import currify from 'currify'; + +export const isAudio = (name) => /\.(mp3|ogg|m4a)$/i.test(name); -const {extname} = require('node:path'); -const currify = require('currify'); -const isAudio = (name) => /\.(mp3|ogg|m4a)$/i.test(name); const testRegExp = currify((name, reg) => reg.test(name)); const getRegExp = (ext) => RegExp(`\\.${ext}$`, 'i'); @@ -10,7 +10,7 @@ const isPDF = (a) => /\.pdf$/i.test(a); const isHTML = (a) => a.endsWith('.html'); const isMarkdown = (a) => /.\.md$/.test(a); -module.exports.getType = async (path) => { +export const getType = async (path) => { const ext = extname(path); if (!ext) @@ -32,8 +32,7 @@ module.exports.getType = async (path) => { return 'markdown'; }; -module.exports.isImage = isImage; -function isImage(name) { +export function isImage(name) { const images = [ 'jp(e|g|eg)', 'gif', @@ -53,13 +52,12 @@ function isMedia(name) { return isAudio(name) || isVideo(name); } -module.exports.isAudio = isAudio; - function isVideo(name) { return /\.(mp4|avi|webm)$/i.test(name); } -module.exports._detectType = detectType; +export const _detectType = detectType; + async function detectType(path) { const {headers} = await fetch(path, { method: 'HEAD', diff --git a/client/modules/view/types.spec.js b/client/modules/view/types.spec.mjs similarity index 89% rename from client/modules/view/types.spec.js rename to client/modules/view/types.spec.mjs index d8004f9e..58a06672 100644 --- a/client/modules/view/types.spec.js +++ b/client/modules/view/types.spec.mjs @@ -1,7 +1,5 @@ -'use strict'; - -const {test, stub} = require('supertape'); -const {isAudio, _detectType} = require('./types'); +import {test, stub} from 'supertape'; +import {isAudio, _detectType} from './types.mjs'; test('cloudcmd: client: view: types: isAudio', (t) => { const result = isAudio('hello.mp3');