在谷歌闭包编译器中使用模块

4 javascript compiler-errors google-closure-compiler

我只是想让一个基本模块正常工作:

测试A.js:

goog.module('foo');

exports.bar = function(){
  console.log('here');
};
Run Code Online (Sandbox Code Playgroud)

测试B.js:

var bar = goog.require('foo.bar');
bar();
Run Code Online (Sandbox Code Playgroud)

我正在跑步:

java -jar ~/Downloads/compiler.jar --js testA.js testB.js
Run Code Online (Sandbox Code Playgroud)

产生:

testB.js:1: ERROR - Closure dependency methods(goog.provide, goog.require, etc) must be called at file scope.
var bar = goog.require('foo.bar');
          ^

1 error(s), 0 warning(s)
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

小智 5

我自己回答这个问题。因此,您不能正常地从“正常”闭包编译.js文件中要求模块,只能从模块文件中要求模块,该文件似乎被定义为goog.module其中包含的任何文件。

所以如果在一个模块中,goog.require('module')工作得很好。如果模块中没有,goog.module.get('module')则需要