Class ProcessCommonJSModules

  • All Implemented Interfaces:
    CompilerPass, NodeTraversal.Callback

    public final class ProcessCommonJSModules
    extends NodeTraversal.AbstractPreOrderCallback
    implements CompilerPass
    Rewrites a CommonJS module http://wiki.commonjs.org/wiki/Modules/1.1.1 into a form that can be safely concatenated. Does not add a function around the module body but instead adds suffixes to global variables to avoid conflicts. Calls to require are changed to reference the required module directly.
    • Field Detail

      • UNKNOWN_REQUIRE_ENSURE

        public static final DiagnosticType UNKNOWN_REQUIRE_ENSURE
      • SUSPICIOUS_EXPORTS_ASSIGNMENT

        public static final DiagnosticType SUSPICIOUS_EXPORTS_ASSIGNMENT
    • Constructor Detail

      • ProcessCommonJSModules

        public ProcessCommonJSModules​(AbstractCompiler compiler)
        Creates a new ProcessCommonJSModules instance which can be used to rewrite CommonJS modules to a concatenable form.
        Parameters:
        compiler - The compiler
    • 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
      • getModuleName

        public static java.lang.String getModuleName​(CompilerInput input)
      • getBasePropertyImport

        public java.lang.String getBasePropertyImport​(java.lang.String moduleName)
      • isCommonJsImport

        public boolean isCommonJsImport​(Node requireCall)
      • isCommonJsImport

        public static boolean isCommonJsImport​(Node requireCall,
                                               ModuleLoader.ResolutionMode resolutionMode)
        Recognize if a node is a module import. We recognize two forms:
        • require("something");
        • __webpack_require__(4); // only when the module resolution is WEBPACK
        • __webpack_require__.t(4); // only when the module resolution is WEBPACK
      • getCommonJsImportPath

        public java.lang.String getCommonJsImportPath​(Node requireCall)
      • isCommonJsExport

        public static boolean isCommonJsExport​(NodeTraversal t,
                                               Node export,
                                               ModuleLoader.ResolutionMode resolutionMode)
        Recognize if a node is a module export. We recognize several forms:
        • module.exports = something;
        • module.exports.something = something;
        • exports.something = something;

        In addition, we only recognize an export if the base export object is not defined or is defined in externs.

      • isCommonJsDynamicImportCallback

        public static boolean isCommonJsDynamicImportCallback​(Node n,
                                                              ModuleLoader.ResolutionMode resolutionMode)
        Recognize if a node is a dynamic module import. Currently only the webpack dynamic import is recognized:
        • __webpack_require__.e(0).then(function() { return __webpack_require__(4);})
        • Promise.all([__webpack_require__.e(0)]).then(function() { return __webpack_require__(4);})