GWT Linkers target HTML5 Web Workers, Chrome Extensions, and more
Matt Mastracci ([email protected], @mmastrac) May 19, 2010 View live notes and ask questions about this session on Google Wave http://bit.ly/io2010-gwt4 Agenda
Linkers How GWT loads code What is a linker? Artifacts Linkers and chains SelectionScriptLinker and Web Worker linker
Demo: build a linker
Advanced linkers Code splitting Development mode Upcoming features What is a linker? How GWT Loads Code
Four step process: Load the host page Load the selection script Load the permutation script Load any runAsync fragments (on-demand) How GWT Loads Code (2) How GWT Loads Code (3) How GWT Loads Code (4) How GWT Loads Code (5) What is a Linker?
Shapes the output Javascript into different forms Same input code, different packaging Makes GWT more flexible! What is a Linker?
Three output forms for your application: IFrameLinker (default) XSLinker SingleScriptLinker
Each form is a linker! Switching Linkers (2)
A: Metadata. Artifacts public abstract class Artifact
Products of the compilation process Provenance metadata: browser/language permutations Artifacts
Can be created by: scripts and styles specified in module XML files in the module's public path the compiler (script artifacts) generators (via GWT.create)
Gives you a channel to send messages to a linker from a generator (more on this later) Artifact Sets
Artifacts passed from compiler and through linker chain in ArtifactSets
Sorted collection of artifacts Type-aware find() Artifact Types
Final output composed of EmittedArtifacts from the artifact set An artifact that not a subclass of EmittedArtifact is not written to disk
But linkers can still find and act on them! Artifact Flow
Output consists of emitted artifacts from the artifact set Artifact Flow (2)
File in the public path are added directly Artifact Flow (3)
Generators contribute binary and other artifacts Compiler contributes CompilationResults Artifact Flow (4)
Linkers consume and contribute artifacts Linkers Linker Order
public enum LinkerOrder.Order { POST, PRE, PRIMARY; } Order.PRIMARY
Responsible for the overall "shape" of the output Controls how the generated Javascript is packaged and run There can be only one!
Examples:
IFrame and cross-site linkers Firefox extension linker HTML5 Web Worker linker Chrome extension linker Order.PRIMARY (2)
Caveats: Bootstrap sequence and global variables required by GWT change between versions "Soft Permutations" are coming soon, will require some changes to linkers Order.PRE
Processes artifacts before the primary linker runs Ideal place to post-process generator artifacts and turn them into a concrete, emitted file
Example: GWT's RpcPolicyManifestLinker Gathers RpcPolicyFileArtifact generated at compile time by ProxyCreator Summarizes the policy files into one RPC policy manifest Order.POST
Post-processing/decoration for generated primary artifacts Repackage, rename, transcode, augment
Example: Offline application manifest (demo) SelectionScriptLinker
Most primary linkers will subclass SelectionScriptLinker Does the heavy lifting for permutations
Outputs: the selection script file: mymodule.nocache.js one strongly-named compilation result per permutation any per-compilation-result deferred fragments Example: Web Worker Linker
LinkerOrder.PRIMARY Subclass of SelectionScriptLinker Written by the Speed Tracer team http://code.google.com/p/speedtracer/
Load GWT module as an HTML5 Web Worker! new Worker('module.nocache.js') Example: Web Worker Linker
Linker: DedicatedWorkerLinker.java @LinkerOrder(Order.PRIMARY) public class DedicatedWorkerLinker extends SelectionScriptLinker { getDescription() { return "Dedicated Web Worker Linker"; } getCompilationExtension(logger, linkerContext) { return ".cache.js"; } getModulePrefix(logger, linkerContext, strongName) { return ""; } getModuleSuffix(logger, linkerContext) { return ""; } getSelectionScriptTemplate(logger, linkerContext) { return "DedicatedWorkerTemplate.js"; } } Example: Web Worker Linker
Selection script: DedicatedWorkerTemplate.js function __MODULE_FUNC__() { var strongName; try { // __PERMUTATIONS_BEGIN__ // Permutation logic // __PERMUTATIONS_END__ } catch (e) { // intentionally silent on property failure return; } importScripts(strongName + ".cache.js"); gwtOnLoad(undefined, '__MODULE_NAME__', ''); }
__MODULE_FUNC__(); Demo HTML Offline Manifest
Allows applications to run without an internet connection
Spec: http://www.w3.org/TR/offline-webapps/
HTML Offline Manifest
my.manifest:
CACHE MANIFEST index.html help.html style/default.css images/logo.png images/backgound.png Demo Advanced Linkers Code Splitting
New in GWT 2.0 Compiler splits fragments across GWT.runAsync() calls runAsync() call delegated to a LoadingStrategy Code Splitting
Default XhrLoadingStrategy will handle nearly all cases
Learn more: com.google.gwt.core.client.impl.AsyncFragmentLoader Development Mode
New in GWT 2.0 Subtle differences for linking in development mode
Learn more: GWT source: Linker.link() vs. Linker.relink() User discussion group Upcoming Linker Features
Coming soon: Shardable linkers-distributed linking for a distributed compile Soft permutations-permutations switched at runtime Wrap Up Wrap Up
Linkers package, analyze and decorate compiled scripts More structured approach to your output Easy to write most linkers Makes GWT more than just a simple web application compiler
User discussion group: http://groups.google.com/group/Google-Web-Toolkit View live notes and ask questions about this session on Google Wave http://bit.ly/io2010-gwt4