Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions maldoca/js/babel/babel.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ struct BabelGenerateResult {
// TODO: Determine if source_code and error can co-exist, or they should be in
// a std::variant.
std::optional<BabelError> error;

// The generated source map, if requested.
std::optional<std::string> source_map;
};

class Babel {
Expand Down
3 changes: 3 additions & 0 deletions maldoca/js/babel/babel.proto
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ message BabelGenerateOptions {

// Whether to minify the generated source.
optional bool compact = 2 [default = false];

// Whether to generate source maps.
optional bool source_maps = 3 [default = false];
}

message PositionPb {
Expand Down
1 change: 1 addition & 0 deletions maldoca/js/babel/babel_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ nlohmann::json BabelGenerateOptionsToJson(const BabelGenerateOptions &options,
{"comments", options.include_comments()},
{"compact", options.compact()},
{"base64DecodeStringLiterals", string_literals_base64_encoded},
{"sourceMaps", options.source_maps()},
};

return json;
Expand Down
5 changes: 5 additions & 0 deletions maldoca/js/babel/babel_internal.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ message BabelGenerateRequest {

// Whether to minify the generated source.
optional bool compact = 3 [default = false];

// Whether to generate source maps.
optional bool source_maps = 4 [default = false];
}

message BabelResponse {
Expand All @@ -62,4 +65,6 @@ message BabelParseResponse {
message BabelGenerateResponse {
// Babel throws exceptions if the provided AST is invalid.
optional BabelError error = 1;

optional string source_map = 2;
}
23 changes: 23 additions & 0 deletions maldoca/js/babel/babel_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,29 @@ TEST_P(BabelTest, GenerateCompact) {
}
}

TEST_P(BabelTest, GenerateSourceMap) {
static const char kSourceMap[] =
R"({"version":3,"names":["console","log"],"sources":["source.js"],"sourcesContent":[null],"mappings":"AAAAA,OAAO,CAACC,GAAG,CAAC,eAAe,CAAC","ignoreList":[]})";

std::unique_ptr<Babel> babel = GetParam().babel_factory();
BabelParseRequest request;
MALDOCA_ASSERT_OK_AND_ASSIGN(
BabelParseResult parse_result,
babel->Parse(kSource, request, absl::InfiniteDuration()));

BabelGenerateOptions options;
options.set_source_maps(true);

MALDOCA_ASSERT_OK_AND_ASSIGN(BabelGenerateResult generate_result,
babel->Generate(parse_result.ast_string, options,
absl::InfiniteDuration()));

EXPECT_EQ(generate_result.source_code, kSource);
EXPECT_EQ(generate_result.error, std::nullopt);
EXPECT_TRUE(generate_result.source_map.has_value());
EXPECT_THAT(*generate_result.source_map, StrEq(kSourceMap));
}

GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BabelTest);

} // namespace maldoca
16 changes: 12 additions & 4 deletions maldoca/js/quickjs_babel/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,12 @@ function generateInternal(ast, options) {

convertCommentUidsToComments(ast);

const {code} = Babel.packages.generator.default(ast, options);
return code;
if (options.sourceMaps) {
options.sourceFileName = 'source.js';
}

const {code, map} = Babel.packages.generator.default(ast, options);
return {code, map};
}
exports.generateInternal = generateInternal;

Expand Down Expand Up @@ -523,8 +527,12 @@ exports.generate = function(astString, optionsSerialized) {
options = JSON.parse(optionsSerialized);
}

const source = generateInternal(ast, options);
return {source, response: '{}'};
const {code, map} = generateInternal(ast, options);
const response = {};
if (map) {
response.sourceMap = JSON.stringify(map);
}
return {source: code, response: JSON.stringify(response)};

} catch (error) {
const response = {};
Expand Down
6 changes: 6 additions & 0 deletions maldoca/js/quickjs_babel/quickjs_babel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,15 @@ absl::StatusOr<BabelGenerateResult> QuickJsBabel::Generate(
error = response.error();
}

std::optional<std::string> source_map;
if (response.has_source_map()) {
source_map = response.source_map();
}

return BabelGenerateResult{
.source_code = std::move(*source_code),
.error = error,
.source_map = source_map,
};
}

Expand Down
Loading