Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

language-support/ts generate package in commonjs format #4380

Merged
merged 5 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,8 @@ yarn_install(
"eslint/index.bzl": "def eslint_test(*args, **kwargs):\n pass",
"jest-cli/BUILD.bazel": 'exports_files(["index.bzl"])',
"jest-cli/index.bzl": "def jest_test(*args, **kwargs):\n pass",
"typescript/BUILD.bazel": 'exports_files(["index.bzl"])',
"typescript/index.bzl": "def tsc(*args, **kwargs):\n pass",
},
)

Expand Down
15 changes: 4 additions & 11 deletions language-support/ts/daml-ledger/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@

load("@os_info//:os_info.bzl", "is_windows")
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
load("//language-support/ts:defs.bzl", "da_ts_library")
load("//language-support/ts:eslint.bzl", "eslint_test")
load("@sdk_version//:sdk_version.bzl", "sdk_version")

ts_library(
da_ts_library(
name = "daml-ledger",
srcs = glob(["**/*.ts"]),
data = [
":LICENSE",
":README.md",
":package.json",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these files still included in the package with the new rule?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes they are, just checked.

],
srcs = ["index.ts"],
module_name = "@daml/ledger",
node_modules = "@language_support_ts_deps//:node_modules",
tsconfig = ":tsconfig.json",
visibility = ["//visibility:public"],
deps = [
"//language-support/ts/daml-types",
Expand Down Expand Up @@ -53,7 +46,7 @@ pkg_npm(
substitutions = {"0.0.0-SDKVERSION": sdk_version},
visibility = ["//visibility:public"],
deps = [
"daml-ledger",
":daml-ledger",
":license",
],
) if not is_windows else None
22 changes: 4 additions & 18 deletions language-support/ts/daml-react/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,18 @@

load("@os_info//:os_info.bzl", "is_windows")
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
load("//language-support/ts:defs.bzl", "da_ts_library")
load("//language-support/ts:eslint.bzl", "eslint_test")
load("//language-support/ts:jest.bzl", "jest_test")
load("@sdk_version//:sdk_version.bzl", "sdk_version")

ts_library(
da_ts_library(
name = "daml-react",
srcs = glob([
"**/*.ts",
]),
data = [
":LICENSE",
":README.md",
":package.json",
],
module_name = "@daml/react",
node_modules = "@language_support_ts_deps//:node_modules",
tsconfig = ":tsconfig.json",
visibility = ["//visibility:public"],
deps = [
"//language-support/ts/daml-ledger",
Expand Down Expand Up @@ -56,23 +49,16 @@ pkg_npm(
substitutions = {"0.0.0-SDKVERSION": sdk_version},
visibility = ["//visibility:public"],
deps = [
"daml-react",
":daml-react",
":license",
],
) if not is_windows else None

# we use the output js files for the jest test below.
filegroup(
name = "daml-react.js",
srcs = [":daml-react"],
output_group = "es5_sources",
)

jest_test(
name = "test",
# Use the js outputs because jest cannot resolve imports in the ts_library
# despite moduleNameMapper.
srcs = [":daml-react.js"],
srcs = [":daml-react"],
jest_config = ":jest.config.js",
deps = [
"//language-support/ts/daml-ledger",
Expand Down
14 changes: 4 additions & 10 deletions language-support/ts/daml-types/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
load("@language_support_ts_deps//eslint:index.bzl", "eslint_test")
load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("@os_info//:os_info.bzl", "is_windows")
load("//language-support/ts:defs.bzl", "da_ts_library")
load("//language-support/ts:eslint.bzl", "eslint_test")
load("//language-support/ts:jest.bzl", "jest_test")
load("@sdk_version//:sdk_version.bzl", "sdk_version")

ts_library(
da_ts_library(
name = "daml-types",
srcs = glob(["**/*.ts"]),
data = [
":LICENSE",
":README.md",
":package.json",
],
srcs = ["index.ts"],
module_name = "@daml/types",
node_modules = "@language_support_ts_deps//:node_modules",
tsconfig = ":tsconfig.json",
visibility = ["//visibility:public"],
deps = [
Expand Down Expand Up @@ -55,7 +49,7 @@ pkg_npm(
substitutions = {"0.0.0-SDKVERSION": sdk_version},
visibility = ["//visibility:public"],
deps = [
"daml-types",
":daml-types",
":license",
],
) if not is_windows else None
Expand Down
78 changes: 78 additions & 0 deletions language-support/ts/defs.bzl
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we find deps, via the global npm deps repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rules_nodejs traces them through this attribute and makes sure they're in the right place in the execroot. They come either from local targets or from the npm deps repo. E.g. https://github.com/digital-asset/daml/pull/4380/files#diff-cb32cca6075c7ecf0996019a06da7488R16-R20

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, thanks for the explanation!

module_name = module_name,
module_root = module_root,
**kwargs
)