From 00e6b3bc241b51aab5d135e44d30deca858312f7 Mon Sep 17 00:00:00 2001 From: tdeegan Date: Tue, 14 Feb 2017 14:39:14 -0800 Subject: [PATCH] Add support for --js=path/to/zip/file.zip!/relative/path/to/entry.js style js files. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=147522774 --- .../google/javascript/jscomp/SourceFile.java | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/com/google/javascript/jscomp/SourceFile.java b/src/com/google/javascript/jscomp/SourceFile.java index e1dd513b488..13d9455fd4d 100644 --- a/src/com/google/javascript/jscomp/SourceFile.java +++ b/src/com/google/javascript/jscomp/SourceFile.java @@ -46,9 +46,9 @@ import java.util.zip.ZipFile; /** - * An abstract representation of a source file that provides access to - * language-neutral features. The source file can be loaded from various - * locations, such as from disk or from a preloaded string. + * An abstract representation of a source file that provides access to language-neutral features. + * The source file can be loaded from various locations, such as from disk or from a preloaded + * string. * * @author nicksantos@google.com (Nick Santos) */ @@ -93,9 +93,9 @@ public interface Generator { /** * Construct a new abstract source file. * - * @param fileName The file name of the source file. It does not necessarily - * need to correspond to a real path. But it should be unique. Will - * appear in warning messages emitted by the compiler. + * @param fileName The file name of the source file. It does not necessarily need to correspond to + * a real path. But it should be unique. Will appear in warning messages emitted by the + * compiler. */ public SourceFile(String fileName) { if (isNullOrEmpty(fileName)) { @@ -368,11 +368,14 @@ public static List fromZipFile(String zipName, Charset inputCharset) return sourceFiles; } + static final String BANG_SLASH = "!/"; + static final String JAR_URL_PREFIX = "jar:file:"; + @GwtIncompatible("java.net.URL") public static SourceFile fromZipEntry( String originalZipPath, String absoluteZipPath, String entryPath, Charset inputCharset) throws MalformedURLException { - String zipEntryPath = "jar:file:" + absoluteZipPath + "!/" + entryPath; + String zipEntryPath = JAR_URL_PREFIX + absoluteZipPath + BANG_SLASH + entryPath; URL zipEntryUrl = new URL(zipEntryPath); return builder() @@ -382,8 +385,18 @@ public static SourceFile fromZipEntry( } @GwtIncompatible("java.io.File") - public static SourceFile fromFile(String fileName, Charset c) { - return builder().withCharset(c).buildFromFile(fileName); + public static SourceFile fromFile(String fileName, Charset charset) { + if (fileName.contains(BANG_SLASH)) { + String[] components = fileName.split(BANG_SLASH); + try { + String zipPath = components[0]; + String relativePath = components[1]; + return fromZipEntry(zipPath, zipPath, relativePath, charset); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } + return builder().withCharset(charset).buildFromFile(fileName); } public static SourceFile fromFile(String fileName) {