A custom linker that creates a plain javascript file that can be used with node.js.
| 25 | * that can be used with node.js. |
| 26 | */ |
| 27 | @LinkerOrder(LinkerOrder.Order.PRIMARY) |
| 28 | public class NodeJsModuleLinker extends AbstractLinker { |
| 29 | |
| 30 | /** |
| 31 | * {@inheritDoc} |
| 32 | */ |
| 33 | public ArtifactSet link(final TreeLogger logger, final LinkerContext context, |
| 34 | final ArtifactSet artifacts) throws UnableToCompleteException { |
| 35 | |
| 36 | ArtifactSet toReturn = new ArtifactSet(artifacts); |
| 37 | DefaultTextOutput out = new DefaultTextOutput(true); |
| 38 | |
| 39 | // get compilation result |
| 40 | Set<CompilationResult> results = artifacts.find(CompilationResult.class); |
| 41 | CompilationResult result = results.iterator().next(); |
| 42 | // get the generated javascript |
| 43 | String[] javaScript = result.getJavaScript(); |
| 44 | |
| 45 | out.print("'use strict';"); |
| 46 | out.newline(); |
| 47 | // create dummies for several things requested by gwt |
| 48 | out.newline(); |
| 49 | out.print("// -------------- "); |
| 50 | out.print(" FAKE ELEMENTS GWT ASSUMES EXIST"); |
| 51 | out.print(" -------------- "); |
| 52 | out.newline(); |
| 53 | // node provides most elements of a browser's window |
| 54 | out.print("var $wnd;"); |
| 55 | out.newline(); |
| 56 | out.print("if (typeof window !== 'undefined')"); |
| 57 | out.newline(); |
| 58 | out.print(" $wnd = window"); |
| 59 | out.newline(); |
| 60 | out.print("else if (typeof global !== 'undefined')"); |
| 61 | out.newline(); |
| 62 | out.print(" $wnd = global // nodejs"); |
| 63 | out.newline(); |
| 64 | out.print("else if (typeof self !== 'undefined')"); |
| 65 | out.newline(); |
| 66 | out.print(" $wnd = self // web worker"); |
| 67 | out.newline(); |
| 68 | out.newline(); |
| 69 | |
| 70 | out.print("var $moduleName,"); |
| 71 | out.newline(); |
| 72 | out.print(" $moduleBase;"); |
| 73 | out.newline(); |
| 74 | out.newline(); |
| 75 | |
| 76 | // Workaround for strict mode, see #127 |
| 77 | out.print("// -------------- "); |
| 78 | out.print(" WORKAROUND STRICT MODE, SEE #127 "); |
| 79 | out.print(" -------------- "); |
| 80 | out.newline(); |
| 81 | if (context.isOutputCompact()) { |
| 82 | out.print("var g, i, o;"); |
| 83 | } else { |
| 84 | out.print("var _xblockexpression;"); |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…