@@ -224,7 +224,8 @@ std::string MaybeCrossFileRef(const GeneratorOptions& options,
224
224
const FileDescriptor* from_file,
225
225
const Descriptor* to_message) {
226
226
if ((options.import_style == GeneratorOptions::kImportCommonJs ||
227
- options.import_style == GeneratorOptions::kImportCommonJsStrict ) &&
227
+ options.import_style == GeneratorOptions::kImportCommonJsStrict ||
228
+ options.import_style == GeneratorOptions::kImportEs6 ) &&
228
229
from_file != to_message->file ()) {
229
230
// Cross-file ref in CommonJS needs to use the module alias instead of
230
231
// the global name.
@@ -3614,8 +3615,14 @@ void Generator::GenerateFile(const GeneratorOptions& options,
3614
3615
3615
3616
// Generate "require" statements.
3616
3617
if ((options.import_style == GeneratorOptions::kImportCommonJs ||
3617
- options.import_style == GeneratorOptions::kImportCommonJsStrict )) {
3618
- printer->Print (" var jspb = require('google-protobuf');\n " );
3618
+ options.import_style == GeneratorOptions::kImportCommonJsStrict ||
3619
+ options.import_style == GeneratorOptions::kImportEs6 )) {
3620
+
3621
+ if (options.import_style == GeneratorOptions::kImportEs6 ) {
3622
+ printer->Print (" import * as jspb from 'google-protobuf';\n " );
3623
+ } else {
3624
+ printer->Print (" var jspb = require('google-protobuf');\n " );
3625
+ }
3619
3626
printer->Print (" var goog = jspb;\n " );
3620
3627
3621
3628
// Do not use global scope in strict mode
@@ -3644,13 +3651,22 @@ void Generator::GenerateFile(const GeneratorOptions& options,
3644
3651
" Function('return this')();\n\n " );
3645
3652
}
3646
3653
3647
- for (int i = 0 ; i < file->dependency_count (); i++) {
3648
- const std::string& name = file->dependency (i)->name ();
3649
- printer->Print (
3650
- " var $alias$ = require('$file$');\n "
3651
- " goog.object.extend(proto, $alias$);\n " ,
3652
- " alias" , ModuleAlias (name), " file" ,
3653
- GetRootPath (file->name (), name) + GetJSFilename (options, name));
3654
+ if (options.import_style == GeneratorOptions::kImportEs6 ) {
3655
+ for (int i = 0 ; i < file->dependency_count (); i++) {
3656
+ const std::string& name = file->dependency (i)->name ();
3657
+ printer->Print (" import * as $alias$ from '$file$';\n "
3658
+ " goog.object.extend(proto, $alias$);\n " ,
3659
+ " alias" , ModuleAlias (name), " file" ,
3660
+ GetRootPath (file->name (), name) + GetJSFilename (options, name));
3661
+ }
3662
+ } else {
3663
+ for (int i = 0 ; i < file->dependency_count (); i++) {
3664
+ const std::string& name = file->dependency (i)->name ();
3665
+ printer->Print (" var $alias$ = require('$file$');\n "
3666
+ " goog.object.extend(proto, $alias$);\n " ,
3667
+ " alias" , ModuleAlias (name), " file" ,
3668
+ GetRootPath (file->name (), name) + GetJSFilename (options, name));
3669
+ }
3654
3670
}
3655
3671
}
3656
3672
@@ -3694,6 +3710,20 @@ void Generator::GenerateFile(const GeneratorOptions& options,
3694
3710
} else if (options.import_style == GeneratorOptions::kImportCommonJsStrict ) {
3695
3711
printer->Print (" goog.object.extend(exports, proto);\n " , " package" ,
3696
3712
GetNamespace (options, file));
3713
+ } else if (options.import_style == GeneratorOptions::kImportEs6 ) {
3714
+ std::string package = GetNamespace (options, file);
3715
+ for (std::set<std::string>::iterator it = provided.begin ();
3716
+ it != provided.end (); ++it) {
3717
+ std::string fullname = *it;
3718
+ std::string name = fullname.substr (package.length ());
3719
+
3720
+ std::string::iterator iend = std::remove (name.begin (), name.end (), ' .' );
3721
+ name.resize (name.length ()-(name.end ()-iend));
3722
+ name.shrink_to_fit ();
3723
+
3724
+ printer->Print (" export const $name$ = $fullname$;\n " ,
3725
+ " name" , name, " fullname" , fullname);
3726
+ }
3697
3727
}
3698
3728
3699
3729
// Emit well-known type methods.
0 commit comments