|
| 1 | +load("@aspect_rules_js//js:providers.bzl", "JsInfo", "js_info") |
| 2 | +load("@aspect_rules_ts//ts:defs.bzl", _ts_project = "ts_project") |
| 3 | +load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo", "JSEcmaScriptModuleInfo", "JSModuleInfo", "LinkablePackageInfo") |
| 4 | +load("@devinfra//bazel/ts_project:index.bzl", "strict_deps_test") |
| 5 | + |
| 6 | +def _ts_deps_interop_impl(ctx): |
| 7 | + types = [] |
| 8 | + sources = [] |
| 9 | + runfiles = ctx.runfiles(files = []) |
| 10 | + for dep in ctx.attr.deps: |
| 11 | + if not DeclarationInfo in dep: |
| 12 | + fail("Expected target with DeclarationInfo: %s", dep) |
| 13 | + types.append(dep[DeclarationInfo].transitive_declarations) |
| 14 | + if not JSModuleInfo in dep: |
| 15 | + fail("Expected target with JSModuleInfo: %s", dep) |
| 16 | + sources.append(dep[JSModuleInfo].sources) |
| 17 | + if not DefaultInfo in dep: |
| 18 | + fail("Expected target with DefaultInfo: %s", dep) |
| 19 | + runfiles = runfiles.merge(dep[DefaultInfo].default_runfiles) |
| 20 | + |
| 21 | + return [ |
| 22 | + DefaultInfo(runfiles = runfiles), |
| 23 | + ## NOTE: We don't need to propagate module mappings FORTUNATELY! |
| 24 | + # because rules_nodejs supports tsconfig path mapping, given that |
| 25 | + # everything is nicely compiled from `bazel-bin/`! |
| 26 | + js_info( |
| 27 | + target = ctx.label, |
| 28 | + transitive_types = depset(transitive = types), |
| 29 | + transitive_sources = depset(transitive = sources), |
| 30 | + ), |
| 31 | + ] |
| 32 | + |
| 33 | +ts_deps_interop = rule( |
| 34 | + implementation = _ts_deps_interop_impl, |
| 35 | + attrs = { |
| 36 | + "deps": attr.label_list(providers = [DeclarationInfo], mandatory = True), |
| 37 | + }, |
| 38 | +) |
| 39 | + |
| 40 | +def _ts_project_module_impl(ctx): |
| 41 | + # Forward runfiles. e.g. JSON files on `ts_project#data`. The jasmine |
| 42 | + # consuming rules may rely on this, or the linker due to its symlinks then. |
| 43 | + runfiles = ctx.attr.dep[DefaultInfo].default_runfiles |
| 44 | + info = ctx.attr.dep[JsInfo] |
| 45 | + |
| 46 | + # Filter runfiles to not include `node_modules` from Aspect as this interop |
| 47 | + # target is supposed to be used downstream by `rules_nodejs` consumers, |
| 48 | + # and mixing pnpm-style node modules with linker node modules is incompatible. |
| 49 | + filtered_runfiles = [] |
| 50 | + for f in runfiles.files.to_list(): |
| 51 | + if f.short_path.startswith("node_modules/"): |
| 52 | + continue |
| 53 | + filtered_runfiles.append(f) |
| 54 | + runfiles = ctx.runfiles(files = filtered_runfiles) |
| 55 | + |
| 56 | + providers = [ |
| 57 | + DefaultInfo( |
| 58 | + runfiles = runfiles, |
| 59 | + ), |
| 60 | + JSModuleInfo( |
| 61 | + direct_sources = info.sources, |
| 62 | + sources = depset(transitive = [info.transitive_sources]), |
| 63 | + ), |
| 64 | + JSEcmaScriptModuleInfo( |
| 65 | + direct_sources = info.sources, |
| 66 | + sources = depset(transitive = [info.transitive_sources]), |
| 67 | + ), |
| 68 | + DeclarationInfo( |
| 69 | + declarations = _filter_types_depset(info.types), |
| 70 | + transitive_declarations = _filter_types_depset(info.transitive_types), |
| 71 | + type_blocklisted_declarations = depset(), |
| 72 | + ), |
| 73 | + ] |
| 74 | + |
| 75 | + if ctx.attr.module_name: |
| 76 | + providers.append( |
| 77 | + LinkablePackageInfo( |
| 78 | + package_name = ctx.attr.module_name, |
| 79 | + package_path = "", |
| 80 | + path = "%s/%s/%s" % (ctx.bin_dir.path, ctx.label.workspace_root, ctx.label.package), |
| 81 | + files = info.sources, |
| 82 | + ), |
| 83 | + ) |
| 84 | + |
| 85 | + return providers |
| 86 | + |
| 87 | +ts_project_module = rule( |
| 88 | + implementation = _ts_project_module_impl, |
| 89 | + attrs = { |
| 90 | + "dep": attr.label(providers = [JsInfo], mandatory = True), |
| 91 | + # Noop attribute for aspect propagation of the linker interop deps; so |
| 92 | + # that transitive linker dependencies are discovered. |
| 93 | + "deps": attr.label_list(), |
| 94 | + # Note: The module aspect from consuming `ts_library` targets will |
| 95 | + # consume the module mappings automatically. |
| 96 | + "module_name": attr.string(), |
| 97 | + "module_root": attr.string(), |
| 98 | + }, |
| 99 | +) |
| 100 | + |
| 101 | +def ts_project( |
| 102 | + name, |
| 103 | + module_name = None, |
| 104 | + deps = [], |
| 105 | + interop_deps = [], |
| 106 | + tsconfig = None, |
| 107 | + testonly = False, |
| 108 | + visibility = None, |
| 109 | + ignore_strict_deps = False, |
| 110 | + enable_runtime_rnjs_interop = True, |
| 111 | + **kwargs): |
| 112 | + # Pull in the `rules_nodejs` variants of dependencies we know are "hybrid". This |
| 113 | + # is necessary as we can't mix `npm/node_modules` from RNJS with the pnpm-style |
| 114 | + # symlink-dependent node modules. In addition, we need to extract `_rjs` interop |
| 115 | + # dependencies so that we can forward and capture the module mappings for runtime |
| 116 | + # execution, with regards to first-party dependency linking. |
| 117 | + rjs_modules_to_rnjs = [] |
| 118 | + if enable_runtime_rnjs_interop: |
| 119 | + for d in deps: |
| 120 | + if d.startswith("//:node_modules/"): |
| 121 | + rjs_modules_to_rnjs.append(d.replace("//:node_modules/", "@npm//")) |
| 122 | + if d.endswith("_rjs"): |
| 123 | + rjs_modules_to_rnjs.append(d.replace("_rjs", "")) |
| 124 | + |
| 125 | + if tsconfig == None: |
| 126 | + tsconfig = "//src:test-tsconfig" if testonly else "//src:build-tsconfig" |
| 127 | + |
| 128 | + ts_deps_interop( |
| 129 | + name = "%s_interop_deps" % name, |
| 130 | + deps = [] + interop_deps + rjs_modules_to_rnjs, |
| 131 | + visibility = visibility, |
| 132 | + testonly = testonly, |
| 133 | + ) |
| 134 | + |
| 135 | + _ts_project( |
| 136 | + name = "%s_rjs" % name, |
| 137 | + testonly = testonly, |
| 138 | + declaration = True, |
| 139 | + tsconfig = tsconfig, |
| 140 | + visibility = visibility, |
| 141 | + # Use the worker from our own Angular rules, as the default worker |
| 142 | + # from `rules_ts` is incompatible with TS5+ and abandoned. We need |
| 143 | + # worker for efficient and fast DX. |
| 144 | + supports_workers = 1, |
| 145 | + tsc_worker = "@rules_angular//worker:worker_vanilla_ts", |
| 146 | + deps = [":%s_interop_deps" % name] + deps, |
| 147 | + **kwargs |
| 148 | + ) |
| 149 | + |
| 150 | + if not ignore_strict_deps: |
| 151 | + strict_deps_test( |
| 152 | + name = "%s_strict_deps_test" % name, |
| 153 | + srcs = kwargs.get("srcs", []), |
| 154 | + deps = deps, |
| 155 | + ) |
| 156 | + |
| 157 | + ts_project_module( |
| 158 | + name = name, |
| 159 | + testonly = testonly, |
| 160 | + visibility = visibility, |
| 161 | + dep = "%s_rjs" % name, |
| 162 | + # Forwarded dependencies for linker module mapping aspect. |
| 163 | + # RJS deps can also transitively pull in module mappings from their `interop_deps`. |
| 164 | + deps = [] + ["%s_interop_deps" % name] + deps, |
| 165 | + module_name = module_name, |
| 166 | + ) |
| 167 | + |
| 168 | +# Filter type provider to not include `.json` files. `ts_config` |
| 169 | +# targets are included in `ts_project` and their tsconfig json file |
| 170 | +# is included as type. See: |
| 171 | +# https://github.com/aspect-build/rules_ts/blob/main/ts/private/ts_config.bzl#L55C63-L55C68. |
| 172 | +def _filter_types_depset(types_depset): |
| 173 | + types = [] |
| 174 | + |
| 175 | + for t in types_depset.to_list(): |
| 176 | + if t.short_path.endswith(".json"): |
| 177 | + continue |
| 178 | + types.append(t) |
| 179 | + |
| 180 | + return depset(types) |
0 commit comments