Skip to content

Commit d669dd6

Browse files
alan-agius4hansl
authored andcommitted
feat(@angular-devkit/build-ng-packagr): add support for watch when building a library
`ng-packagr` version `4.0.0-rc.3`, lands the incremental builds feature. More info: https://github.com/dherges/ng-packagr/blob/master/CHANGELOG.md#400-rc2-2018-06-23 `enableResourceInlining` needs to be enabled for libraries that contain components Closes: #10643
1 parent c8240d9 commit d669dd6

File tree

4 files changed

+109
-21
lines changed

4 files changed

+109
-21
lines changed

package-lock.json

+41-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/angular_devkit/build_ng_packagr/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"dependencies": {
1212
"@angular-devkit/architect": "0.0.0",
1313
"@angular-devkit/core": "0.0.0",
14-
"rxjs": "^6.0.0"
14+
"rxjs": "^6.0.0",
15+
"semver": "^5.3.0"
1516
},
1617
"peerDependencies": {
1718
"ng-packagr": "^2.2.0 || ^3.0.0 || ^4.0.0"

packages/angular_devkit/build_ng_packagr/src/build/index.ts

+61-8
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,55 @@ import {
1212
BuilderConfiguration,
1313
BuilderContext,
1414
} from '@angular-devkit/architect';
15-
import { getSystemPath, normalize, resolve } from '@angular-devkit/core';
15+
import { getSystemPath, normalize, resolve, tags } from '@angular-devkit/core';
16+
import * as fs from 'fs';
1617
import * as ngPackagr from 'ng-packagr';
17-
import { Observable } from 'rxjs';
18+
import { EMPTY, Observable } from 'rxjs';
19+
import { catchError, tap } from 'rxjs/operators';
20+
import * as semver from 'semver';
21+
22+
const NEW_NG_PACKAGR_VERSION = '4.0.0-rc.3';
1823

1924
// TODO move this function to architect or somewhere else where it can be imported from.
2025
// Blatantly copy-pasted from 'require-project-module.ts'.
2126
function requireProjectModule(root: string, moduleName: string) {
2227
return require(require.resolve(moduleName, { paths: [root] }));
2328
}
2429

30+
function resolveProjectModule(root: string, moduleName: string) {
31+
return require.resolve(moduleName, { paths: [root] });
32+
}
2533

2634
export interface NgPackagrBuilderOptions {
2735
project: string;
2836
tsConfig?: string;
37+
watch?: boolean;
38+
}
39+
40+
function checkNgPackagrVersion(projectRoot: string): boolean {
41+
let ngPackagrJsonPath;
42+
43+
try {
44+
ngPackagrJsonPath = resolveProjectModule(projectRoot, 'ng-packagr/package.json');
45+
} catch {
46+
// ng-packagr is not installed
47+
throw new Error(tags.stripIndent`
48+
ng-packagr is not installed. Run \`npm install ng-packagr --save-dev\` and try again.
49+
`);
50+
}
51+
52+
const ngPackagrPackageJson = fs.readFileSync(ngPackagrJsonPath).toString();
53+
const ngPackagrVersion = JSON.parse(ngPackagrPackageJson)['version'];
54+
55+
if (!semver.gte(ngPackagrVersion, NEW_NG_PACKAGR_VERSION)) {
56+
throw new Error(tags.stripIndent`
57+
The installed version of ng-packagr is ${ngPackagrVersion}. The watch feature
58+
requires ng-packagr version to satisfy ${NEW_NG_PACKAGR_VERSION}.
59+
Please upgrade your ng-packagr version.
60+
`);
61+
}
62+
63+
return true;
2964
}
3065

3166
export class NgPackagrBuilder implements Builder<NgPackagrBuilderOptions> {
@@ -53,12 +88,30 @@ export class NgPackagrBuilder implements Builder<NgPackagrBuilderOptions> {
5388
ngPkgProject.withTsConfig(tsConfigPath);
5489
}
5590

56-
ngPkgProject.build()
57-
.then(() => {
58-
obs.next({ success: true });
59-
obs.complete();
60-
})
61-
.catch((e) => obs.error(e));
91+
if (options.watch) {
92+
checkNgPackagrVersion(getSystemPath(root));
93+
94+
const ngPkgSubscription = ngPkgProject
95+
.watch()
96+
.pipe(
97+
tap(() => obs.next({ success: true })),
98+
catchError(e => {
99+
obs.error(e);
100+
101+
return EMPTY;
102+
}),
103+
)
104+
.subscribe();
105+
106+
return () => ngPkgSubscription.unsubscribe();
107+
} else {
108+
ngPkgProject.build()
109+
.then(() => {
110+
obs.next({ success: true });
111+
obs.complete();
112+
})
113+
.catch(e => obs.error(e));
114+
}
62115
});
63116
}
64117

packages/angular_devkit/build_ng_packagr/src/build/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
"tsConfig": {
1111
"type": "string",
1212
"description": "The file path of the TypeScript configuration file."
13+
},
14+
"watch": {
15+
"type": "boolean",
16+
"description": "Run build when files change.",
17+
"default": false
1318
}
1419
},
1520
"additionalProperties": false,

0 commit comments

Comments
 (0)