From db0fcdd906422be064ca9bf543b95b6de0f61e6b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gabriel=20N=C3=BCtzi?= Date: Fri, 2 Nov 2018 16:54:43 +0100 Subject: [PATCH] --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) --- src/idl_gen_js.cpp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/idl_gen_js.cpp b/src/idl_gen_js.cpp index 8245dde..2986056 100644 --- a/src/idl_gen_js.cpp +++ b/src/idl_gen_js.cpp @@ -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(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. -- 2.7.4