Example usage for com.google.gwt.core.ext LinkerContext optimizeJavaScript

List of usage examples for com.google.gwt.core.ext LinkerContext optimizeJavaScript

Introduction

In this page you can find the example usage for com.google.gwt.core.ext LinkerContext optimizeJavaScript.

Prototype

String optimizeJavaScript(TreeLogger logger, String jsProgram) throws UnableToCompleteException;

Source Link

Document

Applies optimizations to a JavaScript program.

Usage

From source file:com.totsp.crossword.misc.GadgetLinker.java

License:Apache License

@Override
protected EmittedArtifact emitSelectionScript(TreeLogger logger, LinkerContext context, ArtifactSet artifacts)
        throws UnableToCompleteException {
    logger = logger.branch(TreeLogger.DEBUG, "Building gadget manifest", null);

    String bootstrap = "<script>"
            + context.optimizeJavaScript(logger, generateSelectionScript(logger, context, artifacts))
            + "</script>\n" + "<div id=\"__gwt_gadget_content_div\"></div>";

    // Read the content
    StringBuffer manifest = new StringBuffer();

    try {/*  ww  w . ja va  2  s.c om*/
        BufferedReader in = new BufferedReader(new InputStreamReader(manifestArtifact.getContents(logger)));

        for (String line = in.readLine(); line != null; line = in.readLine()) {
            manifest.append(line).append("\n");
        }

        in.close();
    } catch (IOException e) {
        logger.log(TreeLogger.ERROR, "Unable to read manifest stub", e);
        throw new UnableToCompleteException();
    }

    replaceAll(manifest, "__BOOTSTRAP__", bootstrap);

    return emitString(logger, manifest.toString(), manifestArtifact.getPartialPath());
}

From source file:gwt.ns.webworker.linker.WorkerModuleLinker.java

License:Apache License

@Override
protected EmittedArtifact emitSelectionScript(TreeLogger logger, LinkerContext context, ArtifactSet artifacts)
        throws UnableToCompleteException {

    DefaultTextOutput out = new DefaultTextOutput(true);

    // Emit the selection script from template
    String bootstrap = generateSelectionScript(logger, context, artifacts);
    bootstrap = context.optimizeJavaScript(logger, bootstrap);
    out.print(bootstrap);/*w  w w  .  j  a v a  2s . co  m*/
    out.newlineOpt();

    // Emit the module's JS within a closure.
    out.print("(function () {");
    out.newlineOpt();
    out.print("var $gwt_version = \"" + About.getGwtVersionNum() + "\";");
    out.newlineOpt();

    /* Point $wnd and $doc to insideWorker global scope. Shouldn't be used, but there
     * in case preexisting code uses either as a generic global variable
     * normal access of $wnd and $doc attributes and methods will be broken,
     * per Worker spec
     */
    out.print("var $self = self;");
    out.newlineOpt();
    out.print("var $wnd = self;");
    out.newlineOpt();
    out.print("var $doc = self;");
    out.newlineOpt();

    out.print("var $moduleName, $moduleBase;"); //needed if no stats/error handling?
    out.newlineOpt();
    out.print("var $stats = null;");
    out.newlineOpt();

    // append module code

    // Find the single CompilationResult
    Set<CompilationResult> results = artifacts.find(CompilationResult.class);
    if (results.size() != 1) {
        logger.log(TreeLogger.ERROR, "The module must have exactly one distinct"
                + " permutation when using the " + getDescription() + " Linker.", null);

        // give a hint to reason for failure
        logPermutationProperties(logger, context.getProperties());

        throw new UnableToCompleteException();
    }
    CompilationResult result = results.iterator().next();

    out.print("var $strongName = '" + result.getStrongName() + "';");
    out.newlineOpt();

    // get actual compiled javascript and output
    // only one fragment currently supported (no runAsync)
    String[] js = result.getJavaScript();
    if (js.length != 1) {
        logger.log(TreeLogger.ERROR,
                "The module must not have multiple fragments when using the " + getDescription()
                        + " Linker. Use of GWT.runAsync within Worker"
                        + " code is the most likely cause of this error.",
                null);
        throw new UnableToCompleteException();
    }
    out.print(js[0]);

    // Generate the call to tell the bootstrap code that we're ready to go.
    out.newlineOpt();
    out.print("if (" + context.getModuleFunctionName() + ") " + context.getModuleFunctionName()
            + ".onScriptLoad(gwtOnLoad);");
    out.newlineOpt();
    out.print("})();");
    out.newlineOpt();

    // TODO: this naming scheme helps WorkerCompilationLinker, but users
    // compiling separate worker scripts may desire a strong file name
    return emitString(logger, out.toString(), context.getModuleName() + WORKER_EXTENSION);
}

