diff --git a/generator/js_generator.cc b/generator/js_generator.cc index 97b5844..1cd4394 100644 --- a/generator/js_generator.cc +++ b/generator/js_generator.cc @@ -224,7 +224,8 @@ std::string MaybeCrossFileRef(const GeneratorOptions& options, const FileDescriptor* from_file, const Descriptor* to_message) { if ((options.import_style == GeneratorOptions::kImportCommonJs || - options.import_style == GeneratorOptions::kImportCommonJsStrict) && + options.import_style == GeneratorOptions::kImportCommonJsStrict || + options.import_style == GeneratorOptions::kImportEs6) && from_file != to_message->file()) { // Cross-file ref in CommonJS needs to use the module alias instead of // the global name. @@ -3618,8 +3619,14 @@ void Generator::GenerateFile(const GeneratorOptions& options, // Generate "require" statements. if ((options.import_style == GeneratorOptions::kImportCommonJs || - options.import_style == GeneratorOptions::kImportCommonJsStrict)) { - printer->Print("var jspb = require('google-protobuf');\n"); + options.import_style == GeneratorOptions::kImportCommonJsStrict || + options.import_style == GeneratorOptions::kImportEs6)) { + + if (options.import_style == GeneratorOptions::kImportEs6) { + printer->Print("import * as jspb from 'google-protobuf';\n"); + } else { + printer->Print("var jspb = require('google-protobuf');\n"); + } printer->Print("var goog = jspb;\n"); // Do not use global scope in strict mode @@ -3648,13 +3655,22 @@ void Generator::GenerateFile(const GeneratorOptions& options, " Function('return this')();\n\n"); } - for (int i = 0; i < file->dependency_count(); i++) { - const std::string& name = file->dependency(i)->name(); - printer->Print( - "var $alias$ = require('$file$');\n" - "goog.object.extend(proto, $alias$);\n", - "alias", ModuleAlias(name), "file", - GetRootPath(file->name(), name) + GetJSFilename(options, name)); + if (options.import_style == GeneratorOptions::kImportEs6) { + for (int i = 0; i < file->dependency_count(); i++) { + const std::string& name = file->dependency(i)->name(); + printer->Print("import * as $alias$ from '$file$';\n" + "goog.object.extend(proto, $alias$);\n", + "alias", ModuleAlias(name), "file", + GetRootPath(file->name(), name) + GetJSFilename(options, name)); + } + } else { + for (int i = 0; i < file->dependency_count(); i++) { + const std::string& name = file->dependency(i)->name(); + printer->Print("var $alias$ = require('$file$');\n" + "goog.object.extend(proto, $alias$);\n", + "alias", ModuleAlias(name), "file", + GetRootPath(file->name(), name) + GetJSFilename(options, name)); + } } } @@ -3698,6 +3714,20 @@ void Generator::GenerateFile(const GeneratorOptions& options, } else if (options.import_style == GeneratorOptions::kImportCommonJsStrict) { printer->Print("goog.object.extend(exports, proto);\n", "package", GetNamespace(options, file)); + } else if (options.import_style == GeneratorOptions::kImportEs6) { + std::string package = GetNamespace(options, file); + for (std::set::iterator it = provided.begin(); + it != provided.end(); ++it) { + std::string fullname = *it; + std::string name = fullname.substr(package.length()); + + std::string::iterator iend = std::remove(name.begin(), name.end(), '.'); + name.resize(name.length()-(name.end()-iend)); + name.shrink_to_fit(); + + printer->Print("export const $name$ = $fullname$;\n", + "name", name, "fullname", fullname); + } } // Emit well-known type methods.