Skip to content

Commit

Permalink
Add support for the --js and --externs flags to the GWT version
Browse files Browse the repository at this point in the history
Closes #3036

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=207211332
  • Loading branch information
ChadKillingsworth authored and blickly committed Aug 3, 2018
1 parent 0e232da commit 1222684
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/com/google/javascript/jscomp/CommandLineRunner.java
Expand Up @@ -857,8 +857,6 @@ private void parse(List<String> args) throws CmdLineException {

private static final ImmutableSet<String> gwtUnsupportedFlags =
ImmutableSet.of(
"externs",
"js",
"conformance_configs",
"error_format",
"warnings_whitelist_file",
Expand Down
75 changes: 70 additions & 5 deletions src/com/google/javascript/jscomp/gwt/client/GwtRunner.java
Expand Up @@ -104,13 +104,15 @@ private static class Flags {
String[] entryPoint;
String env;
boolean exportLocalPropertyDefinitions;
Object[] externs;
String[] extraAnnotationName;
String[] forceInjectLibraries;
String[] formatting;
boolean generateExports;
String[] hideWarningsFor;
boolean injectLibraries;
String isolationMode;
String[] js;
String[] jscompError;
String[] jscompOff;
String[] jscompWarning;
Expand Down Expand Up @@ -142,7 +144,6 @@ private static class Flags {
// These flags do not match the Java compiler JAR.
@Deprecated
File[] jsCode;
File[] externs;
JsMap defines;
}

Expand Down Expand Up @@ -186,6 +187,7 @@ private static Flags getDefaultFlags() {
defaultFlags.generateExports = false;
defaultFlags.hideWarningsFor = null;
defaultFlags.injectLibraries = true;
defaultFlags.js = null;
defaultFlags.jsCode = null;
defaultFlags.jscompError = null;
defaultFlags.jscompOff = null;
Expand Down Expand Up @@ -283,6 +285,28 @@ private native Object get(String key) /*-{
}-*/;
}

/**
* @param jsFilePaths Array of file paths. If running under NodeJS, they will be loaded via the
* native node fs module.
* @return Array of File objects. If called without running under node, return null to indicate
* failure.
*/
private static native File[] getJsFiles(String[] jsFilePaths) /*-{
if (!(typeof process === 'object' && process.version)) {
return null;
}
var jsFiles = [];
for (var i = 0; i < jsFilePaths.length; i++) {
if (typeof process === 'object' && process.version) {
jsFiles.push({
path: jsFilePaths[i],
src: require('fs').readFileSync(jsFilePaths[i], 'utf8')
});
}
}
return jsFiles;
}-*/;

@JsMethod(name = "keys", namespace = "Object")
private static native String[] keys(Object o);

Expand Down Expand Up @@ -315,7 +339,7 @@ private static ChunkOutput writeChunkOutput(
parseModuleWrappers(Arrays.asList(getStringArray(flags, "chunkWrapper")), chunks);

for (JSModule c : chunks) {
if (flags.createSourceMap != null) {
if (flags.createSourceMap != null && !flags.createSourceMap.equals(false)) {
compiler.getSourceMap().reset();
}

Expand Down Expand Up @@ -363,7 +387,7 @@ private static ChunkOutput writeChunkOutput(

file.src = out.toString();

if (flags.createSourceMap != null) {
if (flags.createSourceMap != null && !flags.createSourceMap.equals(false)) {
StringBuilder b = new StringBuilder();
try {
compiler.getSourceMap().appendTo(b, file.path);
Expand Down Expand Up @@ -413,7 +437,7 @@ private static ChunkOutput writeOutput(Compiler compiler, Flags flags) {
}
postfix = flags.outputWrapper.substring(pos + marker.length());
}
if (flags.createSourceMap != null) {
if (flags.createSourceMap != null && !flags.createSourceMap.equals(false)) {
StringBuilder b = new StringBuilder();
try {
compiler.getSourceMap().appendTo(b, flags.jsOutputFile);
Expand Down Expand Up @@ -671,6 +695,29 @@ private static void applyOptionsFromFlags(
options.setModuleRoots(Arrays.asList(getStringArray(flags, "jsModuleRoot")));
}

/**
* @param externs Array of strings or File[]. If running under NodeJS, an array of strings will be
* treated as file paths and loaded via the native node fs module.
* @return Array of extern File objects. If an array of strings is passed without running under
* node, return null to indicate failure.
*/
private static native File[] fromExternsFlag(Object[] externs) /*-{
var externFiles = [];
for (var i = 0; i < externs.length; i++) {
if (externs[i].path || externs[i].src) {
externFiles.push(externs[i]);
} else if (typeof process === 'object' && process.version) {
externFiles.push({
path: externs[i],
src: require('fs').readFileSync(externs[i], 'utf8')
});
} else {
return null;
}
}
return externFiles;
}-*/;

private static List<SourceFile> fromFileArray(File[] src, String unknownPrefix) {
List<SourceFile> out = new ArrayList<>();
if (src != null) {
Expand Down Expand Up @@ -769,12 +816,30 @@ public static ChunkOutput compile(Flags flags, File[] inputs) throws IOException
}
}

if (flags.js != null) {
File[] jsFiles = getJsFiles(getStringArray(flags, "js"));
if (jsFiles == null) {
throw new RuntimeException(
"Can only load files from the filesystem when running in NodeJS.");
} else {
jsCode.addAll(fromFileArray(jsFiles, "Input_"));
}
}

Compiler compiler = new Compiler(new NodePrintStream());
CompilerOptions options = new CompilerOptions();
applyOptionsFromFlags(options, flags, compiler.getDiagnosticGroups());
options.setInputSourceMaps(sourceMaps);

List<SourceFile> externs = fromFileArray(flags.externs, "Extern_");
List<SourceFile> externs = new ArrayList<>();
if (flags.externs != null) {
File[] externFiles = fromExternsFlag(getStringArray(flags, "externs"));
if (externFiles == null) {
throw new RuntimeException(
"Can only load files from the filesystem when running in NodeJS.");
}
externs = fromFileArray(externFiles, "Extern_");
}
externs.addAll(createExterns(options.getEnvironment()));

NodeErrorManager errorManager = new NodeErrorManager();
Expand Down

0 comments on commit 1222684

Please sign in to comment.