Class RewriteGoogJsImports

  • All Implemented Interfaces:
    CompilerPass

    public class RewriteGoogJsImports
    extends java.lang.Object
    implements CompilerPass
    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

      • 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