Skip to content

Commit

Permalink
refactor(examples): use pkg_npm to replace root workspace linking in …
Browse files Browse the repository at this point in the history
…examples/angular_view_engine
  • Loading branch information
gregmagolan authored and alexeagle committed Jun 16, 2020
1 parent e12b3af commit 080b460
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/angular_view_engine/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")

package(default_visibility = ["//:__subpackages__"])

# ts_library and ng_module use the `//:tsconfig.json` target
Expand All @@ -13,3 +15,23 @@ sh_test(
name = "dummy_test",
srcs = ["dummy_test.sh"],
)

# Here we selectively chose which files to include in node_modules/examples_angular_view_engine
# as node_modules/examples_angular_view_engine is no longer automatically linke to the root of bazel-bin.
# Because ngfactory files are generated with absolute `examples_angular_view_engine/` imports, we need
# to ensure all the files that these imports reference are available under
# node_modules/examples_angular_view_engine for rollup_bundle to find.
pkg_npm(
name = "examples_angular_view_engine",
package_name = "examples_angular_view_engine",
deps = [
"//src:src_esm",
"//src/app:app_esm",
"//src/app/hello-world:hello-world_esm",
"//src/app/home:home_esm",
"//src/app/todos:todos_esm",
"//src/app/todos/reducers:reducers_esm",
"//src/lib/typography:typography_esm",
"//src/shared/material:material_esm",
],
)
7 changes: 7 additions & 0 deletions examples/angular_view_engine/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ ng_module(
],
)

filegroup(
name = "src_esm",
srcs = [":src"],
output_group = "es6_sources",
)

filegroup(
name = "rxjs_umd_modules",
srcs = [
Expand Down Expand Up @@ -137,6 +143,7 @@ rollup_bundle(
},
output_dir = True,
deps = [
"//:examples_angular_view_engine",
"//src",
"@npm//rollup-plugin-amd",
"@npm//rollup-plugin-commonjs",
Expand Down
6 changes: 6 additions & 0 deletions examples/angular_view_engine/src/app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ ng_module(
"@npm//@ngrx/store",
],
)

filegroup(
name = "app_esm",
srcs = [":app"],
output_group = "es6_sources",
)
6 changes: 6 additions & 0 deletions examples/angular_view_engine/src/app/hello-world/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ ng_module(
],
)

filegroup(
name = "hello-world_esm",
srcs = [":hello-world"],
output_group = "es6_sources",
)

ts_library(
name = "test_lib",
testonly = 1,
Expand Down
6 changes: 6 additions & 0 deletions examples/angular_view_engine/src/app/home/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ ng_module(
"@npm//@angular/router",
],
)

filegroup(
name = "home_esm",
srcs = [":home"],
output_group = "es6_sources",
)
6 changes: 6 additions & 0 deletions examples/angular_view_engine/src/app/todos/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ ng_module(
"@npm//rxjs",
],
)

filegroup(
name = "todos_esm",
srcs = [":todos"],
output_group = "es6_sources",
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ ts_library(
"@npm//@ngrx/store",
],
)

filegroup(
name = "reducers_esm",
srcs = [":reducers"],
output_group = "es6_sources",
)
6 changes: 6 additions & 0 deletions examples/angular_view_engine/src/lib/typography/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ ng_module(
"@npm//@angular/core",
],
)

filegroup(
name = "typography_esm",
srcs = [":typography"],
output_group = "es6_sources",
)
6 changes: 6 additions & 0 deletions examples/angular_view_engine/src/shared/material/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ ng_module(
"@npm//@angular/material",
],
)

filegroup(
name = "material_esm",
srcs = [":material"],
output_group = "es6_sources",
)
12 changes: 12 additions & 0 deletions packages/typescript/test/ts_library_esm_with_jest/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current",
},
},
],
]
}
50 changes: 50 additions & 0 deletions packages/typescript/test/ts_library_esm_with_jest/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
load("@build_bazel_rules_nodejs//:index.bzl", "copy_to_bin")
load("//packages/typescript:index.bzl", "ts_library")
load(":ts_jest_test.bzl", "ts_jest_test")

ts_library(
name = "lib",
srcs = [
"lib.ts",
],
# NB: hacky hidden configuration setting so that es6_sources does not include tsickle
# .externs.js outputs
runtime = "nodejs",
deps = [
"@npm//@types/node",
],
)

# Shenanigans for Windows which doesn't have runfiles symlinks
# We need the jest config to be in the output tree where the specs are
copy_to_bin(
name = "jest_config",
srcs = [
"jest.config.js",
],
)

# Same goes for babelrc. We can't add it to the jest_config copy_to_bin
# since must be a file that is passed to jest in the --config arg.
copy_to_bin(
name = "babel_rc",
srcs = [
".babelrc",
],
)

ts_jest_test(
name = "test",
srcs = [
"lib.test.ts",
],
data = [
":babel_rc",
],
jest_config = ":jest_config",
deps = [
":lib",
"@npm//@babel/preset-env",
"@npm//babel-jest",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
testEnvironment: 'node',
transform: {'^.+\\.mjs?$': ['babel-jest', {configFile: __dirname + '/.babelrc'}]},
testMatch: ['**/?(*.)(spec|test).?(m)js?(x)'],
moduleFileExtensions: ['js', 'mjs'],
};
7 changes: 7 additions & 0 deletions packages/typescript/test/ts_library_esm_with_jest/lib.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {doStuff} from './lib';

describe('doStuff', () => {
it('should do some stuff', () => {
expect(doStuff('boom')).toContain('boom');
});
});
3 changes: 3 additions & 0 deletions packages/typescript/test/ts_library_esm_with_jest/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function doStuff(a: string): string {
return a
}
41 changes: 41 additions & 0 deletions packages/typescript/test/ts_library_esm_with_jest/ts_jest_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Simple macro around jest_test"""

load("@npm//jest-cli:index.bzl", _jest_test = "jest_test")
load("//packages/typescript:index.bzl", "ts_library")

def ts_jest_test(name, srcs, jest_config, deps = [], data = [], **kwargs):
"""A macro around the autogenerated jest_test rule that takes typescript sources
Uses ts_library prodmode esm output"""

ts_library(
name = "%s_ts" % name,
srcs = srcs,
data = data,
deps = deps + ["@npm//@types/jest"],
# NB: hacky hidden configuration setting so that es6_sources does not include tsickle
# .externs.js outputs
runtime = "nodejs",
# Prevent requesting the .d.ts output as that will also produce lib.test.js
# Without sandbox (e.g. windows) that will be loaded instead of lib.test.mjs
tags = ["manual"],
)
native.filegroup(
name = "%s_esm" % name,
srcs = [":%s_ts" % name],
output_group = "es6_sources",
)

args = [
"--no-cache",
"--no-watchman",
"--ci",
]
args.extend(["--config", "$$(rlocation $(rootpath %s))" % jest_config])

_jest_test(
name = name,
data = [jest_config, ":%s_esm" % name] + deps + data,
templated_args = args,
**kwargs
)

0 comments on commit 080b460

Please sign in to comment.