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

fix: source map error with source-map-explorer #9659

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
30 changes: 25 additions & 5 deletions crates/swc/tests/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{
env::current_dir,
fs::create_dir_all,
path::{Path, PathBuf},
process::Command,
};

use anyhow::Context;
Expand Down Expand Up @@ -842,11 +843,30 @@ fn tests(input_dir: PathBuf, is_module: Option<IsModule>) {
serde_json::to_string_pretty(&json).unwrap()
});

NormalizedOutput::from(map.unwrap_or_default())
.compare_to_file(
output_dir.join(rel_path.with_extension("map").file_name().unwrap()),
)
.unwrap();
if let Some(map) = map {
let js_path = output_dir.join(rel_path);
let map_path =
output_dir.join(rel_path.with_extension("map").file_name().unwrap());

NormalizedOutput::from(map)
.compare_to_file(map_path.clone())
.unwrap();
let output = Command::new("node")
.arg("-e")
.arg(include_str!("source_map.js"))
.arg(js_path.clone())
.arg(map_path.clone())
.output()
.unwrap();

if !output.status.success() {
panic!(
"Validation failed: \n{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
}
}

if let Some(extra) = v.output {
let mut value: serde_json::Map<_, serde_json::Value> =
Expand Down
19 changes: 13 additions & 6 deletions crates/swc/tests/source_map.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
const validate = require("sourcemap-validator");
const fs = require("fs");
const { explore } = require("source-map-explorer");

const jsFile = process.argv[1];
const mapFile = process.argv[2];

const jsContent = fs.readFileSync(jsFile, "utf-8");
const mapContent = fs.readFileSync(mapFile, "utf-8");

validate(jsContent, mapContent);
explore([
{
code: jsFile,
map: mapFile
}
]).catch(({ errors }) => {
if (errors.length) {
const { code, message } = errors[0];
console.error(`${code} Error: ${message}`);
process.exit(1);
}
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"reflect-metadata": "^0.1.13",
"regenerator-runtime": "^0.13.9",
"source-map": "^0.7.3",
"source-map-explorer": "^2.5.3",
"source-map-support": "^0.5.19",
"sourcemap-validator": "^2.1.0",
"swc-plugin-coverage-instrument": "^0.0.24",
Expand Down
31 changes: 22 additions & 9 deletions packages/core/__tests__/transform/sourcemap_test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
const swc = require("../../"),
validate = require("sourcemap-validator"),
sourceMap = require("source-map");
const swc = require("../../");
const { explore } = require("source-map-explorer");
const path = require("path");

function validate(code, map) {
return explore([
{
code: Buffer.from(code),
map: Buffer.from(map),
}
])
.catch(({ errors }) => {
if (errors.length) {
const { code, message } = errors[0];
throw new Error(`${code} Error: ${message}`);
}
});
}

it("should handle sourcemap correctly", async () => {
const raw = `
class Foo extends Array {
Expand All @@ -15,7 +29,7 @@ console.log('foo')
});

expect(out.map).toBeTruthy();
validate(out.code, out.map, { "input.js": raw });
await validate(out.code, out.map);

// await sourceMap.SourceMapConsumer.with(JSON.parse(out.map), null, async (consumer) => {
// consumer.eachMapping((mapping) => {
Expand Down Expand Up @@ -45,7 +59,7 @@ console.log('foo')

expect(out1.map).toBeTruthy();
expect(JSON.parse(out1.map).sources).toEqual(["input.js"]);
validate(out1.code, out1.map, { "input.js": raw });
await validate(out1.code, out1.map);

const out2 = swc.transformSync(raw, {
sourceMaps: true,
Expand All @@ -55,7 +69,7 @@ console.log('foo')

expect(out2.map).toBeTruthy();
expect(JSON.parse(out2.map).sources).toEqual(["<anon>"]);
validate(out2.code, out2.map, { "input.js": raw });
await validate(out2.code, out2.map);
});

it("should handle input sourcemap correctly", async () => {
Expand All @@ -74,7 +88,7 @@ it("should handle input sourcemap correctly", async () => {
});

expect(out1.map).toBeTruthy();
validate(out1.code, out1.map, { "input.js": raw });
await validate(out1.code, out1.map);
console.log(out1.code);

const out2 = swc.transformSync(out1.code, {
Expand All @@ -92,8 +106,7 @@ it("should handle input sourcemap correctly", async () => {

console.log(out2.code);
expect(out2.map).toBeTruthy();
validate(out2.code, out2.map, { "input2.js": out1.code });
validate(out2.code, out2.map, { "input.js": raw });
await validate(out2.code, out2.map);

// await sourceMap.SourceMapConsumer.with(JSON.parse(out1.map), null, async (consumer1) => {
// await sourceMap.SourceMapConsumer.with(JSON.parse(out2.map), null, async (consumer2) => {
Expand Down
Loading
Loading