diff --git a/src/idl_gen_js.cpp b/src/idl_gen_js.cpp index 8245ddeb1..2986056f8 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.