aboutsummaryrefslogtreecommitdiffhomepage
path: root/js/commonjs
diff options
context:
space:
mode:
authorGravatar Josh Haberman <jhaberman@gmail.com>2016-02-04 10:38:06 -0800
committerGravatar Josh Haberman <jhaberman@gmail.com>2016-02-18 10:30:21 -0800
commit9e60036c1b1db947d29cbaaa668aeae19e7d2068 (patch)
treea4803ea2a242988c543710f4b6979771a59b0f87 /js/commonjs
parente9f31ee3d7cf7c0f370607e54dbea01ba7240a77 (diff)
Moved CommonJS-specific files to commonjs/.
Diffstat (limited to 'js/commonjs')
-rw-r--r--js/commonjs/export.js11
-rw-r--r--js/commonjs/export_asserts.js27
-rw-r--r--js/commonjs/jasmine.json9
-rw-r--r--js/commonjs/rewrite_tests_for_commonjs.js45
4 files changed, 92 insertions, 0 deletions
diff --git a/js/commonjs/export.js b/js/commonjs/export.js
new file mode 100644
index 00000000..ffbeb9db
--- /dev/null
+++ b/js/commonjs/export.js
@@ -0,0 +1,11 @@
+/**
+ * @fileoverview Export symbols needed by generated code in CommonJS style.
+ */
+
+exports.Message = jspb.Message;
+exports.BinaryReader = jspb.BinaryReader;
+exports.BinaryWriter = jspb.BinaryWriter;
+exports.ExtensionFieldInfo = jspb.ExtensionFieldInfo;
+
+exports.exportSymbol = goog.exportSymbol;
+exports.inherits = goog.inherits;
diff --git a/js/commonjs/export_asserts.js b/js/commonjs/export_asserts.js
new file mode 100644
index 00000000..16abdd7d
--- /dev/null
+++ b/js/commonjs/export_asserts.js
@@ -0,0 +1,27 @@
+/**
+ * @fileoverview Description of this file.
+ */
+
+goog.require('goog.testing.asserts');
+
+var global = Function('return this')();
+
+// The Google Closure assert functions start with assert, eg.
+// assertThrows
+// assertNotThrows
+// assertTrue
+// ...
+//
+// The one exception is the "fail" function.
+function shouldExport(str) {
+ return str.lastIndexOf('assert') === 0 || str == 'fail';
+}
+
+for (var key in global) {
+ if ((typeof key == "string") && global.hasOwnProperty(key) &&
+ shouldExport(key)) {
+ exports[key] = global[key];
+ }
+}
+
+exports.COMPILED = COMPILED
diff --git a/js/commonjs/jasmine.json b/js/commonjs/jasmine.json
new file mode 100644
index 00000000..666b8edb
--- /dev/null
+++ b/js/commonjs/jasmine.json
@@ -0,0 +1,9 @@
+{
+ "spec_dir": "",
+ "spec_files": [
+ "*_test.js",
+ "binary/proto_test.js"
+ ],
+ "helpers": [
+ ]
+}
diff --git a/js/commonjs/rewrite_tests_for_commonjs.js b/js/commonjs/rewrite_tests_for_commonjs.js
new file mode 100644
index 00000000..4dc0a22d
--- /dev/null
+++ b/js/commonjs/rewrite_tests_for_commonjs.js
@@ -0,0 +1,45 @@
+/**
+ * @fileoverview Description of this file.
+ */
+
+var lineReader = require('readline').createInterface({
+ input: process.stdin,
+ output: process.stdout
+});
+
+var module = null;
+lineReader.on('line', function(line) {
+ var is_require = line.match(/goog\.require\('([^']*\.)([^'.]+)'\)/);
+ var is_loadfromfile = line.match(/CommonJS-LoadFromFile: (.*)/);
+ var is_settestonly = line.match(/goog.setTestOnly()/);
+ if (is_settestonly) {
+ // Remove this line.
+ } else if (is_require) {
+ if (module) { // Skip goog.require() lines before the first directive.
+ var pkg = is_require[1];
+ var sym = is_require[2];
+ console.log("google_protobuf.exportSymbol('" + pkg + sym + "', " + module + "." + sym + ', global);');
+ }
+ } else if (is_loadfromfile) {
+ if (!module) {
+ console.log("var asserts = require('closure_asserts_commonjs');");
+ console.log("var global = Function('return this')();");
+ console.log("");
+ console.log("// Bring asserts into the global namespace.");
+ console.log("for (var key in asserts) {");
+ console.log(" if (asserts.hasOwnProperty(key)) {");
+ console.log(" global[key] = asserts[key];");
+ console.log(" }");
+ console.log("}");
+ console.log("");
+ console.log("var google_protobuf = require('google-protobuf');");
+ }
+ module = is_loadfromfile[1].replace("-", "_");
+
+ if (module != "google_protobuf") { // We unconditionally require this in the header.
+ console.log("var " + module + " = require('" + is_loadfromfile[1] + "');");
+ }
+ } else {
+ console.log(line);
+ }
+});