diff --git a/examples/angular_view_engine/BUILD.bazel b/examples/angular_view_engine/BUILD.bazel index 0bccf0762c..270d7480a0 100644 --- a/examples/angular_view_engine/BUILD.bazel +++ b/examples/angular_view_engine/BUILD.bazel @@ -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 @@ -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", + ], +) diff --git a/examples/angular_view_engine/src/BUILD.bazel b/examples/angular_view_engine/src/BUILD.bazel index acb8129441..cec783340c 100644 --- a/examples/angular_view_engine/src/BUILD.bazel +++ b/examples/angular_view_engine/src/BUILD.bazel @@ -60,6 +60,12 @@ ng_module( ], ) +filegroup( + name = "src_esm", + srcs = [":src"], + output_group = "es6_sources", +) + filegroup( name = "rxjs_umd_modules", srcs = [ @@ -137,6 +143,7 @@ rollup_bundle( }, output_dir = True, deps = [ + "//:examples_angular_view_engine", "//src", "@npm//rollup-plugin-amd", "@npm//rollup-plugin-commonjs", diff --git a/examples/angular_view_engine/src/app/BUILD.bazel b/examples/angular_view_engine/src/app/BUILD.bazel index 08093a112e..02b9e24fd4 100644 --- a/examples/angular_view_engine/src/app/BUILD.bazel +++ b/examples/angular_view_engine/src/app/BUILD.bazel @@ -26,3 +26,9 @@ ng_module( "@npm//@ngrx/store", ], ) + +filegroup( + name = "app_esm", + srcs = [":app"], + output_group = "es6_sources", +) diff --git a/examples/angular_view_engine/src/app/hello-world/BUILD.bazel b/examples/angular_view_engine/src/app/hello-world/BUILD.bazel index 33c977abc3..20e106eed2 100644 --- a/examples/angular_view_engine/src/app/hello-world/BUILD.bazel +++ b/examples/angular_view_engine/src/app/hello-world/BUILD.bazel @@ -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, diff --git a/examples/angular_view_engine/src/app/home/BUILD.bazel b/examples/angular_view_engine/src/app/home/BUILD.bazel index e88628c8e5..700df35b57 100644 --- a/examples/angular_view_engine/src/app/home/BUILD.bazel +++ b/examples/angular_view_engine/src/app/home/BUILD.bazel @@ -12,3 +12,9 @@ ng_module( "@npm//@angular/router", ], ) + +filegroup( + name = "home_esm", + srcs = [":home"], + output_group = "es6_sources", +) diff --git a/examples/angular_view_engine/src/app/todos/BUILD.bazel b/examples/angular_view_engine/src/app/todos/BUILD.bazel index e35ec0d7a7..7301e7d0c4 100644 --- a/examples/angular_view_engine/src/app/todos/BUILD.bazel +++ b/examples/angular_view_engine/src/app/todos/BUILD.bazel @@ -35,3 +35,9 @@ ng_module( "@npm//rxjs", ], ) + +filegroup( + name = "todos_esm", + srcs = [":todos"], + output_group = "es6_sources", +) diff --git a/examples/angular_view_engine/src/app/todos/reducers/BUILD.bazel b/examples/angular_view_engine/src/app/todos/reducers/BUILD.bazel index eddd3de3e9..9ede71c5ee 100644 --- a/examples/angular_view_engine/src/app/todos/reducers/BUILD.bazel +++ b/examples/angular_view_engine/src/app/todos/reducers/BUILD.bazel @@ -9,3 +9,9 @@ ts_library( "@npm//@ngrx/store", ], ) + +filegroup( + name = "reducers_esm", + srcs = [":reducers"], + output_group = "es6_sources", +) diff --git a/examples/angular_view_engine/src/lib/typography/BUILD.bazel b/examples/angular_view_engine/src/lib/typography/BUILD.bazel index c84729d253..3c03c3478d 100644 --- a/examples/angular_view_engine/src/lib/typography/BUILD.bazel +++ b/examples/angular_view_engine/src/lib/typography/BUILD.bazel @@ -11,3 +11,9 @@ ng_module( "@npm//@angular/core", ], ) + +filegroup( + name = "typography_esm", + srcs = [":typography"], + output_group = "es6_sources", +) diff --git a/examples/angular_view_engine/src/shared/material/BUILD.bazel b/examples/angular_view_engine/src/shared/material/BUILD.bazel index 9dec0e57b5..74f77d039f 100644 --- a/examples/angular_view_engine/src/shared/material/BUILD.bazel +++ b/examples/angular_view_engine/src/shared/material/BUILD.bazel @@ -17,3 +17,9 @@ ng_module( "@npm//@angular/material", ], ) + +filegroup( + name = "material_esm", + srcs = [":material"], + output_group = "es6_sources", +) diff --git a/packages/typescript/test/ts_library_esm_with_jest/.babelrc b/packages/typescript/test/ts_library_esm_with_jest/.babelrc new file mode 100644 index 0000000000..af2cb75a9a --- /dev/null +++ b/packages/typescript/test/ts_library_esm_with_jest/.babelrc @@ -0,0 +1,12 @@ +{ + "presets": [ + [ + "@babel/preset-env", + { + "targets": { + "node": "current", + }, + }, + ], + ] +} \ No newline at end of file diff --git a/packages/typescript/test/ts_library_esm_with_jest/BUILD.bazel b/packages/typescript/test/ts_library_esm_with_jest/BUILD.bazel new file mode 100644 index 0000000000..2b8d30426a --- /dev/null +++ b/packages/typescript/test/ts_library_esm_with_jest/BUILD.bazel @@ -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", + ], +) diff --git a/packages/typescript/test/ts_library_esm_with_jest/jest.config.js b/packages/typescript/test/ts_library_esm_with_jest/jest.config.js new file mode 100644 index 0000000000..e153404932 --- /dev/null +++ b/packages/typescript/test/ts_library_esm_with_jest/jest.config.js @@ -0,0 +1,6 @@ +module.exports = { + testEnvironment: 'node', + transform: {'^.+\\.mjs?$': ['babel-jest', {configFile: __dirname + '/.babelrc'}]}, + testMatch: ['**/?(*.)(spec|test).?(m)js?(x)'], + moduleFileExtensions: ['js', 'mjs'], +}; diff --git a/packages/typescript/test/ts_library_esm_with_jest/lib.test.ts b/packages/typescript/test/ts_library_esm_with_jest/lib.test.ts new file mode 100644 index 0000000000..0ea73f6237 --- /dev/null +++ b/packages/typescript/test/ts_library_esm_with_jest/lib.test.ts @@ -0,0 +1,7 @@ +import {doStuff} from './lib'; + +describe('doStuff', () => { + it('should do some stuff', () => { + expect(doStuff('boom')).toContain('boom'); + }); +}); diff --git a/packages/typescript/test/ts_library_esm_with_jest/lib.ts b/packages/typescript/test/ts_library_esm_with_jest/lib.ts new file mode 100644 index 0000000000..d7318214c4 --- /dev/null +++ b/packages/typescript/test/ts_library_esm_with_jest/lib.ts @@ -0,0 +1,3 @@ +export function doStuff(a: string): string { + return a +} \ No newline at end of file diff --git a/packages/typescript/test/ts_library_esm_with_jest/ts_jest_test.bzl b/packages/typescript/test/ts_library_esm_with_jest/ts_jest_test.bzl new file mode 100644 index 0000000000..4c531eedb3 --- /dev/null +++ b/packages/typescript/test/ts_library_esm_with_jest/ts_jest_test.bzl @@ -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 + )