feat: better logs & helpful messages

This commit is contained in:
Ayo Ayco 2024-08-17 11:33:39 +02:00
parent 1bd968df58
commit bd3a1fe84c

View file

@ -22,19 +22,19 @@ const ASTROSW = 'astro-sw';
/** /**
* Accepts configuration options with service worker path * Accepts configuration options with service worker path
* and injects needed variables such as `__assets` generated by Astro * and injects needed variables such as `__assets` generated by Astro
* @param {ServiceWorkerConfig} config * @param {ServiceWorkerConfig} options
* @returns {AstroIntegration} * @returns {AstroIntegration}
*/ */
export default function serviceWorker(config) { export default function serviceWorker(options) {
let { let {
assetCachePrefix = ASTROSW, assetCachePrefix = ASTROSW,
assetCacheVersionID = randomUUID(), assetCacheVersionID = randomUUID(),
path: serviceWorkerPath, path: serviceWorkerPath = undefined,
customRoutes = [], customRoutes = [],
excludeRoutes = [], excludeRoutes = [],
logAssets = false, logAssets = false,
esbuild = {} esbuild = {}
} = config; } = options || {};
/** /**
* @type {Array<string>} * @type {Array<string>}
@ -75,8 +75,15 @@ export default function serviceWorker(config) {
return { return {
'name': ASTROSW, 'name': ASTROSW,
'hooks': { 'hooks': {
'astro:config:setup': async ({ injectScript, config, command, logger }) => { 'astro:config:setup': async ({ injectScript, config: _config, command, logger }) => {
output = config.output;
if (!serviceWorkerPath || serviceWorkerPath === '') {
// REQUIRED OPTION IS MISSING
logger.error('Missing required path to service worker script');
}
output = _config.output;
if (command === 'build') { if (command === 'build') {
injectScript('page', registrationScript); injectScript('page', registrationScript);
} }
@ -98,7 +105,9 @@ declare const __prefix: string;`
}, },
'astro:build:done': async ({ dir, routes, pages, logger }) => { 'astro:build:done': async ({ dir, routes, pages, logger }) => {
const outfile = fileURLToPath(new URL('./sw.js', dir)); const outfile = fileURLToPath(new URL('./sw.js', dir));
const swPath = path.join(__dirname, serviceWorkerPath ?? ''); const swPath = (serviceWorkerPath && serviceWorkerPath !== '')
? path.join(__dirname, serviceWorkerPath)
: undefined;
let originalScript; let originalScript;
const _publicFiles = (await readdir(dir, { withFileTypes: true }) ?? []) const _publicFiles = (await readdir(dir, { withFileTypes: true }) ?? [])
@ -147,17 +156,26 @@ declare const __prefix: string;`
&& !_excludeRoutes.includes(asset) && !_excludeRoutes.includes(asset)
); );
logger.info(`${assets.length} assets for caching.`);
if (logAssets) { if (logAssets) {
logger.info('Assets: ' + assets.toString().replaceAll(',', ', ')); logger.info(`\n\n[${ASTROSW}] ${assets.length} assets for caching: \n${assets.toString().replaceAll(',', ',\n ▶ ')}\n`);
} else {
logger.info(`${assets.length} assets for caching.`);
} }
try { try {
logger.info(`Using service worker: ${swPath}`); logger.info(`Using service worker in path: ${swPath}`);
originalScript = await readFile(swPath); originalScript = await readFile(swPath);
} catch { } catch {
logger.error(`Service worker script not found! ${swPath}`) logger.error(`Service worker script not found! ${swPath}`)
if (!swPath) {
logger.error(`
[${ASTROSW}] ERR: The 'path' option is required!
[${ASTROSW}] INFO: Please see service worker options in https://ayco.io/gh/astro-sw#readme
`);
}
} }
const assetsDeclaration = `const __assets = ${JSON.stringify(assets)};\n`; const assetsDeclaration = `const __assets = ${JSON.stringify(assets)};\n`;