When a ts_library has a module_name, use this name in place of
workspace_name/path/to/package in UMD outputs.
It will still have deep imports, such as 'my-package/index'

This allows eg. ts-api-guardian to publish the UMD outputs of ts_library.

PiperOrigin-RevId: 187549298
diff --git a/examples/es5_output/es5_output_test.sh b/examples/es5_output/es5_output_test.sh
index 22fdb23..ee928f5 100755
--- a/examples/es5_output/es5_output_test.sh
+++ b/examples/es5_output/es5_output_test.sh
@@ -3,7 +3,7 @@
 
 # should produce named UMD modules
 readonly LIBRARY_JS=$(cat $TEST_SRCDIR/build_bazel_rules_typescript/examples/some_library/library.js)
-if [[ "$LIBRARY_JS" != *"define(\"build_bazel_rules_typescript/examples/some_library/library\""* ]]; then
+if [[ "$LIBRARY_JS" != *"define(\"some-lib/library\""* ]]; then
   echo "Expected library.js to declare named module, but was"
   echo "$A_JS"
   exit 1
@@ -19,7 +19,7 @@
 
 # should give a name to required modules from other compilation unit
 readonly FOO_JS=$(cat $TEST_SRCDIR/build_bazel_rules_typescript/examples/bar.js)
-if [[ "$FOO_JS" != *"require(\"build_bazel_rules_typescript/examples/some_library/library\")"* ]]; then
+if [[ "$FOO_JS" != *"require(\"some-lib/library\")"* ]]; then
   echo "Expected bar.js to require named module library, but was"
   echo "$FOO_JS"
   exit 1
diff --git a/internal/common/tsconfig.bzl b/internal/common/tsconfig.bzl
index 56778da..7346dcd 100644
--- a/internal/common/tsconfig.bzl
+++ b/internal/common/tsconfig.bzl
@@ -98,6 +98,7 @@
   bazel_options = {
       "workspaceName": ctx.workspace_name,
       "target": str(ctx.label),
+      "package": ctx.label.package,
       "tsickle": tsickle_externs != None,
       "tsickleGenerateExterns": getattr(ctx.attr, "generate_externs", True),
       "tsickleExternsPath": tsickle_externs.path if tsickle_externs else "",
@@ -118,6 +119,9 @@
   else:
     bazel_options["allowedStrictDeps"] = [f.path for f in allowed_deps]
 
+  if hasattr(ctx.attr, "module_name") and ctx.attr.module_name:
+    bazel_options["moduleName"] = ctx.attr.module_name
+
   if "TYPESCRIPT_WORKER_CACHE_SIZE_MB" in ctx.var:
     max_cache_size_mb = int(ctx.var["TYPESCRIPT_WORKER_CACHE_SIZE_MB"])
     if max_cache_size_mb < 0:
diff --git a/internal/tsc_wrapped/compiler_host.ts b/internal/tsc_wrapped/compiler_host.ts
index d52f91d..7309786 100644
--- a/internal/tsc_wrapped/compiler_host.ts
+++ b/internal/tsc_wrapped/compiler_host.ts
@@ -251,8 +251,13 @@
   amdModuleName(sf: ts.SourceFile): string|undefined {
     if (!this.shouldNameModule(sf.fileName)) return undefined;
     // /build/work/bazel-out/local-fastbuild/bin/path/to/file.ts
-    // -> path/to/file.ts
-    let fileName = this.rootDirsRelative(sf.fileName);
+    // -> path/to/file
+    let fileName = this.rootDirsRelative(sf.fileName).replace(/(\.d)?\.tsx?$/, '');
+
+    if (this.bazelOpts.moduleName) {
+      return path.join(this.bazelOpts.moduleName, path.relative(this.bazelOpts.package, fileName));
+    }
+
     let workspace = this.bazelOpts.workspaceName;
 
     // Workaround https://github.com/bazelbuild/bazel/issues/1262
@@ -270,9 +275,9 @@
       fileName = parts.slice(2).join('/');
     }
 
-    // path/to/file.ts ->
+    // path/to/file ->
     // myWorkspace/path/to/file
-    return path.join(workspace, fileName.replace(/(\.d)?\.tsx?$/, ''));
+    return path.join(workspace, fileName);
   }
 
   /** Loads a source file from disk (or the cache). */
diff --git a/internal/tsc_wrapped/tsconfig.ts b/internal/tsc_wrapped/tsconfig.ts
index 88140cb..568c848 100644
--- a/internal/tsc_wrapped/tsconfig.ts
+++ b/internal/tsc_wrapped/tsconfig.ts
@@ -30,6 +30,9 @@
   /** The full bazel target that is being built, e.g. //my/pkg:library. */
   target: string;
 
+  /** The bazel package, eg my/pkg */
+  package: string;
+
   /** If true, convert require()s into goog.module(). */
   googmodule: boolean;
 
@@ -115,6 +118,12 @@
    * Suppress warnings about tsconfig.json properties that are overridden.
    */
   suppressTsconfigOverrideWarnings: boolean;
+
+  /**
+   * An explicit name for this module, given by the module_name attribute on a
+   * ts_library.
+   */
+  moduleName?: string;
 }
 
 export interface ParsedTsConfig {