Skip to content

Commit deff1ed

Browse files
committed
fixup ES6 import support (alongside commonjs and closure modules)
This work is largely based on protocolbuffers#156
1 parent 9722ead commit deff1ed

22 files changed

Lines changed: 21877 additions & 104 deletions

MODULE.bazel.lock

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

commonjs/export_testdeps.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ if (typeof exports === 'object') {
4848
'array': goog.array,
4949
'object': goog.object,
5050
'requireType': goog.requireType,
51+
'typeOf': goog.typeOf,
52+
'inherits': goog.inherits,
5153
};
5254

5355
exports['jspb'] = {

es6/import_test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import googleProtobuf from 'google-protobuf';
2+
import * as test7_pb from './test7/test7_pb.mjs';
3+
4+
const global = globalThis;
5+
googleProtobuf.exportSymbol('jspb.Message', googleProtobuf.Message, global);
6+
googleProtobuf.exportSymbol('proto.jspb.test.framing.FramingMessage', test7_pb.FramingMessage, global);
7+
8+
describe('Import test suite', () => {
9+
it('testImportedMessage', () => {
10+
const framing1 = new proto.jspb.test.framing.FramingMessage([]);
11+
const framing2 = new proto.jspb.test.framing.FramingMessage([]);
12+
expect(framing1.toObject()).toEqual(framing2.toObject());
13+
});
14+
});

es6/rewrite_tests_for_es6.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* @fileoverview Utility to translate test files to ES6 imports.
3+
*/
4+
5+
var lineReader = require('readline').createInterface({
6+
input: process.stdin,
7+
output: process.stdout
8+
});
9+
10+
function tryStripPrefix(str, prefix) {
11+
if (str.lastIndexOf(prefix) !== 0) {
12+
throw "String: " + str + " didn't start with: " + prefix;
13+
}
14+
return str.substr(prefix.length);
15+
}
16+
17+
function camelCase(str) {
18+
var ret = '';
19+
var ucaseNext = false;
20+
for (var i = 0; i < str.length; i++) {
21+
if (str[i] == '-') {
22+
ucaseNext = true;
23+
} else if (ucaseNext) {
24+
ret += str[i].toUpperCase();
25+
ucaseNext = false;
26+
} else {
27+
ret += str[i];
28+
}
29+
}
30+
return ret;
31+
}
32+
33+
var module = null;
34+
var pkg = null;
35+
36+
// Header
37+
console.log("import googleProtobuf from 'google-protobuf';");
38+
console.log("import testdeps from 'testdeps_commonjs';");
39+
console.log("globalThis.COMPILED = testdeps.COMPILED;");
40+
console.log("globalThis.goog = testdeps.goog;");
41+
console.log("globalThis.googleProtobuf = googleProtobuf;");
42+
console.log("");
43+
44+
lineReader.on('line', function (line) {
45+
const isModuleGet = line.match(/(.*)goog\.module\.get\('([^']*)'\)([^;]*);/);
46+
if (isModuleGet) {
47+
const fullSym = isModuleGet[2];
48+
console.log(isModuleGet[1] + "testdeps." + fullSym + isModuleGet[3]);
49+
return;
50+
}
51+
var isRequire = line.match(/goog\.require\('([^']*)'\)/);
52+
var isLoadFromFile = line.match(/CommonJS-LoadFromFile: (\S*) (.*)/);
53+
var isSetTestOnly = line.match(/goog.setTestOnly()/);
54+
if (isRequire) {
55+
if (module) {
56+
var fullSym = isRequire[1];
57+
if (fullSym.match(/^jspb\./)) return;
58+
var sym = tryStripPrefix(fullSym, pkg);
59+
console.log("googleProtobuf.exportSymbol('" + fullSym + "', " + module + sym + ', globalThis);');
60+
}
61+
} else if (isLoadFromFile) {
62+
var module_path = isLoadFromFile[1].split('/');
63+
module = camelCase(module_path[module_path.length - 1]);
64+
pkg = isLoadFromFile[2];
65+
66+
if (module != "googleProtobuf") {
67+
console.log("import * as " + module + " from './" + isLoadFromFile[1] + ".mjs';");
68+
}
69+
} else if (!isSetTestOnly) {
70+
console.log(line);
71+
}
72+
});

es6/strict_test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import googleProtobuf from 'google-protobuf';
2+
import * as test9_pb from './protos/test9_pb.mjs';
3+
import * as test10_pb from './protos/test10_pb.mjs';
4+
5+
const global = globalThis;
6+
7+
describe('Strict test suite', () => {
8+
it('testImportedMessage', () => {
9+
const simple1 = new test9_pb.Simple9()
10+
const simple2 = new test9_pb.Simple9()
11+
expect(simple1.toObject()).toEqual(simple2.toObject());
12+
});
13+
14+
it('testGlobalScopePollution', () => {
15+
expect(global.jspb.exttest).toBeUndefined();
16+
});
17+
18+
describe('with imports', () => {
19+
it('testImportedMessage', () => {
20+
const simple1 = new test10_pb.Simple10()
21+
const simple2 = new test10_pb.Simple10()
22+
expect(simple1.toObject()).toEqual(simple2.toObject());
23+
});
24+
25+
it('testGlobalScopePollution', () => {
26+
expect(global.jspb.exttest).toBeUndefined();
27+
});
28+
});
29+
});

0 commit comments

Comments
 (0)