use of com.google.javascript.jscomp.modules.ModuleMap in project closure-compiler by google.
the class RewriteDynamicImports method visit.
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.getToken() != Token.DYNAMIC_IMPORT) {
return;
}
// If the module specifier is a string, attempt to resolve the module
final ModuleMap moduleMap = compiler.getModuleMap();
final Node importSpecifier = n.getFirstChild();
if (importSpecifier.isStringLit() && moduleMap != null) {
final ModulePath targetPath = t.getInput().getPath().resolveJsModule(importSpecifier.getString(), n.getSourceFileName(), n.getLineno(), n.getCharno());
final Module module = (targetPath == null) ? null : compiler.getModuleMap().getModule(targetPath);
final String targetModuleVarName = (module == null) ? null : GlobalizedModuleName.create(module.metadata(), null, null).aliasName().join();
final Var targetModuleNS = (targetModuleVarName == null) ? null : t.getScope().getVar(targetModuleVarName);
if (targetModuleNS != null) {
final JSChunk targetModule = targetModuleNS.getInput().getChunk();
// No further rewriting occurs for this case.
if (t.getChunk() == targetModule) {
replaceDynamicImportWithPromise(t, n, targetModuleNS);
return;
} else {
// The target output chunk is recognized and different from the current chunk.
// Retarget the import specifier path to the output chunk path and rewrite
// the import to reference the rewritten global module namespace variable.
retargetImportSpecifier(t, n, targetModule);
if (NodeUtil.isExpressionResultUsed(n)) {
addChainedThen(n, targetModuleNS);
}
}
}
}
if (aliasIsValid()) {
aliasDynamicImport(t, n);
} else if (this.alias != null) {
t.report(n, DYNAMIC_IMPORT_INVALID_ALIAS);
} else if (requiresAliasing) {
t.report(n, DYNAMIC_IMPORT_ALIASING_REQUIRED);
}
}
use of com.google.javascript.jscomp.modules.ModuleMap in project closure-compiler by google.
the class TypeInference method traverseDynamicImport.
private FlowScope traverseDynamicImport(Node dynamicImport, FlowScope scope) {
JSType templateType = registry.getNativeType(JSTypeNative.UNKNOWN_TYPE);
// If the module specifier is a string, attempt to resolve the module
ModuleMap moduleMap = compiler.getModuleMap();
Node importSpecifier = dynamicImport.getFirstChild();
CompilerInput input = compiler.getInput(NodeUtil.getInputId(dynamicImport));
if (importSpecifier.isStringLit() && moduleMap != null && input != null) {
ModulePath targetPath = input.getPath().resolveJsModule(importSpecifier.getString(), importSpecifier.getSourceFileName(), importSpecifier.getLineno(), importSpecifier.getCharno());
Module targetModule = targetPath != null ? moduleMap.getModule(targetPath) : null;
if (targetModule != null) {
// TypedScopeCreator ensures that the MODULE_BODY type is the export namespace type
Node scriptNode = targetModule.metadata().rootNode();
if (scriptNode.hasOneChild() && scriptNode.getFirstChild().isModuleBody()) {
JSType exportNamespaceType = scriptNode != null ? scriptNode.getOnlyChild().getJSType() : null;
if (exportNamespaceType != null) {
templateType = exportNamespaceType;
}
} else {
// Module transpilation has occurred before type inference so a MODULE_BODY node
// no longer exists. Traverse the script to locate the module namespace variable
// and retrieve the type from it.
Node moduleName = NodeUtil.findPreorder(scriptNode, (node) -> node.matchesQualifiedName(targetPath.toModuleName()), Predicates.alwaysTrue());
if (moduleName != null && moduleName.getJSType() != null) {
templateType = moduleName.getJSType();
}
}
}
}
dynamicImport.setJSType(registry.createTemplatizedType(registry.getNativeObjectType(JSTypeNative.PROMISE_TYPE), templateType));
return traverseChildren(dynamicImport, scope);
}
use of com.google.javascript.jscomp.modules.ModuleMap in project closure-compiler by google.
the class RewriteGoogJsImports method changeModules.
private void changeModules() {
ImmutableMap.Builder<String, Module> resolvedModules = ImmutableMap.builder();
ImmutableMap.Builder<String, Module> closureModules = ImmutableMap.builder();
for (Map.Entry<String, Module> m : moduleMap.getModulesByPath().entrySet()) {
Module newModule = moduleReplacements.getOrDefault(m.getValue(), m.getValue());
resolvedModules.put(m.getKey(), newModule);
}
for (Map.Entry<String, Module> m : moduleMap.getModulesByClosureNamespace().entrySet()) {
Module newModule = moduleReplacements.getOrDefault(m.getValue(), m.getValue());
closureModules.put(m.getKey(), newModule);
}
compiler.setModuleMap(new ModuleMap(resolvedModules.buildOrThrow(), closureModules.buildOrThrow()));
moduleReplacements.clear();
}
Aggregations