@@ -224,7 +224,8 @@ std::string MaybeCrossFileRef(const GeneratorOptions& options,
224224 const FileDescriptor* from_file,
225225 const Descriptor* to_message) {
226226 if ((options.import_style == GeneratorOptions::kImportCommonJs ||
227- options.import_style == GeneratorOptions::kImportCommonJsStrict ) &&
227+ options.import_style == GeneratorOptions::kImportCommonJsStrict ||
228+ options.import_style == GeneratorOptions::kImportEs6 ) &&
228229 from_file != to_message->file ()) {
229230 // Cross-file ref in CommonJS needs to use the module alias instead of
230231 // the global name.
@@ -3618,8 +3619,14 @@ void Generator::GenerateFile(const GeneratorOptions& options,
36183619
36193620 // Generate "require" statements.
36203621 if ((options.import_style == GeneratorOptions::kImportCommonJs ||
3621- options.import_style == GeneratorOptions::kImportCommonJsStrict )) {
3622- printer->Print (" var jspb = require('google-protobuf');\n " );
3622+ options.import_style == GeneratorOptions::kImportCommonJsStrict ||
3623+ options.import_style == GeneratorOptions::kImportEs6 )) {
3624+
3625+ if (options.import_style == GeneratorOptions::kImportEs6 ) {
3626+ printer->Print (" import * as jspb from 'google-protobuf';\n " );
3627+ } else {
3628+ printer->Print (" var jspb = require('google-protobuf');\n " );
3629+ }
36233630 printer->Print (" var goog = jspb;\n " );
36243631
36253632 // Do not use global scope in strict mode
@@ -3648,13 +3655,22 @@ void Generator::GenerateFile(const GeneratorOptions& options,
36483655 " Function('return this')();\n\n " );
36493656 }
36503657
3651- for (int i = 0 ; i < file->dependency_count (); i++) {
3652- const std::string& name = file->dependency (i)->name ();
3653- printer->Print (
3654- " var $alias$ = require('$file$');\n "
3655- " goog.object.extend(proto, $alias$);\n " ,
3656- " alias" , ModuleAlias (name), " file" ,
3657- GetRootPath (file->name (), name) + GetJSFilename (options, name));
3658+ if (options.import_style == GeneratorOptions::kImportEs6 ) {
3659+ for (int i = 0 ; i < file->dependency_count (); i++) {
3660+ const std::string& name = file->dependency (i)->name ();
3661+ printer->Print (" import * as $alias$ from '$file$';\n "
3662+ " goog.object.extend(proto, $alias$);\n " ,
3663+ " alias" , ModuleAlias (name), " file" ,
3664+ GetRootPath (file->name (), name) + GetJSFilename (options, name));
3665+ }
3666+ } else {
3667+ for (int i = 0 ; i < file->dependency_count (); i++) {
3668+ const std::string& name = file->dependency (i)->name ();
3669+ printer->Print (" var $alias$ = require('$file$');\n "
3670+ " goog.object.extend(proto, $alias$);\n " ,
3671+ " alias" , ModuleAlias (name), " file" ,
3672+ GetRootPath (file->name (), name) + GetJSFilename (options, name));
3673+ }
36583674 }
36593675 }
36603676
@@ -3698,6 +3714,20 @@ void Generator::GenerateFile(const GeneratorOptions& options,
36983714 } else if (options.import_style == GeneratorOptions::kImportCommonJsStrict ) {
36993715 printer->Print (" goog.object.extend(exports, proto);\n " , " package" ,
37003716 GetNamespace (options, file));
3717+ } else if (options.import_style == GeneratorOptions::kImportEs6 ) {
3718+ std::string package = GetNamespace (options, file);
3719+ for (std::set<std::string>::iterator it = provided.begin ();
3720+ it != provided.end (); ++it) {
3721+ std::string fullname = *it;
3722+ std::string name = fullname.substr (package.length ());
3723+
3724+ std::string::iterator iend = std::remove (name.begin (), name.end (), ' .' );
3725+ name.resize (name.length ()-(name.end ()-iend));
3726+ name.shrink_to_fit ();
3727+
3728+ printer->Print (" export const $name$ = $fullname$;\n " ,
3729+ " name" , name, " fullname" , fullname);
3730+ }
37013731 }
37023732
37033733 // Emit well-known type methods.
0 commit comments