parcel-bundler / parcel

The zero configuration build tool for the web. 📦🚀
https://parceljs.org
MIT License
43.51k stars 2.27k forks source link

`@parcel/transformer-webextension` Changes for Manifest v3 #7458

Closed michaelfig closed 2 years ago

michaelfig commented 2 years ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @parcel/transformer-webextension@2.0.1 for the project I'm working on.

I found that when I used a v3 Manifest, then the parcel invocation failed with:

$ parcel build source/manifest.json --no-content-hash --no-source-maps --dist-dir distribution --no-cache --detailed-report 0
🚨 Build failed.

@parcel/transformer-webextension: Invalid Web Extension manifest

  /Users/michael/agoric/powerbox/source/manifest.json:6:22
    5 |   "homepage_url": "https://github.com/Agoric/powerbox",
  > 6 |   "manifest_version": 3,
  >   |                       ^ Possible values: "2"
    7 |   "minimum_chrome_version": "74",
    8 |   "applications": {
error Command failed with exit code 1.

Here is the diff that solved my problem:

diff --git a/node_modules/@parcel/transformer-webextension/src/WebExtensionTransformer.js b/node_modules/@parcel/transformer-webextension/src/WebExtensionTransformer.js
index c409b21..aae7125 100644
--- a/node_modules/@parcel/transformer-webextension/src/WebExtensionTransformer.js
+++ b/node_modules/@parcel/transformer-webextension/src/WebExtensionTransformer.js
@@ -20,6 +20,7 @@ const DEP_LOCS = [
   ['page_action', 'default_icon'],
   ['page_action', 'default_popup'],
   ['background', 'scripts'],
+  ['background', 'service_worker'],
   ['chrome_url_overrides'],
   ['devtools_page'],
   ['options_ui', 'page'],
@@ -172,27 +173,41 @@ async function collectDependencies(
   }
   if (program.web_accessible_resources) {
     let war = [];
-    for (let i = 0; i < program.web_accessible_resources.length; ++i) {
-      // TODO: this doesn't support Parcel resolution
-      const globFiles = (
-        await glob(
-          path.join(
-            path.dirname(filePath),
-            program.web_accessible_resources[i],
-          ),
-          fs,
-          {},
-        )
-      ).map(fp =>
-        asset.addURLDependency(path.relative(path.dirname(filePath), fp), {
-          needsStableName: true,
-          loc: {
-            filePath,
-            ...getJSONSourceLocation(ptrs[`/web_accessible_resources/${i}`]),
-          },
-        }),
-      );
-      war = war.concat(globFiles);
+    let i = 0;
+    for (const resource of program.web_accessible_resources) {
+      const reses = typeof resource === 'string' ? [resource] : resource.resources;
+      for (const res of reses) {
+        // TODO: this doesn't support Parcel resolution
+        const globFiles = (
+          await glob(
+            path.join(
+              path.dirname(filePath),
+              res,
+            ),
+            fs,
+            {},
+          )
+        ).map(fp =>
+          asset.addURLDependency(path.relative(path.dirname(filePath), fp), {
+            needsStableName: true,
+            loc: {
+              filePath,
+              ...getJSONSourceLocation(ptrs[`/web_accessible_resources/${i}`]),
+            },
+          }),
+        );
+        war = war.concat(globFiles);
+
+        if (typeof resource === 'string') {
+          war = war.concat(globFiles);
+        } else {
+          war.push({
+            ...resource,
+            resources: globFiles
+          });
+        }
+        i += 1;
+      }
     }
     program.web_accessible_resources = war;
   }
diff --git a/node_modules/@parcel/transformer-webextension/src/schema.js b/node_modules/@parcel/transformer-webextension/src/schema.js
index 4d829ba..08c0b9e 100644
--- a/node_modules/@parcel/transformer-webextension/src/schema.js
+++ b/node_modules/@parcel/transformer-webextension/src/schema.js
@@ -43,7 +43,7 @@ export default ({
   properties: {
     manifest_version: {
       type: 'number',
-      enum: [2],
+      enum: [2, 3],
     },
     name: {type: 'string'},
     version: {
@@ -92,6 +92,7 @@ export default ({
       properties: {
         scripts: arrStr,
         page: {type: 'string'},
+        service_worker: {type: 'string'},
         persistent: {type: 'boolean'},
       },
     },
@@ -387,6 +388,17 @@ export default ({
       },
     },
     version_name: {type: 'string'},
-    web_accessible_resources: arrStr,
+    web_accessible_resources: {
+      oneOf: [arrStr, {
+        type: 'array',
+        items: {
+          type: 'object',
+          properties: {
+            resources: arrStr,
+            matches: arrStr,
+          },
+        },
+      }],
+    },
   },
 }: SchemaEntity);

This issue body was partially generated by patch-package.

fregante commented 2 years ago

Duplicate of https://github.com/parcel-bundler/parcel/issues/6079

There's an open PR