Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit fd5a255

Browse files
committed
support loading default externs
Change-Id: I8e930d43061cbce9cf30fc01595910b19456b6d5
1 parent df9310e commit fd5a255

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
node_modules
22
.DS_Store
3-
contrib
4-
externs
5-
compile.js
3+
build

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ Contributions are welcome.
2525
### Native Node Usage
2626

2727
The module provides `compile` as a low-level method to compile JavaScript.
28+
By default, this compiles ES6 to ES5 and includes the default set of EcmaScript externs files.
2829

2930
```js
3031
const compiler = require('closure-compiler-js').compiler;
3132

3233
const flags = {
33-
jsCode: [{source: 'var x = 1 + 2;'}],
34+
jsCode: [{source: 'const x = 1 + 2;'}],
3435
};
3536
const out = compile(flags);
3637
console.info(out.compiledCode); // will print 'var x = 3;\n'

build.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919

2020
const spawn = require('child_process').spawnSync;
2121
const ncp = require('ncp');
22+
const fs = require('fs');
23+
24+
try {
25+
fs.mkdirSync('./build');
26+
} catch (e) {
27+
// ignore, probably exists
28+
}
2229

2330
const moduleName = 'com.google.javascript:closure-compiler-gwt';
2431
const compilerBuild = spawn('mvn', ['-DskipTests', '-pl', moduleName], {
@@ -31,12 +38,12 @@ if (compilerBuild.status !== 0) {
3138
}
3239

3340
const targetPath = './closure-compiler/target/closure-compiler-gwt-1.0-SNAPSHOT/jscomp/jscomp.js';
34-
ncp(targetPath, './compile.js', err => {
41+
ncp(targetPath, './build/compile.js', err => {
3542
if (err) {
3643
throw new Error(err);
3744
}
3845
['contrib', 'externs'].forEach(name => {
39-
ncp('./closure-compiler/' + name, './' + name, function(err) {
46+
ncp('./closure-compiler/' + name, './build/' + name, function(err) {
4047
if (err) {
4148
throw new Error(err);
4249
}

compile.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)