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));
}
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',