|
125 | 125 | (defmethod js-source-file BufferedInputStream [^String name ^BufferedInputStream source]
|
126 | 126 | (SourceFile/fromInputStream name source))
|
127 | 127 |
|
| 128 | +(def check-level |
| 129 | + {:error CheckLevel/ERROR |
| 130 | + :warning CheckLevel/WARNING |
| 131 | + :off CheckLevel/OFF}) |
| 132 | + |
| 133 | +(def warning-types |
| 134 | + {:access-controls DiagnosticGroups/ACCESS_CONTROLS |
| 135 | + :ambiguous-function-decl DiagnosticGroups/AMBIGUOUS_FUNCTION_DECL |
| 136 | + :debugger-statement-present DiagnosticGroups/DEBUGGER_STATEMENT_PRESENT |
| 137 | + :check-regexp DiagnosticGroups/CHECK_REGEXP |
| 138 | + :check-types DiagnosticGroups/CHECK_TYPES |
| 139 | + :check-useless-code DiagnosticGroups/CHECK_USELESS_CODE |
| 140 | + :check-variables DiagnosticGroups/CHECK_VARIABLES |
| 141 | + :const DiagnosticGroups/CONST |
| 142 | + :constant-property DiagnosticGroups/CONSTANT_PROPERTY |
| 143 | + :deprecated DiagnosticGroups/DEPRECATED |
| 144 | + :duplicate-message DiagnosticGroups/DUPLICATE_MESSAGE |
| 145 | + :es5-strict DiagnosticGroups/ES5_STRICT |
| 146 | + :externs-validation DiagnosticGroups/EXTERNS_VALIDATION |
| 147 | + :fileoverview-jsdoc DiagnosticGroups/FILEOVERVIEW_JSDOC |
| 148 | + :global-this DiagnosticGroups/GLOBAL_THIS |
| 149 | + :internet-explorer-checks DiagnosticGroups/INTERNET_EXPLORER_CHECKS |
| 150 | + :invalid-casts DiagnosticGroups/INVALID_CASTS |
| 151 | + :missing-properties DiagnosticGroups/MISSING_PROPERTIES |
| 152 | + :non-standard-jsdoc DiagnosticGroups/NON_STANDARD_JSDOC |
| 153 | + :strict-module-dep-check DiagnosticGroups/STRICT_MODULE_DEP_CHECK |
| 154 | + :tweaks DiagnosticGroups/TWEAKS |
| 155 | + :undefined-names DiagnosticGroups/UNDEFINED_NAMES |
| 156 | + :undefined-variables DiagnosticGroups/UNDEFINED_VARIABLES |
| 157 | + :unknown-defines DiagnosticGroups/UNKNOWN_DEFINES |
| 158 | + :visiblity DiagnosticGroups/VISIBILITY}) |
| 159 | + |
128 | 160 | (defn set-options
|
129 | 161 | "TODO: Add any other options that we would like to support."
|
130 | 162 | [opts ^CompilerOptions compiler-options]
|
|
157 | 189 |
|
158 | 190 | (when (contains? opts :print-input-delimiter)
|
159 | 191 | (set! (.printInputDelimiter compiler-options)
|
160 |
| - (:print-input-delimiter opts)))) |
| 192 | + (:print-input-delimiter opts))) |
161 | 193 |
|
162 |
| -(def check-level |
163 |
| - {:error CheckLevel/ERROR |
164 |
| - :warning CheckLevel/WARNING |
165 |
| - :off CheckLevel/OFF}) |
| 194 | + (when (contains? opts :closure-warnings) |
| 195 | + (doseq [[type level] (:closure-warnings opts)] |
| 196 | + (. compiler-options |
| 197 | + (setWarningLevel (type warning-types) (level check-level))))) |
166 | 198 |
|
167 |
| -(def warning-types |
168 |
| - {:access-controls DiagnosticGroups/ACCESS_CONTROLS |
169 |
| - :ambiguous-function-decl DiagnosticGroups/AMBIGUOUS_FUNCTION_DECL |
170 |
| - :debugger-statement-present DiagnosticGroups/DEBUGGER_STATEMENT_PRESENT |
171 |
| - :check-regexp DiagnosticGroups/CHECK_REGEXP |
172 |
| - :check-types DiagnosticGroups/CHECK_TYPES |
173 |
| - :check-useless-code DiagnosticGroups/CHECK_USELESS_CODE |
174 |
| - :check-variables DiagnosticGroups/CHECK_VARIABLES |
175 |
| - :const DiagnosticGroups/CONST |
176 |
| - :constant-property DiagnosticGroups/CONSTANT_PROPERTY |
177 |
| - :deprecated DiagnosticGroups/DEPRECATED |
178 |
| - :duplicate-message DiagnosticGroups/DUPLICATE_MESSAGE |
179 |
| - :es5-strict DiagnosticGroups/ES5_STRICT |
180 |
| - :externs-validation DiagnosticGroups/EXTERNS_VALIDATION |
181 |
| - :fileoverview-jsdoc DiagnosticGroups/FILEOVERVIEW_JSDOC |
182 |
| - :global-this DiagnosticGroups/GLOBAL_THIS |
183 |
| - :internet-explorer-checks DiagnosticGroups/INTERNET_EXPLORER_CHECKS |
184 |
| - :invalid-casts DiagnosticGroups/INVALID_CASTS |
185 |
| - :missing-properties DiagnosticGroups/MISSING_PROPERTIES |
186 |
| - :non-standard-jsdoc DiagnosticGroups/NON_STANDARD_JSDOC |
187 |
| - :strict-module-dep-check DiagnosticGroups/STRICT_MODULE_DEP_CHECK |
188 |
| - :tweaks DiagnosticGroups/TWEAKS |
189 |
| - :undefined-names DiagnosticGroups/UNDEFINED_NAMES |
190 |
| - :undefined-variables DiagnosticGroups/UNDEFINED_VARIABLES |
191 |
| - :unknown-defines DiagnosticGroups/UNKNOWN_DEFINES |
192 |
| - :visiblity DiagnosticGroups/VISIBILITY}) |
| 199 | + (when (contains? opts :closure-extra-annotations) |
| 200 | + (. compiler-options |
| 201 | + (setExtraAnnotationNames (map name (:closure-extra-annotations opts))))) |
| 202 | + |
| 203 | + compiler-options) |
193 | 204 |
|
194 | 205 | (defn ^CompilerOptions make-options
|
195 | 206 | "Create a CompilerOptions object and set options from opts map."
|
|
209 | 220 | (or (true? val)
|
210 | 221 | (false? val)) (.setDefineToBooleanLiteral compiler-options key val)
|
211 | 222 | :else (println "value for" key "must be string, int, float, or bool"))))
|
212 |
| - (doseq [[type level] (:closure-warnings opts)] |
213 |
| - (. compiler-options |
214 |
| - (setWarningLevel (type warning-types) (level check-level)))) |
215 | 223 | (if-let [extra-annotations (:closure-extra-annotations opts)]
|
216 | 224 | (. compiler-options (setExtraAnnotationNames (map name extra-annotations))))
|
217 | 225 | (when (contains? opts :source-map)
|
|
1259 | 1267 | :commonjs
|
1260 | 1268 | module-type)))
|
1261 | 1269 |
|
| 1270 | +(defn make-convert-js-module-options [opts] |
| 1271 | + (-> opts |
| 1272 | + (select-keys [:closure-warnings |
| 1273 | + :pretty-print |
| 1274 | + :closure-extra-annotations]) |
| 1275 | + (set-options (CompilerOptions.)))) |
| 1276 | + |
1262 | 1277 | (util/compile-if can-convert-commonjs?
|
1263 | 1278 | (defmethod convert-js-module :commonjs [js opts]
|
1264 | 1279 | (let [{:keys [file module-type]} js
|
1265 | 1280 | ^List externs '()
|
1266 | 1281 | ^List source-files (get-source-files module-type opts)
|
1267 |
| - ^CompilerOptions options (CompilerOptions.) |
| 1282 | + ^CompilerOptions options (make-convert-js-module-options opts) |
1268 | 1283 | closure-compiler (doto (make-closure-compiler)
|
1269 | 1284 | (.init externs source-files options))
|
1270 | 1285 | es6-loader (if is-new-es6-loader?
|
|
1276 | 1291 | (when (= (:module-type js) :amd)
|
1277 | 1292 | (.process (TransformAMDToCJSModule. closure-compiler) nil root)))
|
1278 | 1293 | (.process cjs nil root)
|
| 1294 | + (report-failure (.getResult closure-compiler)) |
1279 | 1295 | (.toSource closure-compiler root))))
|
1280 | 1296 |
|
1281 | 1297 | (util/compile-if can-convert-es6?
|
1282 | 1298 | (defmethod convert-js-module :es6 [js opts]
|
1283 | 1299 | (let [{:keys [file module-type]} js
|
1284 | 1300 | ^List externs '()
|
1285 | 1301 | ^List source-files (get-source-files module-type opts)
|
1286 |
| - ^CompilerOptions options (doto (CompilerOptions.) |
| 1302 | + ^CompilerOptions options (doto (make-convert-js-module-options opts) |
1287 | 1303 | (.setLanguageIn CompilerOptions$LanguageMode/ECMASCRIPT6)
|
1288 | 1304 | (.setLanguageOut CompilerOptions$LanguageMode/ECMASCRIPT5))
|
1289 | 1305 | closure-compiler (doto (make-closure-compiler)
|
|
1294 | 1310 | cjs (ProcessEs6Modules. closure-compiler es6-loader true)
|
1295 | 1311 | ^Node root (get-root-node file closure-compiler)]
|
1296 | 1312 | (.processFile cjs root)
|
| 1313 | + (report-failure (.getResult closure-compiler)) |
1297 | 1314 | (.toSource closure-compiler root))))
|
1298 | 1315 |
|
1299 | 1316 | (defmethod convert-js-module :default [js opts]
|
|
0 commit comments