Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added a bit of comments

  • Loading branch information...
commit b5e43e915cf53d2114742dc92d2d50a1176fed16 1 parent 6880849
Henrik Hofmeister authored February 02, 2013
26  src/main/java/com/vonhof/webi/file/JavascriptHandler.java
@@ -39,6 +39,7 @@
39 39
 
40 40
     public JavascriptHandler() {
41 41
         super("text/javascript");
  42
+        Compiler.setLoggingLevel(Level.SEVERE);
42 43
     }
43 44
 
44 45
     @Override
@@ -58,8 +59,10 @@ protected void outputFiles(WebiContext req, List<File> files) throws IOException
58 59
     } 
59 60
     
60 61
     private void compiled(WebiContext req, List<File> files) throws IOException {
61  
-        final Compiler compiler = new Compiler();
  62
+        
62 63
         final String sourceName = req.getRequest().getRequestURI();
  64
+        final String sourceMapPath = sourceName+"?map";
  65
+        //Check if we should be outputting source map
63 66
         boolean outputMap = req.getParameterMap().contains("map");
64 67
         if (outputMap) {
65 68
             req.setHeader("Content-type", "application/json");
@@ -67,13 +70,15 @@ private void compiled(WebiContext req, List<File> files) throws IOException {
67 70
                 IOUtils.write(sourceMaps.get(sourceName), req.getOutputStream());
68 71
                 return;
69 72
             }
  73
+        } else {
  74
+            req.setHeader("X-SourceMap",sourceMapPath);
70 75
         }
71 76
         
72 77
         final CompilerOptions options = new CompilerOptions();
73 78
         CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
74 79
         options.setLanguageIn(CompilerOptions.LanguageMode.ECMASCRIPT5);
75 80
         options.sourceMapDetailLevel = SourceMap.DetailLevel.ALL;
76  
-        options.sourceMapOutputPath = sourceName+"?map";
  81
+        options.sourceMapOutputPath = sourceMapPath;
77 82
         
78 83
         
79 84
         final ArrayList<SourceFile> externs = new ArrayList<SourceFile>();
@@ -81,12 +86,12 @@ private void compiled(WebiContext req, List<File> files) throws IOException {
81 86
         JSModule rootModule = new JSModule("global");
82 87
         modules.put("__global", rootModule);
83 88
         
  89
+        //Atleast 1 source file required in root module
84 90
         rootModule.add(SourceFile.fromCode("root.js", ""));
85 91
         
86  
-        
87 92
         final String baseDir = this.getDocumentRoot()+req.getPath();
88  
-        final Map<String,String> dependencies = new HashMap<String, String>();
89 93
         
  94
+        //Run through all files that should be compiled
90 95
         for(File file:files) {
91 96
             
92 97
             String relativePath = req.getBase()+file.getAbsolutePath().substring(this.getDocumentRoot().length()+1);
@@ -96,8 +101,10 @@ private void compiled(WebiContext req, List<File> files) throws IOException {
96 101
             }
97 102
             fileName += "?source";
98 103
             
  104
+            //Build source file using paths that the browsers will recognize in source maps
99 105
             SourceFile sFile = SourceFile.fromCode(fileName,relativePath,Files.toString(file, charset));
100 106
             
  107
+            //Check for special comment //@module <name> @prio - low-tech dependency management
101 108
             String firstLine = Files.readFirstLine(file, charset);
102 109
             Matcher m = modulePattern.matcher(firstLine);
103 110
             String moduleName = null;
@@ -116,6 +123,7 @@ private void compiled(WebiContext req, List<File> files) throws IOException {
116 123
                 modules.put(moduleName, new JSModule(moduleName));
117 124
             }
118 125
             
  126
+            //@TODO: Do a real ordered implementation and not just this
119 127
             if (order > 0) {
120 128
                 modules.get(moduleName).addFirst(sFile);
121 129
             } else {
@@ -123,6 +131,7 @@ private void compiled(WebiContext req, List<File> files) throws IOException {
123 131
             }
124 132
         }
125 133
         
  134
+        //Run through all modules and add dependencies as necessary
126 135
         for(Entry<String,JSModule> entry:modules.entrySet()) {
127 136
             String moduleName = entry.getKey();
128 137
             
@@ -137,6 +146,8 @@ private void compiled(WebiContext req, List<File> files) throws IOException {
137 146
                 }
138 147
             }
139 148
         }
  149
+        
  150
+        //Sort dependencies - Closure needs them to be ordered correctly.
140 151
         List<JSModule> moduleList = null;
141 152
         try {
142 153
             JSModule[] sortJsModules = JSModule.sortJsModules(modules.values());
@@ -145,21 +156,24 @@ private void compiled(WebiContext req, List<File> files) throws IOException {
145 156
             throw new IOException(ex);
146 157
         }
147 158
         
148  
-        Compiler.setLoggingLevel(Level.SEVERE);
149  
-        
  159
+        //Instantiate and compile all modules
  160
+        final Compiler compiler = new Compiler();
150 161
         Result result = compiler.compileModules(externs,moduleList, options);
151 162
         
152 163
         //Must generate sources before accessing source map
153 164
         String source = compiler.toSource();
154 165
         
  166
+        //Build source map
155 167
         StringBuilder sb = new StringBuilder();
156 168
         result.sourceMap.validate(true);
157 169
         result.sourceMap.appendTo(sb, sourceName);
158 170
         sourceMaps.put(sourceName, sb.toString());
159 171
         
160 172
         if (outputMap) {
  173
+            //Output source map
161 174
             IOUtils.write(sourceMaps.get(sourceName), req.getOutputStream());
162 175
         } else {
  176
+            //Add source map special comment to source and output compiled js
163 177
             source += "\n//@ sourceMappingURL="+options.sourceMapOutputPath;
164 178
             req.setHeader("X-SourceMap",options.sourceMapOutputPath);
165 179
             IOUtils.write(source, req.getOutputStream());

0 notes on commit b5e43e9

Please sign in to comment.
Something went wrong with that request. Please try again.