From 06e5bf16613cc57ed6bf1fdf4bd687f9b5f0a182 Mon Sep 17 00:00:00 2001 From: Keen Yee Liau Date: Mon, 14 Jan 2019 15:02:53 -0800 Subject: [PATCH] fix(bazel): Bazel schematics should add router package (#28141) This commit fixes a bug whereby a Bazel project created by the schematics would not compiled if project contains routing module. It is missing a dependency on the router package. PR Close #28141 --- .../bazel-workspace/files/src/BUILD.bazel.template | 3 ++- .../bazel/src/schematics/bazel-workspace/index.ts | 10 ++++++++++ .../src/schematics/bazel-workspace/index_spec.ts | 11 +++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/bazel/src/schematics/bazel-workspace/files/src/BUILD.bazel.template b/packages/bazel/src/schematics/bazel-workspace/files/src/BUILD.bazel.template index 67d0dfaaa6..4f01dafe18 100644 --- a/packages/bazel/src/schematics/bazel-workspace/files/src/BUILD.bazel.template +++ b/packages/bazel/src/schematics/bazel-workspace/files/src/BUILD.bazel.template @@ -23,7 +23,8 @@ ng_module( ]), deps = [ "@angular//packages/core", - "@angular//packages/platform-browser", + "@angular//packages/platform-browser",<% if (routing) { %> + "@angular//packages/router",<% } %> "@npm//@types", ], ) diff --git a/packages/bazel/src/schematics/bazel-workspace/index.ts b/packages/bazel/src/schematics/bazel-workspace/index.ts index d222d539ea..db4b97650a 100644 --- a/packages/bazel/src/schematics/bazel-workspace/index.ts +++ b/packages/bazel/src/schematics/bazel-workspace/index.ts @@ -51,6 +51,15 @@ export function clean(version: string): string|null { return matches && matches.pop() || null; } +/** + * Returns true if project contains routing module, false otherwise. + */ +function hasRoutingModule(host: Tree) { + let hasRouting = false; + host.visit((file: string) => { hasRouting = hasRouting || file.endsWith('-routing.module.ts'); }); + return hasRouting; +} + export default function(options: BazelWorkspaceOptions): Rule { return (host: Tree, context: SchematicContext) => { if (!options.name) { @@ -93,6 +102,7 @@ export default function(options: BazelWorkspaceOptions): Rule { utils: strings, ...options, 'dot': '.', ...workspaceVersions, + routing: hasRoutingModule(host), }), move(appDir), ])); diff --git a/packages/bazel/src/schematics/bazel-workspace/index_spec.ts b/packages/bazel/src/schematics/bazel-workspace/index_spec.ts index bf0efa4771..2963cdcbd8 100644 --- a/packages/bazel/src/schematics/bazel-workspace/index_spec.ts +++ b/packages/bazel/src/schematics/bazel-workspace/index_spec.ts @@ -51,6 +51,17 @@ describe('Bazel-workspace Schematic', () => { expect(content).toContain('entry_module = "demo_app/src/main.dev"'); }); + it('should add router if project contains routing module', () => { + let host = new UnitTestTree(new HostTree); + host.create('/demo/src/app/app-routing.module.ts', ''); + expect(host.files).toContain('/demo/src/app/app-routing.module.ts'); + const options = {...defaultOptions}; + host = schematicRunner.runSchematic('bazel-workspace', options, host); + expect(host.files).toContain('/demo/src/BUILD.bazel'); + const content = host.readContent('/demo/src/BUILD.bazel'); + expect(content).toContain('@angular//packages/router'); + }); + describe('WORKSPACE', () => { it('should contain project name', () => { const options = {...defaultOptions};