Class RewriteGoogJsImports

  • All Implemented Interfaces:
    CompilerPass, HotSwapCompilerPass

    public class RewriteGoogJsImports
    extends java.lang.Object
    implements HotSwapCompilerPass
    Looks for references to Closure's goog.js file and globalizes. The goog.js file is an ES6 module that forwards some symbols on goog from Closure's base.js file, like require.

    This pass scans the goog.js file to find what keys are exported. When rewriting imports only those in the goog.js file are globalized; if there is a reference to something not in the goog.js file it is rewritten in such a way to cause an error later at type checking.

    This is a separate pass that needs to run before Es6RewriteModules which will remove import statements.

    This pass enforces the following so that later compiler passes and regex based tools can correctly recognize references to these properties of goog (like goog.require):

    • No other file is named goog.js
    • The file is always imported as import * as goog
    • No other module level variable is named goog
    • export from is never used on goog.js

    Example:

    import * as goog from 'path/to/closure/goog.js'; const myNamespace = goog.require('my.namespace');

    Will be rewritten to:

    import 'path/to/closure/goog.js'; const myNamespace = goog.require('my.namespace');

    • Method Detail

      • hotSwapScript

        public void hotSwapScript​(Node scriptRoot,
                                  Node originalRoot)
        Description copied from interface: HotSwapCompilerPass
        Process the JS with root node root. This is supposed to be significantly faster compared to corresponding full-compiler passes.
        Specified by:
        hotSwapScript in interface HotSwapCompilerPass
        Parameters:
        scriptRoot - Root node corresponding to the file that is modified, should be of type Token.SCRIPT.
        originalRoot - Root node corresponding to the original version of the file that is modified. Should be of type token.SCRIPT.
      • process

        public void process​(Node externs,
                            Node root)
        Description copied from interface: CompilerPass
        Process the JS with root node root. Can modify the contents of each Node tree
        Specified by:
        process in interface CompilerPass
        Parameters:
        externs - Top of external JS tree
        root - Top of JS tree