From cb948b82ba6e321e4b244de0865cd98d9ad6299d Mon Sep 17 00:00:00 2001 From: Erin Date: Fri, 28 Jul 2023 04:41:15 -0400 Subject: [PATCH] give manifest searching a function --- index.mjs | 103 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 46 deletions(-) diff --git a/index.mjs b/index.mjs index 1c4665e..28eea7f 100644 --- a/index.mjs +++ b/index.mjs @@ -46,58 +46,69 @@ export function createConfig (manifestPathRelative, options, createConfig) { return join(outputDir, platform, getOutputFilename(entryPath, ext)); } - const scriptEntrypointAbsolutePaths = []; - const styleEntrypointAbsolutePathss = []; - const otherAssetAbsolutePaths = []; - // Gather all JS entry points specified in the manifest - (manifestContent.content_scripts || []).forEach(({css, js}) => { - css && css.forEach((filename, i) => { - const id = resolve(manifestDirname, filename); - styleEntrypointAbsolutePathss.push(id); - css.splice(i, 1, getOutputFilename(id, 'css')); + /** Scans a manifest for entrypoints */ + function getEntryPointsFromManifest (manifestContent) { + const scriptEntrypointAbsolutePaths = []; + const styleEntrypointAbsolutePathss = []; + const otherAssetAbsolutePaths = []; + // Gather all JS entry points specified in the manifest + (manifestContent.content_scripts || []).forEach(({css, js}) => { + css && css.forEach((filename, i) => { + const id = resolve(manifestDirname, filename); + styleEntrypointAbsolutePathss.push(id); + css.splice(i, 1, getOutputFilename(id, 'css')); + }); + js && js.forEach((filename, i) => { + const id = resolve(manifestDirname, filename); + scriptEntrypointAbsolutePaths.push(id); + js.splice(i, 1, getOutputFilename(id, 'js')); + }); }); - js && js.forEach((filename, i) => { + manifestContent.background?.scripts && manifestContent.background.scripts.forEach((filename, i) => { const id = resolve(manifestDirname, filename); scriptEntrypointAbsolutePaths.push(id); - js.splice(i, 1, getOutputFilename(id, 'js')); + manifestContent.background.scripts.splice(i, 1, getOutputFilename(id)); }); - }); - manifestContent.background?.scripts && manifestContent.background.scripts.forEach((filename, i) => { - const id = resolve(manifestDirname, filename); - scriptEntrypointAbsolutePaths.push(id); - manifestContent.background.scripts.splice(i, 1, getOutputFilename(id)); - }); - if (manifestContent.background?.service_worker) { - const id = resolve(manifestDirname, manifestContent.background.service_worker); - scriptEntrypointAbsolutePaths.push(id); - manifestContent.background.service_worker = getOutputFilename(id); - } - manifestContent.icons && Object.entries(manifestContent.icons).forEach(([size, filename]) => { - const id = resolve(manifestDirname, filename); - // console.log('icon:', size, filename, getOutputFilename(id)); - otherAssetAbsolutePaths.push(id); - manifestContent.icons[size] = getOutputFilename(id); - }); - (manifestContent.web_accessible_resources || []).forEach((entry, i) => { - if (typeof entry === 'string') { - // mv2 - single top-level array of items - const id = resolve(manifestDirname, entry); - otherAssetAbsolutePaths.push(id); - manifestContent.web_accessible_resources.splice(i, 1, getOutputFilename(id)); - } else { - // mv3 - array of objects with `resources` keys - const {resources} = entry; - resources && resources.forEach((filename, j) => { - const id = resolve(manifestDirname, filename); - otherAssetAbsolutePaths.push(id); - resources.splice(j, 1, getOutputFilename(id)); - }); + if (manifestContent.background?.service_worker) { + const id = resolve(manifestDirname, manifestContent.background.service_worker); + scriptEntrypointAbsolutePaths.push(id); + manifestContent.background.service_worker = getOutputFilename(id); } - }); + manifestContent.icons && Object.entries(manifestContent.icons).forEach(([size, filename]) => { + const id = resolve(manifestDirname, filename); + // console.log('icon:', size, filename, getOutputFilename(id)); + otherAssetAbsolutePaths.push(id); + manifestContent.icons[size] = getOutputFilename(id); + }); + (manifestContent.web_accessible_resources || []).forEach((entry, i) => { + if (typeof entry === 'string') { + // mv2 - single top-level array of items + const id = resolve(manifestDirname, entry); + otherAssetAbsolutePaths.push(id); + manifestContent.web_accessible_resources.splice(i, 1, getOutputFilename(id)); + } else { + // mv3 - array of objects with `resources` keys + const {resources} = entry; + resources && resources.forEach((filename, j) => { + const id = resolve(manifestDirname, filename); + otherAssetAbsolutePaths.push(id); + resources.splice(j, 1, getOutputFilename(id)); + }); + } + }); + + return { + scripts: scriptEntrypointAbsolutePaths, + styles: styleEntrypointAbsolutePathss, + assets: otherAssetAbsolutePaths, + }; + } + + const {scripts, styles, assets} = getEntryPointsFromManifest(manifestContent); const platform = 'firefox'; return [ - ...scriptEntrypointAbsolutePaths.map(entrypointPath => ({ + ...scripts.map(entrypointPath => ({ // Get configuration options for this entrypoint from the caller ...createConfig(), // Overwrite input and output options @@ -119,14 +130,14 @@ export function createConfig (manifestPathRelative, options, createConfig) { plugins: [ { buildStart () { - styleEntrypointAbsolutePathss.forEach(absolutePath => { + styles.forEach(absolutePath => { this.emitFile({ type: 'asset', fileName: getOutputFilename(absolutePath, 'css'), source: readFileSync(absolutePath, {encoding: 'utf-8'}), }); }); - otherAssetAbsolutePaths.forEach(absolutePath => { + assets.forEach(absolutePath => { // console.log('rendering binary file', absolutePath); this.emitFile({ type: 'asset',