Skip to content

Commit

Permalink
Introduce build hook; Patch background page -> service worker
Browse files Browse the repository at this point in the history
Note what we drop/ignore, here:
 - All the various `"scripts"` under the `"background"` key, because for
   the purposes of this minimal migration developing against release
   builds simplifies things considerably.
 - `"type" "module"`, because the generated `out/shared.js` (and
   `out/bg-shared.js`) aren't set up to be statically imported, as per
   https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#man-sw.
   We hackily prepend a copy to `out/background.js` instead, in
   `hooks.core/fix-background-calls`.
  • Loading branch information
CarnunMP committed Sep 14, 2022
1 parent 32d891c commit 7667c6e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
52 changes: 52 additions & 0 deletions shadow/build/hooks/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
(ns hooks.core
(:require
[clojure.data.json :as json]
[clojure.pprint :refer [pprint]]))

(defn- read-manifest []
(json/read-str (slurp "resources/unpacked/manifest.json")))

(defn- write-manifest [manifest]
(spit "resources/unpacked/manifest.json"
(with-out-str
(json/pprint manifest))))

;; Note what we drop/ignore, here:
;; - All the various `"scripts"`, because for the purposes of this minimal migration developing against release builds
;; simplifies things considerably.
;; - `"type" "module"`, because the generated `out/shared.js` and `out/bg-shared.js` aren't set up to be statically
;; imported, as per https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#man-sw. We hackily prepend
;; copies to `out/background.js` instead, in `fix-background-calls` below.
(defn- fix-background-key [manifest]
(update manifest "background"
(fn [{:strs [_scripts]}]
{"service_worker" "out/background.js"
#_#_"type" "module"})))

(defn- fix-background-calls []
(let [background (slurp "resources/unpacked/out/background.js")
;; FIXME: what about `out/bg-shared.js`? It's effectively empty right now, but might not be in future — e.g.
;; during dev builds or as code is added to this minimal example...
shared (slurp "resources/unpacked/out/shared.js")]
(spit "resources/unpacked/out/background.js"
(str "// hacky prepend of `out/shared.js`:" "\n\n" shared
"\n\n\n"
"// `out/background.js`, unchanged:" "\n\n" background))))

(defn patch-extension-outputs-manifest-v2->v3
{:shadow.build/stage :flush}
[build-state & _args]
(let [manifest (read-manifest)
manifest' (-> manifest
fix-background-key)]
(prn)
(prn "manifest.json before:")
(pprint manifest)
(prn)
(prn "manifest.json after:")
(pprint manifest')
(prn)

(write-manifest manifest')
(fix-background-calls))
build-state)
5 changes: 4 additions & 1 deletion shadow/deps.edn
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{:paths ["src/background"
"src/popup"
"src/content-script"]
"src/content-script"
"build"]

:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/clojurescript {:mvn/version "1.11.60"}
org.clojure/data.json {:mvn/version "2.4.0"}
; ---
binaryage/devtools {:mvn/version "1.0.6"}
binaryage/chromex {:mvn/version "0.9.3"}
; ---
Expand Down
3 changes: 2 additions & 1 deletion shadow/shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
:run-at "document_end"}
:entries [chromex-sample.content-script]}
:popup {:output-type :chrome/shared
:entries [chromex-sample.popup]}}}}}
:entries [chromex-sample.popup]}}
:build-hooks [(hooks.core/patch-extension-outputs-manifest-v2->v3)]}}}

0 comments on commit 7667c6e

Please sign in to comment.