Skip to content

Commit

Permalink
#51 - Fixed strict mode, #50 - Fixed HTML template strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrizzly authored and Chrizzly committed Apr 29, 2019
1 parent cb26982 commit 5a21a5d
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/org/netbeans/util/source/minify/MinifyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.javascript.jscomp.CompilerOptions;
import com.google.javascript.jscomp.Compiler;
import com.google.javascript.jscomp.SourceFile;
import com.google.javascript.jscomp.parsing.Config;
import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
import com.googlecode.htmlcompressor.compressor.XmlCompressor;
import com.yahoo.platform.yui.compressor.CssCompressor;
Expand All @@ -43,10 +44,11 @@
import org.openide.util.Exceptions;

public class MinifyUtil {

MinifyResult minify(FileObject parentFile, MinifyProperty minifyProperty) {
int directory = 0, cssFile = 0, jsFile = 0, htmlFile = 0, xmlFile = 0, jsonFile = 0;
MinifyResult minifyResult = new MinifyResult();

for (FileObject file : parentFile.getChildren()) {
if (file.isFolder()) {
directory++;
Expand All @@ -57,7 +59,7 @@ MinifyResult minify(FileObject parentFile, MinifyProperty minifyProperty) {
htmlFile = htmlFile + preMinifyResult.getHtmlFiles();
xmlFile = xmlFile + preMinifyResult.getXmlFiles();
jsonFile = jsonFile + preMinifyResult.getJsonFiles();

minifyResult.setInputJsFilesSize(minifyResult.getInputJsFilesSize() + preMinifyResult.getInputJsFilesSize());
minifyResult.setOutputJsFilesSize(minifyResult.getOutputJsFilesSize() + preMinifyResult.getOutputJsFilesSize());
minifyResult.setInputCssFilesSize(minifyResult.getInputCssFilesSize() + preMinifyResult.getInputCssFilesSize());
Expand Down Expand Up @@ -220,6 +222,12 @@ public String compressSelectedJavaScript(String inputFilename, String content, M
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();

compiler.initOptions(options);

options.setStrictModeInput(false);
options.setEmitUseStrict(false);
options.setTrustedStrings(true);

CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);

List<SourceFile> inputs = new ArrayList<SourceFile>();
Expand Down Expand Up @@ -270,6 +278,13 @@ public MinifyFileResult compressContent(String inputFilename, String content, St
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();

// options.setLanguageOut(CompilerOptions.LanguageMode.ECMASCRIPT3);
compiler.initOptions(options);

// options.setStrictModeInput(false);
options.setEmitUseStrict(false);
options.setTrustedStrings(true);

CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);

List<SourceFile> inputs = new ArrayList<SourceFile>();
Expand All @@ -295,7 +310,6 @@ public MinifyFileResult compressContent(String inputFilename, String content, St
}
} else if (mimeType.equals("text/javascript")) {
compiler.compile(CommandLineRunner.getDefaultExterns(), inputs, options);


if (StringUtils.isBlank(MinifyProperty.getInstance().getHeaderJS())) {
out.write(compiler.toSource());
Expand All @@ -309,7 +323,7 @@ public MinifyFileResult compressContent(String inputFilename, String content, St
StringWriter outputWriter = new StringWriter();
compressor.compress(outputWriter, minifyProperty.getLineBreakPosition());
outputWriter.flush();
if(StringUtils.isBlank(MinifyProperty.getInstance().getHeaderCSS())) {
if (StringUtils.isBlank(MinifyProperty.getInstance().getHeaderCSS())) {
out.write(outputWriter.toString());
} else {
out.write(MinifyProperty.getInstance().getHeaderCSS() + "\n" + outputWriter.toString());
Expand All @@ -318,7 +332,7 @@ public MinifyFileResult compressContent(String inputFilename, String content, St
} else if (mimeType.equals("text/x-json")) {
JSONMinifyUtil compressor = new JSONMinifyUtil();
output = compressor.minify(content);
if(StringUtils.isBlank(MinifyProperty.getInstance().getHeaderJSON())) {
if (StringUtils.isBlank(MinifyProperty.getInstance().getHeaderJSON())) {
out.write(output);
} else {
out.write(MinifyProperty.getInstance().getHeaderJSON() + "\n" + output);
Expand All @@ -329,7 +343,7 @@ public MinifyFileResult compressContent(String inputFilename, String content, St
compressor.setRemoveComments(true);
compressor.setEnabled(true);
output = compressor.compress(content);
if(StringUtils.isBlank(MinifyProperty.getInstance().getHeaderXML())) {
if (StringUtils.isBlank(MinifyProperty.getInstance().getHeaderXML())) {
out.write(output);
} else {
out.write(MinifyProperty.getInstance().getHeaderXML() + "\n" + output);
Expand All @@ -349,6 +363,12 @@ public MinifyFileResult compress(String inputFilename, String mimeType, String o
MinifyFileResult minifyFileResult = new MinifyFileResult();
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();

compiler.initOptions(options);

options.setEmitUseStrict(false);
options.setTrustedStrings(true);

CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);

List<SourceFile> inputs = new ArrayList<SourceFile>();
Expand All @@ -373,7 +393,7 @@ public MinifyFileResult compress(String inputFilename, String mimeType, String o
out.write(MinifyProperty.getInstance().getHeaderHTML() + "\n" + output);
} else if (mimeType.equals("text/javascript")) {
compiler.compile(CommandLineRunner.getDefaultExterns(), inputs, options);

if (StringUtils.isBlank(MinifyProperty.getInstance().getHeaderJS())) {
out.write(compiler.toSource());
} else {
Expand All @@ -384,13 +404,13 @@ public MinifyFileResult compress(String inputFilename, String mimeType, String o
StringWriter outputWriter = new StringWriter();
compressor.compress(outputWriter, minifyProperty.getLineBreakPosition());
outputWriter.flush();

if (StringUtils.isBlank(MinifyProperty.getInstance().getHeaderJS())) {
out.write(outputWriter.toString());
} else {
out.write(MinifyProperty.getInstance().getHeaderCSS() + "\n" + outputWriter.toString());
}

outputWriter.close();
} else if (mimeType.equals("text/x-json")) {
JSONMinifyUtil compressor = new JSONMinifyUtil();
Expand Down Expand Up @@ -469,4 +489,4 @@ public static String fromStream(Reader in) throws IOException {

return srcsb.toString();
}
}
}

0 comments on commit 5a21a5d

Please sign in to comment.