-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
language-support/ts generate package in commonjs format (#4380)
* Build commonjs format for npm packaging CHANGELOG_BEGIN CHANGELOG_END * Custom commonjs typescript library rule The npm packages generated by rules_nodejs' native ts_library rule use the UMD package format. This breaks webpack which attempts to determine dependencies by static code analysis and fails on UMD. To avoid this we call `tsc` directly to ensure generation of commonjs modules. * Enable module mapping on ts_commonjs_library * Replace ts_library by da_ts_library * Add dummy typescript/index.bzl on Windows Co-authored-by: Andreas Herrmann <andreash87@gmx.ch>
- Loading branch information
1 parent
ffa8180
commit 2dbd51b
Showing
5 changed files
with
92 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Copyright (c) 2020 The DAML Authors. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
load("@language_support_ts_deps//typescript:index.bzl", "tsc") | ||
|
||
def _da_ts_library_impl(ctx): | ||
return [ | ||
DefaultInfo( | ||
files = depset(ctx.files.srcs), | ||
runfiles = ctx.runfiles(files = ctx.files.srcs), | ||
), | ||
] | ||
|
||
_da_ts_library_rule = rule( | ||
_da_ts_library_impl, | ||
attrs = { | ||
"srcs": attr.label_list(allow_files = True), | ||
"deps": attr.label_list(allow_files = True), | ||
"module_name": attr.string(), | ||
"module_root": attr.string(), | ||
}, | ||
) | ||
|
||
def da_ts_library( | ||
name, | ||
tsconfig = "tsconfig.json", | ||
srcs = [], | ||
deps = [], | ||
module_name = "", | ||
module_root = "", | ||
**kwargs): | ||
"""Build a typescript library. | ||
Invokes tsc and generates definitions and commonjs files. | ||
Attrs: | ||
name: A unique name for the rule. | ||
tsconfig: The tsconfig.json file. | ||
The "files" attribute defines the typescript sources. | ||
srcs: The typescript source files. | ||
Defines which files are visible to the typescript compiler. | ||
deps: Typescript library dependencies. | ||
module_name: The import name of this library. E.g. @daml/types. | ||
module_root: Treat sources as rooted under module_name. | ||
""" | ||
outs = [ | ||
s.replace(".ts", ext) | ||
for ext in [".js", ".d.ts"] | ||
for s in srcs | ||
] | ||
tsc( | ||
name = "_%s_tsc" % name, | ||
data = [tsconfig] + srcs + deps, | ||
outs = outs, | ||
args = [ | ||
"--outDir", | ||
"$(RULEDIR)", | ||
"--project", | ||
"$(location %s)" % tsconfig, | ||
"--declaration", | ||
], | ||
**kwargs | ||
) | ||
|
||
# rules_nodejs does import remapping based on the module_name attribute. | ||
# The tsc macro is an instance of npm_package_bin under the covers which | ||
# doesn't take a module_name attribute. So, we use this wrapper rule to be | ||
# able to set the module_name attribute. | ||
_da_ts_library_rule( | ||
name = name, | ||
srcs = outs, | ||
# We don't do anything with the deps, but they are needed for | ||
# rules_nodejs's tracking of transitive dependencies. | ||
deps = deps, | ||
module_name = module_name, | ||
module_root = module_root, | ||
**kwargs | ||
) |