diff --git a/examples/typeonly/BUILD.bazel b/examples/typeonly/BUILD.bazel new file mode 100644 index 0000000..a2f8268 --- /dev/null +++ b/examples/typeonly/BUILD.bazel @@ -0,0 +1,41 @@ +load("@aspect_rules_swc//swc:defs.bzl", "swc") +load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files") +load("@aspect_bazel_lib//lib:testing.bzl", "assert_json_matches") + +# Example test reproducing https://github.com/swc-project/swc/issues/7822 + +swc( + name = "compile", + srcs = [ + "a.ts", + "b.ts", + "c.d.ts", + "e.ts", + ], + out_dir = "out", + source_maps = True, + swcrc = "swcrc.json", +) + +# Assert that the output of "compile" rule matches the expected file. +write_source_files( + name = "test", + files = { + "expected/a.js": ":out/a.js", + "expected/b.js": ":out/b.js", + }, +) + +[ + assert_json_matches( + name = "test_%s" % f.replace("/", "-"), + file1 = ":out/%s.js.map" % f, + file2 = "expected/%s.js.map.golden" % f, + filter1 = ".sourceRoot,.sources", + filter2 = ".sourceRoot,.sources", + ) + for f in [ + "a", + "b", + ] +] diff --git a/examples/typeonly/README.md b/examples/typeonly/README.md new file mode 100644 index 0000000..119601d --- /dev/null +++ b/examples/typeonly/README.md @@ -0,0 +1,13 @@ +Can also be reproduced using the swc-binary. Not (yet) reproduced using @swc/cli + +``` +> ./swc-darwin-x64 compile --source-file-name c.d.ts --config-file swcrc.json --source-maps true --out-file c.js c.d.ts +thread 'main' panicked at 'index out of bounds: the len is 0 but the index is 0', /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sourcemap-6.2.3/src/types.rs:655:9 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace +``` + +``` +> ./swc-darwin-x64 compile --source-file-name b.ts --config-file swcrc.json --source-maps true --out-file b.js b.ts +thread 'main' panicked at 'index out of bounds: the len is 0 but the index is 0', /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sourcemap-6.2.3/src/types.rs:655:9 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace +``` diff --git a/examples/typeonly/a.ts b/examples/typeonly/a.ts new file mode 100644 index 0000000..c736d29 --- /dev/null +++ b/examples/typeonly/a.ts @@ -0,0 +1,7 @@ +import { Foo } from "./b" +import { Bar } from "./c" + +const a: Foo = 1 +const b: Bar = 2 + +console.log(a, b) \ No newline at end of file diff --git a/examples/typeonly/b.ts b/examples/typeonly/b.ts new file mode 100644 index 0000000..af648cf --- /dev/null +++ b/examples/typeonly/b.ts @@ -0,0 +1 @@ +export type Foo = number \ No newline at end of file diff --git a/examples/typeonly/c.d.ts b/examples/typeonly/c.d.ts new file mode 100644 index 0000000..f1b8e8b --- /dev/null +++ b/examples/typeonly/c.d.ts @@ -0,0 +1 @@ +export type Bar = number \ No newline at end of file diff --git a/examples/typeonly/e.ts b/examples/typeonly/e.ts new file mode 100644 index 0000000..e69de29 diff --git a/examples/typeonly/expected/a.js b/examples/typeonly/expected/a.js new file mode 100644 index 0000000..805014e --- /dev/null +++ b/examples/typeonly/expected/a.js @@ -0,0 +1,6 @@ +var a = 1; +var b = 2; +console.log(a, b); +export { }; + +//# sourceMappingURL=a.js.map \ No newline at end of file diff --git a/examples/typeonly/expected/a.js.map.golden b/examples/typeonly/expected/a.js.map.golden new file mode 100644 index 0000000..f446d58 --- /dev/null +++ b/examples/typeonly/expected/a.js.map.golden @@ -0,0 +1,5 @@ +{ + "sources": [ + "../a.ts" + ] +} \ No newline at end of file diff --git a/examples/typeonly/expected/b.js b/examples/typeonly/expected/b.js new file mode 100644 index 0000000..172c3bc --- /dev/null +++ b/examples/typeonly/expected/b.js @@ -0,0 +1,3 @@ +export { }; + +//# sourceMappingURL=b.js.map \ No newline at end of file diff --git a/examples/typeonly/expected/b.js.map.golden b/examples/typeonly/expected/b.js.map.golden new file mode 100644 index 0000000..84fb3d8 --- /dev/null +++ b/examples/typeonly/expected/b.js.map.golden @@ -0,0 +1,5 @@ +{ + "sources": [ + "../b.ts" + ] +} \ No newline at end of file diff --git a/examples/typeonly/swcrc.json b/examples/typeonly/swcrc.json new file mode 100755 index 0000000..ba93431 --- /dev/null +++ b/examples/typeonly/swcrc.json @@ -0,0 +1,30 @@ +{ + "sourceMaps": true, + "module": { + "type": "commonjs", + "strictMode": true, + "noInterop": true + }, + "jsc": { + "externalHelpers": true, + "target": "es2015", + "parser": { + "syntax": "typescript", + "tsx": true, + "decorators": true, + "dynamicImport": true + }, + "transform": { + "legacyDecorator": true, + "decoratorMetadata": true, + "react": { + "throwIfNamespace": false, + "useBuiltins": false, + "pragma": "React.createElement", + "pragmaFrag": "React.Fragment", + "importSource": "react" + } + }, + "keepClassNames": false + } +}