Commit db0fcdd9 authored by Gabriel Nützi's avatar Gabriel Nützi Committed by Wouter van Oortmerssen

--keep-prefix for JS generator (#5018)

* keep include path

* add option --keep-prefix for js

* format contribution (format whole files before merge!)

* revert util.h : IsAbsPath ...

* JS Generator: only support relatives paths (keep it as it was)
parent 91fe9ba9
......@@ -128,8 +128,7 @@ class JsGenerator : public BaseGenerator {
const auto basename =
flatbuffers::StripPath(flatbuffers::StripExtension(file));
if (basename != file_name_) {
const auto file_name = basename + kGeneratedFileNamePostfix;
code += GenPrefixedImport(file, file_name);
code += GenPrefixedImport(file, basename);
}
}
}
......@@ -524,10 +523,26 @@ class JsGenerator : public BaseGenerator {
return "NS" + std::to_string(HashFnv1a<uint64_t>(file.c_str()));
}
static std::string GenPrefixedImport(const std::string &full_file_name,
const std::string &base_file_name) {
std::string GenPrefixedImport(const std::string &full_file_name,
const std::string &base_name) {
// Either keep the include path as it was
// or use only the base_name + kGeneratedFileNamePostfix
std::string path;
if (parser_.opts.keep_include_path) {
auto it = parser_.included_files_.find(full_file_name);
FLATBUFFERS_ASSERT(it != parser_.included_files_.end());
path =
flatbuffers::StripExtension(it->second) + kGeneratedFileNamePostfix;
} else {
path = base_name + kGeneratedFileNamePostfix;
}
// Add the include prefix and make the path always relative
path = flatbuffers::ConCatPathFileName(parser_.opts.include_prefix, path);
path = std::string(".") + kPathSeparator + path;
return "import * as " + GenFileNamespacePrefix(full_file_name) +
" from \"./" + base_file_name + "\";\n";
" from \"" + path + "\";\n";
}
// Adds a source-dependent prefix, for of import * statements.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment