美文网首页
goog closure library 中 goog.base

goog closure library 中 goog.base

作者: onedam | 来源:发表于2020-09-18 18:01 被阅读0次

var COMPILED = false; 首先遇到这个拦路虎

image.png

该变量是因为 closure library 分 开发期 与 编译后的区别 .

在 closure compiler 中 E:\clojure\luminusweb\closure-compiler\src\com\google\javascript\jscomp\deps\DependencyInfo.java

该文件中在编译器重新实现了 Require 机制.. 因为需要加载goog.base.js 文件进来. 所以进来的时候直接把
里面的 COMPILED 置位true .这样在以后分析的时候就排除了干扰.变成了closure编译器自己实现的.

com.google.javascript.jscomp.deps.JsFileRegexParser.java 
@Override
  protected boolean parseLine(String line) throws ParseException {
    boolean lineHasProvidesOrRequires = false;

    if (line.startsWith(BUNDLED_GOOG_MODULE_START)) {
      seenLoadModule = true;
    }

    // Quick check that will catch most cases. This is a performance win for teams with a lot of JS.
    if (line.contains("provide")
        || line.contains("require")
        || line.contains("module")
        || line.contains("addDependency")
        || line.contains("declareModuleId")) {
      // Iterate over the provides/requires.
      googMatcher.reset(line);
      while (googMatcher.find()) {
        lineHasProvidesOrRequires = true;

        if (includeGoogBase && !fileHasProvidesOrRequires) {
          fileHasProvidesOrRequires = true;
          requires.add(Require.BASE);
        }

        // See if it's a require or provide.
        String methodName = googMatcher.group("func");
        char firstChar = methodName.charAt(0);
        boolean isDeclareModuleNamespace = firstChar == 'd';
        boolean isModule = !isDeclareModuleNamespace && firstChar == 'm';
        boolean isProvide = firstChar == 'p';
        boolean providesNamespace = isProvide || isModule || isDeclareModuleNamespace;
        boolean isRequire = firstChar == 'r';

        if (isModule && !seenLoadModule) {
          providesNamespace = setModuleType(ModuleType.GOOG_MODULE);
        }

        if (isProvide) {
          providesNamespace = setModuleType(ModuleType.GOOG_PROVIDE);
        }

        if (providesNamespace || isRequire) {
          // Parse the param.
          String arg = parseJsString(googMatcher.group("args"));
          // Add the dependency.
          if (isRequire) {
            if ("requireType".equals(methodName)) {
              typeRequires.add(arg);
            } else if (!"goog".equals(arg)) {
              // goog is always implicit.
              Require require = Require.googRequireSymbol(arg);
              requires.add(require);
            }
          } else {
            provides.add(arg);
          }
        }
      }
    }

    if (line.startsWith("import") || line.startsWith("export")) {
      es6Matcher.reset(line);
      while (es6Matcher.find()) {
        setModuleType(ModuleType.ES6_MODULE);
        lineHasProvidesOrRequires = true;

        String arg = es6Matcher.group(1);
        if (arg != null) {
          if (arg.startsWith("goog:")) {
            // cut off the "goog:" prefix
            requires.add(Require.googRequireSymbol(arg.substring(5)));
          } else {
            ModuleLoader.ModulePath path =
                file.resolveJsModule(arg, filePath, lineNum, es6Matcher.start());
            if (path == null) {
              path = file.resolveModuleAsPath(arg);
            }
            requires.add(Require.es6Import(path.toModuleName(), arg));
          }
        }
      }

      // This check is only relevant for modules that don't import anything.
      if (moduleType != ModuleType.ES6_MODULE && ES6_EXPORT_PATTERN.matcher(line).lookingAt()) {
        setModuleType(ModuleType.ES6_MODULE);
      }
    }

    return !shortcutMode || lineHasProvidesOrRequires
        || CharMatcher.whitespace().matchesAllOf(line)
        || !line.contains(";")
        || line.contains("goog.setTestOnly")
        || line.contains("goog.module.declareLegacyNamespace");
  }

goog.addDependency(relativePath, provides, requires)
goog.addDependency() is used to construct and manage the dependency graph used by
goog.require() in uncompiled JavaScript. When JavaScript is compiled with the Closure Compiler, goog.provide() and goog.require() are analyzed at compile time to
ensure that no namespace is required before it is provided. If this check completes
successfully, calls to goog.provide() are replaced with logic to construct the object on
which the namespace will be built, and calls to goog.require() are removed entirely.
In the compiled case, there is no need for the dependency graph to be constructed on
the client, so goog.addDependency() and the global constants it depends on are defined
in such a way that they will be stripped from the output when COMPILED is true.
By comparison, uncompiled Closure code relies on the information from goog.add
Dependency() to determine which additional JavaScript files to load. Consider the
following example, in which the example.View namespace depends on the example
.Model namespace:

相关文章

网友评论

      本文标题:goog closure library 中 goog.base

      本文链接:https://www.haomeiwen.com/subject/hgwjyktx.html