Class ProcessCommonJSModules
- All Implemented Interfaces:
CompilerPass
,NodeTraversal.Callback
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionProcessCommonJSModules
(AbstractCompiler compiler) Creates a new ProcessCommonJSModules instance which can be used to rewrite CommonJS modules to a concatenable form. -
Method Summary
Modifier and TypeMethodDescriptiongetBasePropertyImport
(String moduleName) getCommonJsImportPath
(Node requireCall) static String
getCommonJsImportPath
(Node requireCall, ModuleLoader.ResolutionMode resolutionMode) static @Nullable String
getModuleName
(CompilerInput input) static String
static boolean
isCommonJsDynamicImportCallback
(Node n, ModuleLoader.ResolutionMode resolutionMode) Recognize if a node is a dynamic module import.static boolean
isCommonJsExport
(NodeTraversal t, Node export, ModuleLoader.ResolutionMode resolutionMode) Recognize if a node is a module export.boolean
isCommonJsImport
(Node requireCall) static boolean
isCommonJsImport
(Node requireCall, ModuleLoader.ResolutionMode resolutionMode) Recognize if a node is a module import.void
Process the JS with root node root.boolean
shouldTraverse
(NodeTraversal t, Node n, Node parent) Visits a node in preorder (before its children) and decides whether the node and its children should be traversed.Methods inherited from class com.google.javascript.jscomp.NodeTraversal.AbstractPreOrderCallback
visit
-
Field Details
-
UNKNOWN_REQUIRE_ENSURE
-
SUSPICIOUS_EXPORTS_ASSIGNMENT
-
-
Constructor Details
-
ProcessCommonJSModules
Creates a new ProcessCommonJSModules instance which can be used to rewrite CommonJS modules to a concatenable form.- Parameters:
compiler
- The compiler
-
-
Method Details
-
process
Description copied from interface:CompilerPass
Process the JS with root node root. Can modify the contents of each Node tree- Specified by:
process
in interfaceCompilerPass
- Parameters:
externs
- Top of external JS treeroot
- Top of JS tree
-
shouldTraverse
Description copied from interface:NodeTraversal.Callback
Visits a node in preorder (before its children) and decides whether the node and its children should be traversed.If this method returns true, the node will be visited by
NodeTraversal.Callback.visit(NodeTraversal, Node, Node)
in postorder and its children will be visited by bothNodeTraversal.Callback.shouldTraverse(NodeTraversal, Node, Node)
in preorder and byNodeTraversal.Callback.visit(NodeTraversal, Node, Node)
in postorder.If this method returns false, the node will not be visited by
NodeTraversal.Callback.visit(NodeTraversal, Node, Node)
and its children will neither be visited byNodeTraversal.Callback.shouldTraverse(NodeTraversal, Node, Node)
norNodeTraversal.Callback.visit(NodeTraversal, Node, Node)
.Siblings are always visited left-to-right.
Implementations can have side-effects (e.g. modify the parse tree). Removing the current node is legal, but removing or reordering nodes above the current node may cause nodes to be visited twice or not at all.
- Specified by:
shouldTraverse
in interfaceNodeTraversal.Callback
- Parameters:
t
- The current traversal.n
- The current node.parent
- The parent of the current node.- Returns:
- whether the children of this node should be visited
-
getModuleName
-
getModuleName
-
getBasePropertyImport
-
isCommonJsImport
-
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
-
getCommonJsImportPath
public static String getCommonJsImportPath(Node requireCall, ModuleLoader.ResolutionMode resolutionMode) -
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);})
-