Search in sources :

Example 1 with ModuleMap

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);
    }
}
Also used : ModulePath(com.google.javascript.jscomp.deps.ModuleLoader.ModulePath) Node(com.google.javascript.rhino.Node) Module(com.google.javascript.jscomp.modules.Module) ModuleMap(com.google.javascript.jscomp.modules.ModuleMap)

Example 2 with ModuleMap

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);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) ModulePath(com.google.javascript.jscomp.deps.ModuleLoader.ModulePath) Node(com.google.javascript.rhino.Node) Module(com.google.javascript.jscomp.modules.Module) ModuleMap(com.google.javascript.jscomp.modules.ModuleMap)

Example 3 with ModuleMap

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();
}
Also used : Module(com.google.javascript.jscomp.modules.Module) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ModuleMap(com.google.javascript.jscomp.modules.ModuleMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ModuleMap(com.google.javascript.jscomp.modules.ModuleMap)

Aggregations

Module (com.google.javascript.jscomp.modules.Module)3 ModuleMap (com.google.javascript.jscomp.modules.ModuleMap)3 ModulePath (com.google.javascript.jscomp.deps.ModuleLoader.ModulePath)2 Node (com.google.javascript.rhino.Node)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 JSType (com.google.javascript.rhino.jstype.JSType)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1