Skip to content

Commit

Permalink
Improve directory and path support
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldraper committed Jul 3, 2023
1 parent 88cd4bb commit d30245c
Show file tree
Hide file tree
Showing 33 changed files with 384 additions and 517 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Startup
startup --host_jvm_args=-DBAZEL_TRACK_SOURCE_DIRECTORIES=1

# Files
import %workspace%/tools/bazel/deleted.bazelrc

Expand Down
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.1.0
6.2.0
12 changes: 2 additions & 10 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,6 @@ files(

# NPM

load("//tools/npm:npm.bzl", "PACKAGES", "ROOTS")
load("//commonjs:workspace.bzl", "cjs_directory_npm_plugin")
load("//npm:workspace.bzl", "npm")
load("//typescript:workspace.bzl", "ts_directory_npm_plugin")
load("//tools/npm:workspace.bzl", "npm_deps")

plugins = [
cjs_directory_npm_plugin(),
ts_directory_npm_plugin(),
]

npm("npm", PACKAGES, ROOTS, plugins)
npm_deps()
183 changes: 88 additions & 95 deletions angular/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ load("//commonjs:providers.bzl", "CjsInfo", "create_cjs_info", "gen_manifest", "
load("//commonjs:rules.bzl", "cjs_root")
load("//javascript:providers.bzl", "JsInfo", "create_js_info")
load("//javascript:rules.bzl", "js_export")
load("//util:path.bzl", "output", "output_name")
load("//util:path.bzl", "link_file", "output", "output_name")
load("//nodejs:rules.bzl", "nodejs_binary")
load("//typescript:providers.bzl", "TsInfo", "create_ts_info", "declaration_path", "is_declaration", "is_directory", "is_json", "js_path", "map_path")
load("//typescript:providers.bzl", "TsInfo", "create_ts_info", "declaration_path", "is_declaration", "is_json", "js_path", "map_path")
load(":providers.bzl", "AngularCompilerInfo", "resource_path")

def _module(module):
Expand Down Expand Up @@ -74,7 +74,6 @@ def _angular_library(ctx):
name = ctx.attr.name
output_ = output(ctx.label, actions)
source_map = ctx.attr._source_map[BuildSettingInfo].value
src_prefix = ctx.attr.src_prefix
strip_prefix = ctx.attr.strip_prefix
ts_deps = compiler.ts_deps + [dep[TsInfo] for dep in ctx.attr.deps if TsInfo in dep]
tsconfig_js = ctx.attr.config_dep and ctx.attr.config_dep[JsInfo]
Expand All @@ -88,27 +87,21 @@ def _angular_library(ctx):
declarations = []
inputs = []
js = []
ts = []
outputs = []

# resource

for file in ctx.files.resources:
path = output_name(
file = file,
label = label,
prefix = js_prefix,
strip_prefix = strip_prefix,
)
if compilation_mode == "opt":
if file.path == "%s/%s" % (output_.path, path):
resource = file
else:
resource = actions.declare_file(path)
actions.symlink(
output = resource,
target_file = file,
)
inputs.append(resource)
inputs.append(
link_file(
actions = actions,
file = file,
label = label,
output = output_,
),
)
else:
js_path_ = output_name(
file = file,
Expand Down Expand Up @@ -143,11 +136,12 @@ def _angular_library(ctx):
args = actions.args()
if tsconfig_path:
args.add("--config", "%s/%s" % (tsconfig_dep.package.path, tsconfig_path))
args.add("--empty", "true")
args.add("--module", module)
args.add("--source-map", json.encode(source_map))
args.add("--out-dir", "%s/%s" % (output_.path, js_prefix) if js_prefix else output_.path)
args.add("--root-dir", "%s/%s" % (output_.path, src_prefix) if src_prefix else output_.path)
args.add("--root-dir", "%s/%s" % (output_.path, strip_prefix) if strip_prefix else output_.path)
args.add("--root-dirs", "%s/%s" % (output_.path, strip_prefix) if strip_prefix else output_.path)
args.add("--root-dirs", "%s/%s" % (output_.path, declaration_prefix) if declaration_prefix else output_.path)
args.add(transpile_tsconfig)
actions.run(
arguments = [args],
Expand All @@ -174,88 +168,83 @@ def _angular_library(ctx):

# transpile
for file in ctx.files.srcs:
path = output_name(
ts_ = link_file(
actions = actions,
file = file,
label = label,
output = output_,
)
ts.append(ts_)
inputs.append(ts_)

if is_declaration(file.path):
continue

js_path_ = output_name(
file = file,
label = label,
prefix = js_prefix,
strip_prefix = strip_prefix,
)
declaration_path_ = output_name(
file = file,
label = label,
prefix = src_prefix,
prefix = declaration_prefix,
strip_prefix = strip_prefix,
)
if file.path == "%s/%s" % (output_.path, path):
ts_ = file
if file.is_directory:
js_ = actions.declare_directory(js_path_)
js.append(js_)
declaration = actions.declare_directory(declaration_path_)
declarations.append(declaration)
outputs.append(declaration)
elif is_json(file.path):
if ts_.path == js_path_:
js_ = ts_
else:
js_ = actions.declare_file(js_path_)
outputs.append(js_)
js.append(js_)
declarations.append(js_)
else:
ts_ = actions.declare_file(path)
actions.symlink(
target_file = file,
output = ts_,
)
inputs.append(ts_)
js_ = actions.declare_file(js_path(js_path_, jsx = jsx))
js.append(js_)
if source_map:
map = actions.declare_file(map_path(js_path(js_path_, jsx = jsx)))
js.append(map)
declaration = actions.declare_file(declaration_path(declaration_path_))
declarations.append(declaration)
outputs.append(declaration)

if not is_declaration(path):
js_path_ = output_name(
file = file,
label = label,
prefix = js_prefix,
strip_prefix = strip_prefix,
)
declaration_path_ = output_name(
file = file,
label = label,
prefix = declaration_prefix,
strip_prefix = strip_prefix,
)
if file.is_directory:
js_ = actions.declare_directory(js_path_)
js.append(js_)
declaration = actions.declare_directory(declaration_path_)
declarations.append(declaration)
outputs.append(declaration)
elif is_json(path):
if path == js_path_:
js_ = ts_
else:
js_ = actions.declare_file(js_path_)
outputs.append(js_)
js.append(js_)
declarations.append(js_)
if compilation_mode == "opt":
outputs.append(js_)
if source_map:
outputs.append(map)
else:
js_ = actions.declare_file(js_path(js_path_, jsx = jsx))
js.append(js_)
js_outputs = [js_]
if source_map:
map = actions.declare_file(map_path(js_path(js_path_, jsx = jsx)))
js.append(map)
declaration = actions.declare_file(declaration_path(declaration_path_))
declarations.append(declaration)
outputs.append(declaration)

if compilation_mode == "opt":
outputs.append(js_)
if source_map:
outputs.append(map)
else:
js_outputs = [js_]
if source_map:
js_outputs.append(map)
args = actions.args()
args.add("--config", transpile_tsconfig)
args.add("--manifest", transpile_package_manifest)
args.add(ts_.path)
args.set_param_file_format("multiline")
args.use_param_file("@%s", use_always = True)
actions.run(
arguments = [args],
executable = compiler.js_compiler.files_to_run.executable,
execution_requirements = {
"requires-worker-protocol": "json",
"supports-workers": "1",
},
inputs = depset(
[ts_, transpile_package_manifest, transpile_tsconfig],
transitive = [js_info.transitive_files for js_info in compiler.js_deps] + [tsconfig_js.transitive_files] if tsconfig_js else [],
),
mnemonic = "TypeScriptTranspile",
outputs = js_outputs,
tools = [compiler.js_compiler.files_to_run],
)
js_outputs.append(map)
args = actions.args()
args.add("--config", transpile_tsconfig)
args.add("--manifest", transpile_package_manifest)
args.add(ts_.path)
args.set_param_file_format("multiline")
args.use_param_file("@%s", use_always = True)
actions.run(
arguments = [args],
executable = compiler.js_compiler.files_to_run.executable,
execution_requirements = {
"requires-worker-protocol": "json",
"supports-workers": "1",
},
inputs = depset(
[ts_, transpile_package_manifest, transpile_tsconfig],
transitive = [js_info.transitive_files for js_info in compiler.js_deps] + [tsconfig_js.transitive_files] if tsconfig_js else [],
),
mnemonic = "TypeScriptTranspile",
outputs = js_outputs,
tools = [compiler.js_compiler.files_to_run],
)

# compile declarations
if outputs:
Expand All @@ -272,7 +261,10 @@ def _angular_library(ctx):
else:
args.add("--declaration-dir", "%s/%s" % (output_.path, declaration_prefix) if declaration_prefix else output_.path)
args.add("--out-dir", "%s/%s" % (output_.path, js_prefix) if js_prefix else output_.path)
args.add("--root-dir", "%s/%s" % (output_.path, src_prefix) if src_prefix else output_.path)
args.add("--root-dir", "%s/%s" % (output_.path, strip_prefix) if strip_prefix else output_.path)
args.add("--root-dirs", "%s/%s" % (output_.path, strip_prefix) if strip_prefix else output_.path)
args.add("--root-dirs", "%s/%s" % (output_.path, declaration_prefix) if declaration_prefix else output_.path)
args.add_all(ts, before_each = "--file")
args.add("--module", module)
if compilation_mode == "opt":
args.add("--source-map", json.encode(source_map))
Expand All @@ -282,6 +274,7 @@ def _angular_library(ctx):
arguments = [args],
executable = config.files_to_run.executable,
tools = [config.files_to_run],
inputs = [file for file in ts if file.is_directory],
outputs = [tsconfig],
)

Expand Down
2 changes: 1 addition & 1 deletion angular/test/bazel/.bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.1.0
6.2.0
2 changes: 1 addition & 1 deletion commonjs/test/bazel/.bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.1.0
6.2.0
2 changes: 1 addition & 1 deletion javascript/test/bazel/.bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.1.0
6.2.0
2 changes: 1 addition & 1 deletion jest/test/bazel/.bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.1.0
6.2.0
2 changes: 1 addition & 1 deletion nodejs/test/bazel/.bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.1.0
6.2.0
19 changes: 10 additions & 9 deletions npm/doc/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,21 @@ TypeScript, CSS, etc.

To support these, the npm repositories can be customized via "plugins."

```bzl
npm("npm", npm_packages, npm_roots, npm_plugins)
```

The defaults are
The defaults are:

```bzl
load("@better_rules_javascript//commonjs:workspace.bzl", "cjs_npm_plugin")
load("@better_rules_javascript//js:workspace.bzl", "js_npm_plugin")

[
cjs_npm_plugin(),
js_npm_plugin(),
]
npm(
name = "npm",
packages = npm_packages,
roots = npm_roots,
plugins = [
cjs_npm_plugin(),
js_npm_plugin(),
]
)
```

If you use TypeScript, replace `js_npm_plugin()` with `ts_npm_plugin()`.
Expand Down
Loading

0 comments on commit d30245c

Please sign in to comment.