user2514982
user2514982

Reputation: 51

RequireJS with Google Apps Script

I am wondering if it is possible to use RequireJS with Google Apps Script. I am loading RequireJS 2.1.5 from a GAS library. This sample app should log the word Monkey but nothing happens when I test it. No errors or warnings are being generated either.

index.html:

<!doctype html>
<head>
  <title></title>
</head>
<body>
  <script data-main="main" src="http://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.5/require.js"></script>
</body>
</html>

main.gs:

var define = Require.loadDefine();
var require = Require.loadRequire();

define( function() {
  Logger.log("Monkey");
});

code.gs:

function doGet(e) {
  Logger.log("doGet");
  return HtmlService.createHtmlOutputFromFile('index').setSandboxMode(HtmlService.SandboxMode.NATIVE); 

  var t;
  t = HtmlService.createTemplateFromFile('index');
  return t.evaluate();
}

Upvotes: 3

Views: 2470

Answers (1)

user2514982
user2514982

Reputation: 51

I found the answer while reading JavaScript Module Pattern: In-Depth

The code inside the module didn't have access to Logger. Passing this as a parameter makes it work.

define( function(g) {
  g.Logger.log("Monkey");
}(this));

Upvotes: 2

Related Questions