give manifest searching a function

This commit is contained in:
Erin 2023-07-28 04:41:15 -04:00
parent bb79e2a059
commit cb948b82ba

103
index.mjs
View file

@ -46,58 +46,69 @@ export function createConfig (manifestPathRelative, options, createConfig) {
return join(outputDir, platform, getOutputFilename(entryPath, ext)); return join(outputDir, platform, getOutputFilename(entryPath, ext));
} }
const scriptEntrypointAbsolutePaths = []; /** Scans a manifest for entrypoints */
const styleEntrypointAbsolutePathss = []; function getEntryPointsFromManifest (manifestContent) {
const otherAssetAbsolutePaths = []; const scriptEntrypointAbsolutePaths = [];
// Gather all JS entry points specified in the manifest const styleEntrypointAbsolutePathss = [];
(manifestContent.content_scripts || []).forEach(({css, js}) => { const otherAssetAbsolutePaths = [];
css && css.forEach((filename, i) => { // Gather all JS entry points specified in the manifest
const id = resolve(manifestDirname, filename); (manifestContent.content_scripts || []).forEach(({css, js}) => {
styleEntrypointAbsolutePathss.push(id); css && css.forEach((filename, i) => {
css.splice(i, 1, getOutputFilename(id, 'css')); 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); const id = resolve(manifestDirname, filename);
scriptEntrypointAbsolutePaths.push(id); scriptEntrypointAbsolutePaths.push(id);
js.splice(i, 1, getOutputFilename(id, 'js')); manifestContent.background.scripts.splice(i, 1, getOutputFilename(id));
}); });
}); if (manifestContent.background?.service_worker) {
manifestContent.background?.scripts && manifestContent.background.scripts.forEach((filename, i) => { const id = resolve(manifestDirname, manifestContent.background.service_worker);
const id = resolve(manifestDirname, filename); scriptEntrypointAbsolutePaths.push(id);
scriptEntrypointAbsolutePaths.push(id); manifestContent.background.service_worker = getOutputFilename(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));
});
} }
}); 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'; const platform = 'firefox';
return [ return [
...scriptEntrypointAbsolutePaths.map(entrypointPath => ({ ...scripts.map(entrypointPath => ({
// Get configuration options for this entrypoint from the caller // Get configuration options for this entrypoint from the caller
...createConfig(), ...createConfig(),
// Overwrite input and output options // Overwrite input and output options
@ -119,14 +130,14 @@ export function createConfig (manifestPathRelative, options, createConfig) {
plugins: [ plugins: [
{ {
buildStart () { buildStart () {
styleEntrypointAbsolutePathss.forEach(absolutePath => { styles.forEach(absolutePath => {
this.emitFile({ this.emitFile({
type: 'asset', type: 'asset',
fileName: getOutputFilename(absolutePath, 'css'), fileName: getOutputFilename(absolutePath, 'css'),
source: readFileSync(absolutePath, {encoding: 'utf-8'}), source: readFileSync(absolutePath, {encoding: 'utf-8'}),
}); });
}); });
otherAssetAbsolutePaths.forEach(absolutePath => { assets.forEach(absolutePath => {
// console.log('rendering binary file', absolutePath); // console.log('rendering binary file', absolutePath);
this.emitFile({ this.emitFile({
type: 'asset', type: 'asset',