Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added minimal support for es6 import_style #150

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions generator/js_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -3614,8 +3615,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
Expand Down Expand Up @@ -3644,13 +3651,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));
}
}
}

Expand Down Expand Up @@ -3694,6 +3710,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<std::string>::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.
Expand Down