diff --git a/crates/swc/tests/projects.rs b/crates/swc/tests/projects.rs index 7a3b568c854d..28ecb151a0b4 100644 --- a/crates/swc/tests/projects.rs +++ b/crates/swc/tests/projects.rs @@ -2,6 +2,7 @@ use std::{ env::current_dir, fs::create_dir_all, path::{Path, PathBuf}, + process::Command, }; use anyhow::Context; @@ -842,11 +843,30 @@ fn tests(input_dir: PathBuf, is_module: Option) { 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> = diff --git a/crates/swc/tests/source_map.js b/crates/swc/tests/source_map.js index 4bb26310d5e0..02a91a26d49e 100644 --- a/crates/swc/tests/source_map.js +++ b/crates/swc/tests/source_map.js @@ -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); + } +}); diff --git a/package.json b/package.json index 5457d2f73bd9..64ffce2babdc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/core/__tests__/transform/sourcemap_test.js b/packages/core/__tests__/transform/sourcemap_test.js index aaac0be2cb19..ab0f19554769 100644 --- a/packages/core/__tests__/transform/sourcemap_test.js +++ b/packages/core/__tests__/transform/sourcemap_test.js @@ -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 { @@ -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) => { @@ -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, @@ -55,7 +69,7 @@ console.log('foo') expect(out2.map).toBeTruthy(); expect(JSON.parse(out2.map).sources).toEqual([""]); - validate(out2.code, out2.map, { "input.js": raw }); + await validate(out2.code, out2.map); }); it("should handle input sourcemap correctly", async () => { @@ -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, { @@ -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) => { diff --git a/yarn.lock b/yarn.lock index 8fb068914a76..a5f7a8281128 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4792,6 +4792,7 @@ __metadata: reflect-metadata: "npm:^0.1.13" regenerator-runtime: "npm:^0.13.9" source-map: "npm:^0.7.3" + source-map-explorer: "npm:^2.5.3" source-map-support: "npm:^0.5.19" sourcemap-validator: "npm:^2.1.0" swc-plugin-coverage-instrument: "npm:^0.0.24" @@ -6025,6 +6026,13 @@ __metadata: languageName: node linkType: hard +"async@npm:^3.2.3": + version: 3.2.6 + resolution: "async@npm:3.2.6" + checksum: cb6e0561a3c01c4b56a799cc8bab6ea5fef45f069ab32500b6e19508db270ef2dffa55e5aed5865c5526e9907b1f8be61b27530823b411ffafb5e1538c86c368 + languageName: node + linkType: hard + "asynckit@npm:^0.4.0": version: 0.4.0 resolution: "asynckit@npm:0.4.0" @@ -6667,6 +6675,15 @@ __metadata: languageName: node linkType: hard +"btoa@npm:^1.2.1": + version: 1.2.1 + resolution: "btoa@npm:1.2.1" + bin: + btoa: bin/btoa.js + checksum: 29f2ca93837e10427184626bdfd5d00065dff28b604b822aa9849297dac8c8d6ad385cc96eed812ebf153d80c24a4556252afdbb97c7a712938baeaad7547705 + languageName: node + linkType: hard + "buffer-crc32@npm:~0.2.3": version: 0.2.13 resolution: "buffer-crc32@npm:0.2.13" @@ -6932,7 +6949,7 @@ __metadata: languageName: node linkType: hard -"chalk@npm:^4.0.0, chalk@npm:^4.1.0, chalk@npm:^4.1.2": +"chalk@npm:^4.0.0, chalk@npm:^4.0.2, chalk@npm:^4.1.0, chalk@npm:^4.1.2": version: 4.1.2 resolution: "chalk@npm:4.1.2" dependencies: @@ -8261,7 +8278,7 @@ __metadata: languageName: node linkType: hard -"duplexer@npm:~0.1.1": +"duplexer@npm:^0.1.2, duplexer@npm:~0.1.1": version: 0.1.2 resolution: "duplexer@npm:0.1.2" checksum: 62ba61a830c56801db28ff6305c7d289b6dc9f859054e8c982abd8ee0b0a14d2e9a8e7d086ffee12e868d43e2bbe8a964be55ddbd8c8957714c87373c7a4f9b0 @@ -8327,6 +8344,17 @@ __metadata: languageName: node linkType: hard +"ejs@npm:^3.1.5": + version: 3.1.10 + resolution: "ejs@npm:3.1.10" + dependencies: + jake: "npm:^10.8.5" + bin: + ejs: bin/cli.js + checksum: a9cb7d7cd13b7b1cd0be5c4788e44dd10d92f7285d2f65b942f33e127230c054f99a42db4d99f766d8dbc6c57e94799593ee66a14efd7c8dd70c4812bf6aa384 + languageName: node + linkType: hard + "electron-to-chromium@npm:^1.4.535": version: 1.4.569 resolution: "electron-to-chromium@npm:1.4.569" @@ -8665,7 +8693,7 @@ __metadata: languageName: node linkType: hard -"escape-html@npm:~1.0.3": +"escape-html@npm:^1.0.3, escape-html@npm:~1.0.3": version: 1.0.3 resolution: "escape-html@npm:1.0.3" checksum: 6213ca9ae00d0ab8bccb6d8d4e0a98e76237b2410302cf7df70aaa6591d509a2a37ce8998008cbecae8fc8ffaadf3fb0229535e6a145f3ce0b211d060decbb24 @@ -9358,6 +9386,15 @@ __metadata: languageName: node linkType: hard +"filelist@npm:^1.0.4": + version: 1.0.4 + resolution: "filelist@npm:1.0.4" + dependencies: + minimatch: "npm:^5.0.1" + checksum: 4b436fa944b1508b95cffdfc8176ae6947b92825483639ef1b9a89b27d82f3f8aa22b21eed471993f92709b431670d4e015b39c087d435a61e1bb04564cf51de + languageName: node + linkType: hard + "fill-range@npm:^4.0.0": version: 4.0.0 resolution: "fill-range@npm:4.0.0" @@ -9911,7 +9948,7 @@ __metadata: languageName: node linkType: hard -"glob@npm:^7.0.3, glob@npm:^7.0.5, glob@npm:^7.1.1, glob@npm:^7.1.2, glob@npm:^7.1.3, glob@npm:^7.1.4": +"glob@npm:^7.0.3, glob@npm:^7.0.5, glob@npm:^7.1.1, glob@npm:^7.1.2, glob@npm:^7.1.3, glob@npm:^7.1.4, glob@npm:^7.1.6": version: 7.2.3 resolution: "glob@npm:7.2.3" dependencies: @@ -10093,6 +10130,15 @@ __metadata: languageName: node linkType: hard +"gzip-size@npm:^6.0.0": + version: 6.0.0 + resolution: "gzip-size@npm:6.0.0" + dependencies: + duplexer: "npm:^0.1.2" + checksum: 2df97f359696ad154fc171dcb55bc883fe6e833bca7a65e457b9358f3cb6312405ed70a8da24a77c1baac0639906cd52358dc0ce2ec1a937eaa631b934c94194 + languageName: node + linkType: hard + "handle-thing@npm:^2.0.0": version: 2.0.1 resolution: "handle-thing@npm:2.0.1" @@ -11315,6 +11361,20 @@ __metadata: languageName: node linkType: hard +"jake@npm:^10.8.5": + version: 10.9.2 + resolution: "jake@npm:10.9.2" + dependencies: + async: "npm:^3.2.3" + chalk: "npm:^4.0.2" + filelist: "npm:^1.0.4" + minimatch: "npm:^3.1.2" + bin: + jake: bin/cli.js + checksum: 3be324708f99f031e0aec49ef8fd872eb4583cbe8a29a0c875f554f6ac638ee4ea5aa759bb63723fd54f77ca6d7db851eaa78353301734ed3700db9cb109a0cd + languageName: node + linkType: hard + "jest-changed-files@npm:^25.5.0": version: 25.5.0 resolution: "jest-changed-files@npm:25.5.0" @@ -12814,7 +12874,7 @@ __metadata: languageName: node linkType: hard -"lodash@npm:^4.17.11, lodash@npm:^4.17.14, lodash@npm:^4.17.19, lodash@npm:^4.17.21, lodash@npm:^4.17.4": +"lodash@npm:^4.17.11, lodash@npm:^4.17.14, lodash@npm:^4.17.19, lodash@npm:^4.17.20, lodash@npm:^4.17.21, lodash@npm:^4.17.4": version: 4.17.21 resolution: "lodash@npm:4.17.21" checksum: c08619c038846ea6ac754abd6dd29d2568aa705feb69339e836dfa8d8b09abbb2f859371e86863eda41848221f9af43714491467b5b0299122431e202bb0c532 @@ -13934,6 +13994,16 @@ __metadata: languageName: node linkType: hard +"open@npm:^7.3.1": + version: 7.4.2 + resolution: "open@npm:7.4.2" + dependencies: + is-docker: "npm:^2.0.0" + is-wsl: "npm:^2.1.1" + checksum: 4fc02ed3368dcd5d7247ad3566433ea2695b0713b041ebc0eeb2f0f9e5d4e29fc2068f5cdd500976b3464e77fe8b61662b1b059c73233ccc601fe8b16d6c1cd6 + languageName: node + linkType: hard + "opn@npm:^5.5.0": version: 5.5.0 resolution: "opn@npm:5.5.0" @@ -15338,6 +15408,17 @@ __metadata: languageName: node linkType: hard +"rimraf@npm:~2.6.2": + version: 2.6.3 + resolution: "rimraf@npm:2.6.3" + dependencies: + glob: "npm:^7.1.3" + bin: + rimraf: ./bin.js + checksum: 756419f2fa99aa119c46a9fc03e09d84ecf5421a80a72d1944c5088c9e4671e77128527a900a313ed9d3fdbdd37e2ae05486cd7e9116d5812d8c31f2399d7c86 + languageName: node + linkType: hard + "ripemd160@npm:^2.0.0, ripemd160@npm:^2.0.1": version: 2.0.2 resolution: "ripemd160@npm:2.0.2" @@ -15914,6 +15995,29 @@ __metadata: languageName: node linkType: hard +"source-map-explorer@npm:^2.5.3": + version: 2.5.3 + resolution: "source-map-explorer@npm:2.5.3" + dependencies: + btoa: "npm:^1.2.1" + chalk: "npm:^4.1.0" + convert-source-map: "npm:^1.7.0" + ejs: "npm:^3.1.5" + escape-html: "npm:^1.0.3" + glob: "npm:^7.1.6" + gzip-size: "npm:^6.0.0" + lodash: "npm:^4.17.20" + open: "npm:^7.3.1" + source-map: "npm:^0.7.4" + temp: "npm:^0.9.4" + yargs: "npm:^16.2.0" + bin: + sme: bin/cli.js + source-map-explorer: bin/cli.js + checksum: 39487c6db499d9e55d724c761d572be6d0e7953eedde848649cabdc200b5195efe3d41ff09916339022791f50d0ec577c9268beb9be10f6f131827a00c22b0c7 + languageName: node + linkType: hard + "source-map-resolve@npm:^0.5.0": version: 0.5.3 resolution: "source-map-resolve@npm:0.5.3" @@ -15968,7 +16072,7 @@ __metadata: languageName: node linkType: hard -"source-map@npm:^0.7.3": +"source-map@npm:^0.7.3, source-map@npm:^0.7.4": version: 0.7.4 resolution: "source-map@npm:0.7.4" checksum: a0f7c9b797eda93139842fd28648e868a9a03ea0ad0d9fa6602a0c1f17b7fb6a7dcca00c144476cccaeaae5042e99a285723b1a201e844ad67221bf5d428f1dc @@ -16533,6 +16637,16 @@ __metadata: languageName: node linkType: hard +"temp@npm:^0.9.4": + version: 0.9.4 + resolution: "temp@npm:0.9.4" + dependencies: + mkdirp: "npm:^0.5.1" + rimraf: "npm:~2.6.2" + checksum: 38d40564656c6e8e3caee41c592b3cc076d257ab4746ae4a6a179c44eb4a6d3e8a19a08c7716c8e88756bb898d6e56dd0a9e0408249bbcb3c990a178c34d0571 + languageName: node + linkType: hard + "terminal-link@npm:^2.0.0": version: 2.1.1 resolution: "terminal-link@npm:2.1.1" @@ -18099,7 +18213,7 @@ __metadata: languageName: node linkType: hard -"yargs@npm:16.2.0, yargs@npm:^16.1.0": +"yargs@npm:16.2.0, yargs@npm:^16.1.0, yargs@npm:^16.2.0": version: 16.2.0 resolution: "yargs@npm:16.2.0" dependencies: