Skip to content

Commit

Permalink
language-support/ts generate package in commonjs format (#4380)
Browse files Browse the repository at this point in the history
* 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
aherrmann-da and aherrmann authored Feb 4, 2020
1 parent ffa8180 commit 2dbd51b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 39 deletions.
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",
],
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,
module_name = module_name,
module_root = module_root,
**kwargs
)

0 comments on commit 2dbd51b

Please sign in to comment.