Skip to content

Instantly share code, notes, and snippets.

@hlolli
Last active January 11, 2018 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hlolli/265b9183566a4c5829d7ee355e3d0998 to your computer and use it in GitHub Desktop.
Save hlolli/265b9183566a4c5829d7ee355e3d0998 to your computer and use it in GitHub Desktop.
chrome_extension for shadow-cljs
(ns shadow.chrome-extension
(:refer-clojure :exclude (flush require))
(:require [shadow.build :as build]
[shadow.build.api :as build-api]
[clojure.java.io :as io]
[clojure.set :as set]
[clojure.string :as str]
[clojure.data.json :as json]
[shadow.build.data :as data]
[shadow.build.output :as output]
[shadow.build.targets.shared :as shared]
[shadow.build.classpath :as cp]
[shadow.build.modules :as modules]
[shadow.cljs.repl :as repl]
[shadow.build.targets.browser :as browser]))
(defn closure-require-hack [{:keys [build-options] :as state} build-modules]
(let [{:keys [dev-inline-js cljs-runtime-path asset-path]} build-options]
(loop [build-modules build-modules
goog-requires ""]
(if (empty? build-modules)
goog-requires
(let [{:keys [sources] :as mod} (first build-modules)
goog-requires' (->> sources
(map #(data/get-source-by-id state %))
(map :output-name)
(map (fn [output-name]
(str "goog.dependencies_.written[\"" output-name "\"] = true;\n"
"goog.dependencies_.written[\"" asset-path "/" cljs-runtime-path "/" output-name "\"] = true;")))
(str/join "\n"))]
(recur (rest build-modules)
(str goog-requires goog-requires')))))))
(defn flush-crx-to-file
[{:keys [mode unoptimizable build-options build-modules build-sources] :as state}
{:keys [output-to asset-path] :as config}]
(output/flush-unoptimized state)
(let [cljs-runtime-path (:cljs-runtime-path build-options)
prepend
(str unoptimizable
(output/closure-defines-and-base state)
"var shadow$provide = {};\n"
"goog.global[\"$CLJS\"] = goog.global;\n")
goog-requires (closure-require-hack state build-modules)
out
(->> build-sources
(map #(data/get-source-by-id state %))
(remove #(= "goog/base.js" (:resource-name %)))
(map #(data/get-output! state %))
(map :js)
(str/join "\n"))]
(spit output-to (str prepend goog-requires out)))
state)
(defn flush [state mode config]
(case mode
:dev
(flush-crx-to-file state config)
:release
(output/flush-optimized state)))
(defn process
[{::build/keys [stage mode config] :as state}]
(-> state
browser/process
(cond->
(= stage :flush)
(flush mode config))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment