Skip to content

Commit 3d13d3f

Browse files
committed
added minimal support for es6 import_style
1 parent 566f359 commit 3d13d3f

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

generator/js_generator.cc

+40-10
Original file line numberDiff line numberDiff line change
@@ -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.
@@ -3614,8 +3615,14 @@ void Generator::GenerateFile(const GeneratorOptions& options,
36143615

36153616
// Generate "require" statements.
36163617
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+
}
36193626
printer->Print("var goog = jspb;\n");
36203627

36213628
// Do not use global scope in strict mode
@@ -3644,13 +3651,22 @@ void Generator::GenerateFile(const GeneratorOptions& options,
36443651
" Function('return this')();\n\n");
36453652
}
36463653

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+
}
36543670
}
36553671
}
36563672

@@ -3694,6 +3710,20 @@ void Generator::GenerateFile(const GeneratorOptions& options,
36943710
} else if (options.import_style == GeneratorOptions::kImportCommonJsStrict) {
36953711
printer->Print("goog.object.extend(exports, proto);\n", "package",
36963712
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+
}
36973727
}
36983728

36993729
// Emit well-known type methods.

0 commit comments

Comments
 (0)