Package com.google.javascript.jscomp
Class ProcessCommonJSModules
- java.lang.Object
-
- com.google.javascript.jscomp.NodeTraversal.AbstractPreOrderCallback
-
- com.google.javascript.jscomp.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 Summary
Fields Modifier and Type Field Description static DiagnosticType
SUSPICIOUS_EXPORTS_ASSIGNMENT
static DiagnosticType
UNKNOWN_REQUIRE_ENSURE
-
Constructor Summary
Constructors Constructor Description ProcessCommonJSModules(AbstractCompiler compiler)
Creates a new ProcessCommonJSModules instance which can be used to rewrite CommonJS modules to a concatenable form.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
getBasePropertyImport(java.lang.String moduleName)
java.lang.String
getCommonJsImportPath(Node requireCall)
static java.lang.String
getCommonJsImportPath(Node requireCall, ModuleLoader.ResolutionMode resolutionMode)
static @Nullable java.lang.String
getModuleName(CompilerInput input)
static java.lang.String
getModuleName(ModuleLoader.ModulePath input)
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(Node externs, Node root)
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 its children should be traversed.-
Methods inherited from class com.google.javascript.jscomp.NodeTraversal.AbstractPreOrderCallback
visit
-
-
-
-
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 interfaceCompilerPass
- Parameters:
externs
- Top of external JS treeroot
- Top of JS tree
-
shouldTraverse
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent)
Description copied from interface:NodeTraversal.Callback
Visits a node in preorder (before its children) and decides whether its children should be traversed. If the children should be traversed, they will be visited byNodeTraversal.Callback.shouldTraverse(NodeTraversal, Node, Node)
in preorder and byNodeTraversal.Callback.visit(NodeTraversal, Node, Node)
in postorder.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
public static @Nullable java.lang.String getModuleName(CompilerInput input)
-
getModuleName
public static java.lang.String getModuleName(ModuleLoader.ModulePath 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)
-
getCommonJsImportPath
public static java.lang.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);})
-
-