You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2020/10/26 21:46:41 UTC

[royale-compiler] branch develop updated: RoyaleClosurePassConfig: prevent-rename uses externs to further reduce release build size

This is an automated email from the ASF dual-hosted git repository.

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git


The following commit(s) were added to refs/heads/develop by this push:
     new 5b5e99d  RoyaleClosurePassConfig: prevent-rename uses externs to further reduce release build size
5b5e99d is described below

commit 5b5e99dcec198928ed11b666f0809df280257cae
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Mon Oct 26 14:46:33 2020 -0700

    RoyaleClosurePassConfig: prevent-rename uses externs to further reduce release build size
---
 .../javascript/jscomp/KeepRoyalePropertyNames.java | 57 ++++++++++++++++++++++
 .../javascript/jscomp/RoyaleClosurePassConfig.java | 39 ++-------------
 2 files changed, 61 insertions(+), 35 deletions(-)

diff --git a/compiler-jx/src/main/java/com/google/javascript/jscomp/KeepRoyalePropertyNames.java b/compiler-jx/src/main/java/com/google/javascript/jscomp/KeepRoyalePropertyNames.java
new file mode 100644
index 0000000..ff73306
--- /dev/null
+++ b/compiler-jx/src/main/java/com/google/javascript/jscomp/KeepRoyalePropertyNames.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2008 The Closure Compiler Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.google.javascript.jscomp;
+
+import java.util.Set;
+
+import com.google.javascript.rhino.IR;
+import com.google.javascript.rhino.Node;
+import com.google.javascript.rhino.jstype.JSType;
+import com.google.javascript.rhino.jstype.JSTypeNative;
+
+public class KeepRoyalePropertyNames implements CompilerPass {
+	
+	private final AbstractCompiler compiler;
+	private Set<String> propertyNamesToKeep;
+
+	public KeepRoyalePropertyNames(AbstractCompiler compiler, Set<String> propertyNamesToKeep) {
+	  this.compiler = compiler;
+	  this.propertyNamesToKeep = propertyNamesToKeep;
+	}
+
+	@Override
+	public void process(Node externs, Node root) {
+	  for(String nameToKeep : propertyNamesToKeep) {
+		addExtern(nameToKeep);
+	  }
+	}
+
+	private void addExtern(String export) {
+	  Node objectPrototype = NodeUtil.newQName(compiler, "Object.prototype");
+	  JSType objCtor = compiler.getTypeRegistry().getNativeType(JSTypeNative.OBJECT_FUNCTION_TYPE);
+	  objectPrototype.getFirstChild().setJSType(objCtor);
+	  Node propstmt = IR.exprResult(IR.getprop(objectPrototype, IR.string(export)));
+	  propstmt.useSourceInfoFromForTree(getSynthesizedExternsRoot());
+	  propstmt.setOriginalName(export);
+	  getSynthesizedExternsRoot().addChildToBack(propstmt);
+	  compiler.reportChangeToEnclosingScope(propstmt);
+	}
+
+	/** Lazily create a "new" externs root for undeclared variables. */
+	private Node getSynthesizedExternsRoot() {
+	  return  compiler.getSynthesizedExternsInput().getAstRoot(compiler);
+	}
+}
diff --git a/compiler-jx/src/main/java/com/google/javascript/jscomp/RoyaleClosurePassConfig.java b/compiler-jx/src/main/java/com/google/javascript/jscomp/RoyaleClosurePassConfig.java
index dee46a3..28281cb 100644
--- a/compiler-jx/src/main/java/com/google/javascript/jscomp/RoyaleClosurePassConfig.java
+++ b/compiler-jx/src/main/java/com/google/javascript/jscomp/RoyaleClosurePassConfig.java
@@ -61,10 +61,7 @@ import com.google.javascript.jscomp.lint.CheckUselessBlocks;
 import com.google.javascript.jscomp.parsing.ParserRunner;
 import com.google.javascript.jscomp.parsing.parser.FeatureSet;
 import com.google.javascript.rhino.IR;
-import com.google.javascript.rhino.JSDocInfo;
-import com.google.javascript.rhino.JSDocInfoBuilder;
 import com.google.javascript.rhino.Node;
-import com.google.javascript.rhino.Token;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -345,7 +342,7 @@ public final class RoyaleClosurePassConfig extends PassConfig {
     }
 
     if (propertyNamesToKeep != null && propertyNamesToKeep.size() > 0) {
-      checks.add(keepPropertyNamesPass);
+      checks.add(keepRoyalePropertyNamesPass);
     }
 
     if (extraSymbolNamesToExport != null) {
@@ -1291,39 +1288,11 @@ public final class RoyaleClosurePassConfig extends PassConfig {
       }
     };
 
-    private final PassFactory keepPropertyNamesPass = 
-        new PassFactory("keep-property-names", true) {
+    private final PassFactory keepRoyalePropertyNamesPass = 
+        new PassFactory("keep-royale-property-names", true) {
           @Override
           protected CompilerPass create(final AbstractCompiler compiler) {
-            return new CompilerPass() {
-              @Override
-              public void process(Node externs, Node root) {
-
-                Node propsObj = new Node(Token.OBJECTLIT);
-                for(String nameToKeep : propertyNamesToKeep)
-                {
-                  Node nameStringKey = IR.stringKey(nameToKeep);
-                  JSDocInfoBuilder builder = new JSDocInfoBuilder(true);
-                  builder.recordExport();
-                  JSDocInfo jsDocInfo = builder.build();
-                  nameStringKey.setJSDocInfo(jsDocInfo);
-
-                  Node propertyDescriptor = new Node(Token.OBJECTLIT);
-                  propertyDescriptor.addChildToBack(IR.propdef(IR.stringKey("get"), NodeUtil.emptyFunction()));
-
-                  Node prop = IR.propdef(nameStringKey, propertyDescriptor);
-                  propsObj.addChildToBack(prop);
-                }
-
-                Node definePropertiesTarget = NodeUtil.newQName(compiler, "Object.defineProperties");
-                Node definePropertiesCall = IR.call(definePropertiesTarget, IR.objectlit(), propsObj);
-                Node expression = IR.exprResult(definePropertiesCall);
-
-                Node scriptNode = compiler.getScriptNode(sourceFileName);
-                scriptNode.addChildToBack(expression);
-                compiler.reportChangeToEnclosingScope(expression);
-              }
-            };
+            return new KeepRoyalePropertyNames(compiler, propertyNamesToKeep);
           }
 
           @Override