From source file:org.cruxframework.crux.core.rebind.offline.AppCacheLinker.java

License:Apache License

private String generateSelectionScript(TreeLogger logger, LinkerContext context, ArtifactSet artifacts)
        throws UnableToCompleteException {
    String selectionScriptText;//from www.  ja va2s  .  co m
    StringBuffer buffer = readFileToStringBuffer(getSelectionScriptTemplate(logger, context), logger);
    appendProcessMetas(logger, context, buffer);
    appendPageLoaderFunction(logger, context, buffer);
    selectionScriptText = fillSelectionScriptTemplate(buffer, logger, context, artifacts);
    // fix for some browsers like IE that cannot see the $doc variable outside the iframe tag.
    selectionScriptText = selectionScriptText.replace("$doc", "document");

    selectionScriptText = context.optimizeJavaScript(logger, selectionScriptText);
    return selectionScriptText;
}

From source file:org.gwtproject.gwt.worker.linker.SingleScriptLinker.java

License:Apache License

@Override
protected EmittedArtifact emitSelectionScript(TreeLogger logger, LinkerContext context, ArtifactSet artifacts)
        throws UnableToCompleteException {

    // Find the single Script result
    Set<Script> results = artifacts.find(Script.class);
    ensureSinglePermutation(logger, context, results);
    Script result = results.iterator().next();

    DefaultTextOutput out = new DefaultTextOutput(true);

    // Emit the selection script.
    String bootstrap = generateSelectionScript(logger, context, artifacts);
    bootstrap = context.optimizeJavaScript(logger, bootstrap);
    out.print(bootstrap);/* w  w  w  . j a  v  a2  s  .com*/
    out.newlineOpt();

    // Emit the module's JS a closure.
    out.print("(function () {");
    out.newlineOpt();
    out.print("var $gwt_version = \"" + About.getGwtVersionNum() + "\";");
    out.newlineOpt();

    defineJsWndAndDoc(out);

    out.print("var $moduleName, $moduleBase;");
    out.newlineOpt();
    out.print("var $stats = $wnd.__gwtStatsEvent ? function(a) {$wnd.__gwtStatsEvent(a)} : null;");
    out.newlineOpt();

    out.print("var $strongName = '" + result.getStrongName() + "';");
    out.newlineOpt();

    out.print(result.getJavaScript());

    // Generate the call to tell the bootstrap code that we're ready to go.
    out.newlineOpt();
    out.print("if (" + context.getModuleFunctionName() + ") " + context.getModuleFunctionName()
            + ".onScriptLoad(gwtOnLoad);");
    out.newlineOpt();
    out.print("})();");
    out.newlineOpt();

    return emitString(logger, out.toString(),
            context.getModuleName() + getCompilationExtension(logger, context));
}

From source file:uk.co.thinkofdeath.thinkcraft.html.linker.WorkerLinker.java

License:Apache License

@Override
protected EmittedArtifact emitSelectionScript(TreeLogger logger, LinkerContext context, ArtifactSet artifacts)
        throws UnableToCompleteException {
    Set<WorkerScript> scripts = artifacts.find(WorkerScript.class);
    if (scripts.size() != 1) {
        logger.branch(TreeLogger.ERROR, "Too many scripts");
        throw new UnableToCompleteException();
    }/*from   w w  w  .j a v a2 s  .  c om*/
    WorkerScript script = scripts.iterator().next();

    DefaultTextOutput output = new DefaultTextOutput(true);

    output.print(context.optimizeJavaScript(logger, generateSelectionScript(logger, context, artifacts)));
    output.newlineOpt();
    output.print(script.javascript);
    output.newlineOpt();
    output.print("gwtOnLoad(null, \"__MODULE_NAME__\", null);");

    return emitString(logger, output.toString(), context.getModuleName() + ".worker.js");
}