Skip to content

Commit

Permalink
add proper support for :output-feature-set
Browse files Browse the repository at this point in the history
it replaced :language-out a while ago
  • Loading branch information
thheller committed Feb 24, 2019
1 parent a749fb9 commit 8332a74
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
1 change: 1 addition & 0 deletions shadow-cljs.edn
Expand Up @@ -144,6 +144,7 @@
{:infer-externs :auto
;; :strip-type-prefixes #{"cljs.pprint"}
:shadow-keywords true
:output-feature-set :es6
;; :closure-output-charset "US-ASCII"
:closure-defines
{demo.browser/FOO "bar"}}
Expand Down
6 changes: 6 additions & 0 deletions src/dev/demo/es6.js
Expand Up @@ -12,5 +12,11 @@ async function someAsyncFn(thing) {
console.log("await value", value);
}

export class Thing {
constructor() {
console.log("hello world");
}
}

export { foo, someAsyncFn };
export default "defaultExport";
2 changes: 1 addition & 1 deletion src/main/shadow/build/api.clj
Expand Up @@ -65,7 +65,7 @@
:closure-configurators []
:infer-externs true
:language-in :ecmascript-next
:language-out :ecmascript5
:output-feature-set :es5

:closure-warnings
{:check-types :off}
Expand Down
4 changes: 2 additions & 2 deletions src/main/shadow/build/cljs_hacks.cljc
Expand Up @@ -538,8 +538,8 @@
(comp/emits (comp/munge head))
(doseq [part tail]
(cond
(and (= "default" part)
(es5>= (:language-out options)))
;; FIXME: should properly check if output is ES3
(= "default" part)
(comp/emits ".default")

(and (contains? ana/js-reserved part)
Expand Down
56 changes: 45 additions & 11 deletions src/main/shadow/build/closure.clj
Expand Up @@ -25,7 +25,8 @@
(shadow.build.closure ReplaceCLJSConstants NodeEnvInlinePass ReplaceRequirePass PropertyCollector NodeStuffInlinePass FindSurvivingRequireCalls ClearUnassignedJsRequires)
(com.google.javascript.jscomp.deps ModuleLoader$ResolutionMode)
(java.nio.charset Charset)
[java.util.logging Logger Level]))
[java.util.logging Logger Level]
[com.google.javascript.jscomp.parsing.parser FeatureSet]))

;; get rid of some annoying/useless warning log messages
;; https://github.com/google/closure-compiler/pull/2998/files
Expand Down Expand Up @@ -80,6 +81,19 @@
:ecmascript-2017 CompilerOptions$LanguageMode/ECMASCRIPT_2017
:ecmascript-next CompilerOptions$LanguageMode/ECMASCRIPT_NEXT))

(defn ^FeatureSet kw->feature-set [kw]
(case kw
:bare-minimum FeatureSet/BARE_MINIMUM
:es3 FeatureSet/ES3
:es5 FeatureSet/ES5
;; FIXME: probably can't allow es6-modules variants since we can never load those
:es6 FeatureSet/ES6
:es7 FeatureSet/ES7
:es8 FeatureSet/ES8
:es2018 FeatureSet/ES2018
:es-next FeatureSet/ES_NEXT
(throw (ex-info "invalid :output-feature-set" {:val kw}))))

(defn set-options
[^CompilerOptions closure-opts opts state]

Expand Down Expand Up @@ -127,6 +141,11 @@
(when-let [lang-key (:language-out opts)]
(.setLanguageOut closure-opts (lang-key->lang-mode lang-key)))

;; FIXME: maybe make this more customizable. may want es5 but allow class or so
(when-let [output-feature-set (:output-feature-set opts)]
(let [fs (kw->feature-set output-feature-set)]
(.setOutputFeatureSet closure-opts fs)))

(when-let [extra-annotations (:closure-extra-annotations opts)]
(. closure-opts (setExtraAnnotationNames (map name extra-annotations))))

Expand Down Expand Up @@ -1254,9 +1273,18 @@
(make-location-mapping resource-name (.getAbsolutePath file)))
(into [])))))

(defn get-output-options [state]
(merge
(when-let [lo (get-in state [:compiler-options :language-out])]
{:language-out lo})
(when-let [ofs (get-in state [:compiler-options :output-feature-set])]
{:output-feature-set ofs})))

(def cache-affecting-options
[[:compiler-options :source-map-use-fs-paths]
[:compiler-options :rewrite-polyfills]
[:compiler-options :language-out]
[:compiler-options :output-feature-set]
[:js-options]])

(defn convert-sources*
Expand Down Expand Up @@ -1333,10 +1361,12 @@

;; FIXME: are there more options we should take from the user?
co-opts
{:pretty-print true
:source-map true
:language-in :ecmascript-next
:language-out :ecmascript5}
(merge
{:pretty-print true
:source-map true
:language-in :ecmascript-next
:output-feature-set :es5}
(get-output-options state))

co
(doto (make-options)
Expand Down Expand Up @@ -1690,7 +1720,9 @@
;; but closure got real picky and complains about some things being es6 that aren't
;; doesn't really impact much here anyways
:language-in :ecmascript-next
:language-out :ecmascript5}
:output-feature-set :es5}
(get-output-options state)
;; js-options should always override things pulled from :compiler-options
(dissoc js-options :resolve)
;; always enable source-map, just skip emitting them later if desired
{:source-map true})
Expand Down Expand Up @@ -2010,11 +2042,13 @@
(data/make-closure-compiler)

co-opts
{:pretty-print true
:pseudo-names false
:language-in :ecmascript-next
:language-out :ecmascript5
:source-map true}
(merge
{:pretty-print true
:pseudo-names false
:language-in :ecmascript-next
:output-feature-set :es5
:source-map true}
(get-output-options state))

generate-source-map?
(not (false? (:source-map js-options)))
Expand Down

0 comments on commit 8332a74

Please sign in to comment.