diff options
author | murgatroid99 <mlumish@google.com> | 2016-02-24 13:44:57 -0800 |
---|---|---|
committer | murgatroid99 <mlumish@google.com> | 2016-02-24 13:44:57 -0800 |
commit | a862b6b77f49527bf1f6e1da54265d82256c4bf1 (patch) | |
tree | 3ce9fdde957b0fea1193b8384c17a15e5ac02b82 /src | |
parent | 8f67b165f0a949219fafc48c533be3fbf53497b7 (diff) |
Fix CommonJS relative require generation, and test it
Diffstat (limited to 'src')
-rwxr-xr-x | src/google/protobuf/compiler/js/js_generator.cc | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/src/google/protobuf/compiler/js/js_generator.cc b/src/google/protobuf/compiler/js/js_generator.cc index 351c3966..0de7e2c6 100755 --- a/src/google/protobuf/compiler/js/js_generator.cc +++ b/src/google/protobuf/compiler/js/js_generator.cc @@ -149,6 +149,20 @@ string GetJSFilename(const string& filename) { return StripSuffixString(filename, suffix) + "_pb.js"; } +// Given a filename like foo/bar/baz.proto, returns the root directory +// path ../../ +string GetRootPath(const string& filename) { + size_t slashes = std::count(filename.begin(), filename.end(), '/'); + if (slashes == 0) { + return "./"; + } + string result = ""; + for (size_t i = 0; i < slashes; i++) { + result += "../"; + } + return result; +} + // Returns the alias we assign to the module of the given .proto filename // when importing. string ModuleAlias(const string& filename) { @@ -2518,7 +2532,7 @@ void Generator::GenerateFile(const GeneratorOptions& options, printer->Print( "var $alias$ = require('$file$');\n", "alias", ModuleAlias(name), - "file", GetJSFilename(name)); + "file", GetRootPath(file->name()) + GetJSFilename(name)); } } |