Skip to content

Commit

Permalink
fix: don't relativize paths to the swcrc location
Browse files Browse the repository at this point in the history
maybe something changed upstream since that code was written?
The newly added example shows it working without that.

Fixes #11
  • Loading branch information
alexeagle authored and gregmagolan committed Dec 21, 2021
1 parent 9e609b9 commit f539478
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 11 deletions.
23 changes: 23 additions & 0 deletions examples/rc/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"decorators": true,
"dynamicImport": false
},
"transform": {
"legacyDecorator": true,
"decoratorMetadata": true
},
"target": "es2016"
},
"module": {
"type": "commonjs",
"strict": false,
"strictMode": true,
"lazy": false,
"noInterop": false
},
"sourceMaps": true
}
1 change: 1 addition & 0 deletions examples/rc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports_files([".swcrc"])
24 changes: 24 additions & 0 deletions examples/rc/src/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@aspect_rules_swc//swc:swc.bzl", "swc")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")

# Runs `swc in.ts > ../../bazel-bin/examples/simple/in.js`
# You can run `bazel build --subcommands //examples/simple:transpile`
# to see the exact command line Bazel runs.
# Note that by default, sources are found by glob(["**/*.ts"])
swc(
name = "transpile",
source_maps = True,
swcrc = "//examples/rc:.swcrc",
)

diff_test(
name = "test",
file1 = "in.js",
file2 = "expected.js",
)

diff_test(
name = "test_map",
file1 = "in.js.map",
file2 = "expected.js.map",
)
10 changes: 10 additions & 0 deletions examples/rc/src/expected.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/rc/src/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/rc/src/in.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a: string = 'foo'
18 changes: 7 additions & 11 deletions swc/private/swc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,32 @@ def _impl(ctx):
outputs.extend(ctx.outputs.map_outs)
for src in ctx.files.srcs:
js_out = ctx.actions.declare_file(paths.replace_extension(src.basename, ".js"), sibling = src)
inputs = [src] + ctx.toolchains["@aspect_rules_swc//swc:toolchain_type"].swcinfo.tool_files
outs = [js_out]
if source_maps:
outs.append(ctx.actions.declare_file(paths.replace_extension(src.basename, ".js.map"), sibling = src))

# Pass in the swcrc config if it is set; if it is
# then source paths are expected to be relative to the swcrc directory
src_path = src.path
# Pass in the swcrc config if it is set
if ctx.file.swcrc:
swcrc_path = ctx.file.swcrc.path
swcrc_directory = paths.dirname(swcrc_path)
args.add_all([
"--config-file",
swcrc_path,
])

# TODO(greg): determine if this is needed, given that relativize below should provide a correct path
# if not src_path.startswith(swcrc_directory):
# fail("sources must be in swcrc directory or subdirectory if swcrc is specified")
src_path = paths.relativize(src_path, swcrc_directory)
inputs.append(ctx.file.swcrc)
else:
args.add("--no-swcrc")

args.add_all([
src_path,
src.path,
"--out-file",
js_out.path,
"--no-swcrc",
"-q",
])

ctx.actions.run(
inputs = [src] + ctx.toolchains["@aspect_rules_swc//swc:toolchain_type"].swcinfo.tool_files,
inputs = inputs,
arguments = [args],
outputs = outs,
env = {
Expand Down

0 comments on commit f539478

Please sign in to comment.