feature: client: sw: migrate to ESM

This commit is contained in:
coderiaser 2026-01-27 20:35:38 +02:00
parent 54c89e52f8
commit 27a52d33d0
7 changed files with 19 additions and 15 deletions

View file

@ -11,6 +11,9 @@
"package-json/add-type": "off"
},
"match": {
".filesystem.json": {
"nodejs/rename-file-cjs-to-js": "off"
},
"base64": {
"types/convert-typeof-to-is-type": "off"
},

View file

@ -32,12 +32,12 @@ const options = {
const rules = clean([
!isDev && {
test: /\.js$/,
test: /\.[mc]?js$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
isDev && {
test: /\.js$/,
test: /\.[mc]?js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options,
@ -113,7 +113,7 @@ export default {
'config': `${dirCss}/config.css`,
'terminal': `${dirCss}/terminal.css`,
'user-menu': `${dirCss}/user-menu.css`,
'sw': `${dir}/sw/sw.js`,
'sw': `${dir}/sw/sw.mjs`,
'cloudcmd': `${dir}/cloudcmd.mjs`,
[`${modules}/edit`]: `${dirModules}/edit.js`,
[`${modules}/edit-file`]: `${dirModules}/edit-file.js`,

View file

@ -16,7 +16,7 @@ export async function registerSW(prefix) {
const {serviceWorker} = navigator;
const register = serviceWorker.register.bind(serviceWorker);
const [e, sw] = await tryToCatch(register, `${prefix}/sw.js`);
const [e, sw] = await tryToCatch(register, `${prefix}/sw.mjs`);
if (e)
return null;

View file

@ -84,7 +84,7 @@ test('sw: register: registerSW', async (t, {location, navigator}) => {
const {register} = navigator.serviceWorker;
await registerSW('/hello');
t.calledWith(register, ['/hello/sw.js'], 'should call register');
t.calledWith(register, ['/hello/sw.mjs'], 'should call register');
t.end();
});
@ -98,6 +98,6 @@ test('sw: register: unregisterSW', async (t, {location, navigator}) => {
await unregisterSW('/hello');
t.calledWith(register, ['/hello/sw.js'], 'should call register');
t.calledWith(register, ['/hello/sw.mjs'], 'should call register');
t.end();
});

View file

@ -1,9 +1,7 @@
'use strict';
const process = require('node:process');
const codegen = require('codegen.macro');
const {tryToCatch} = require('try-to-catch');
const currify = require('currify');
import process from 'node:process';
import codegen from 'codegen.macro';
import {tryToCatch} from 'try-to-catch';
import currify from 'currify';
const isDev = process.env.NODE_ENV === 'development';
@ -98,3 +96,4 @@ async function addToCache(request, response) {
const cache = await caches.open(NAME);
return cache.put(request, response);
}

View file

@ -285,10 +285,12 @@ function setUrl(req, res, next) {
function setSW(req, res, next) {
const {url} = req;
const isSW = /^\/sw\.js(\.map)?$/.test(url);
const isSW = /^\/sw\.[mc]?js(\.map)?$/.test(url);
if (isSW)
if (isSW) {
const url = req.url.replace(/[cm]js/, 'js');
req.url = replaceDist(`/dist${url}`);
}
next();
}

View file

@ -173,7 +173,7 @@ test('cloudcmd: getIndexPath: development', (t) => {
});
test('cloudcmd: sw', async (t) => {
const {status} = await request.get('/sw.js');
const {status} = await request.get('/sw.mjs');
t.equal(status, 200, 'should return sw');
t.end();