|
| 1 | +/* |
| 2 | + * Copyright 2016 The Closure Compiler Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @fileoverview Wrapper for the natively built Closure Compiler GWT binary to |
| 19 | + * work around a few quirks. |
| 20 | + */ |
| 21 | + |
| 22 | +'use strict'; |
| 23 | + |
| 24 | +const compile = require('./build/compile.js'); |
| 25 | + |
| 26 | +let externs; |
| 27 | + |
| 28 | +/** |
| 29 | + * @return {!Array<{source: string}>} default externs files |
| 30 | + */ |
| 31 | +function loadExterns() { |
| 32 | + const fs = require('fs'); |
| 33 | + const path = './build/externs'; |
| 34 | + const all = fs.readdirSync(path); |
| 35 | + |
| 36 | + return all.filter(x => x.endsWith('.js')).map(x => { |
| 37 | + return { |
| 38 | + source: fs.readFileSync(path + '/' + x, {encoding: 'UTF-8'}), |
| 39 | + }; |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +module.exports = function(flags) { |
| 44 | + const clone = { |
| 45 | + languageIn: 'ES6', // TODO(samthor): assume this in upstream code |
| 46 | + }; |
| 47 | + for (const k in flags) { |
| 48 | + clone[k] = flags[k]; |
| 49 | + } |
| 50 | + |
| 51 | + // add default externs |
| 52 | + if (!externs) { |
| 53 | + externs = loadExterns(); |
| 54 | + } |
| 55 | + clone.externs = (clone.externs || []).concat(externs); |
| 56 | + |
| 57 | + const out = compile(clone); |
| 58 | + |
| 59 | + // hide weird GWT internals |
| 60 | + out.warnings = [...out.warnings]; |
| 61 | + out.errors = [...out.errors]; |
| 62 | + |
| 63 | + return out; |
| 64 | +}; |
0 commit comments