Skip to content

Commit 0e3a225

Browse files
committed
feat(@angular-devkit/build-ng-packagr): implement stable architect API
1 parent e96afb8 commit 0e3a225

File tree

6 files changed

+192
-23
lines changed

6 files changed

+192
-23
lines changed

packages/angular_devkit/build_ng_packagr/builders.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"builders": {
44
"build": {
55
"class": "./src/build",
6+
"implementation": "./src/build/index2",
67
"schema": "./src/build/schema.json",
78
"description": "Build a library with ng-packagr."
89
}

packages/angular_devkit/build_ng_packagr/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
"semver": "5.6.0"
1313
},
1414
"peerDependencies": {
15-
"ng-packagr": "^2.2.0 || ^3.0.0 || ^4.0.0"
15+
"ng-packagr": "^4.0.0"
1616
},
1717
"devDependencies": {
1818
"@angular/compiler": "^8.0.0-beta.0",
1919
"@angular/compiler-cli": "^8.0.0-beta.0",
20-
"ng-packagr": "^4.2.0",
20+
"ng-packagr": "^4.0.0",
2121
"tsickle": ">=0.34.0",
2222
"tslib": "^1.9.0"
2323
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect/src/index2';
9+
import { resolve } from 'path';
10+
import { Observable, from } from 'rxjs';
11+
import { catchError, mapTo, switchMap } from 'rxjs/operators';
12+
import { Schema as NgPackagrBuilderOptions } from './schema';
13+
14+
async function initialize(
15+
options: NgPackagrBuilderOptions,
16+
root: string,
17+
): Promise<import ('ng-packagr').NgPackagr> {
18+
const packager = (await import('ng-packagr')).ngPackagr();
19+
20+
packager.forProject(resolve(root, options.project));
21+
22+
if (options.tsConfig) {
23+
packager.withTsConfig(resolve(root, options.tsConfig));
24+
}
25+
26+
return packager;
27+
}
28+
29+
export function buildLibrary(
30+
options: NgPackagrBuilderOptions,
31+
context: BuilderContext,
32+
): Observable<BuilderOutput> {
33+
return from(initialize(options, context.workspaceRoot)).pipe(
34+
switchMap(packager => options.watch ? packager.watch() : packager.build()),
35+
mapTo({ success: true }),
36+
catchError(error => {
37+
context.reportStatus('Error: ' + error);
38+
39+
return [{ success: false }];
40+
}),
41+
);
42+
}
43+
44+
export default createBuilder<Record<string, string> & NgPackagrBuilderOptions>(buildLibrary);
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { WorkspaceNodeModulesArchitectHost } from '@angular-devkit/architect/node';
9+
import { Architect } from '@angular-devkit/architect/src/index2';
10+
import { TestProjectHost } from '@angular-devkit/architect/testing';
11+
import { TestingArchitectHost } from '@angular-devkit/architect/testing/testing-architect-host';
12+
import { experimental, join, normalize, schema, virtualFs } from '@angular-devkit/core';
13+
import { map, take, tap } from 'rxjs/operators';
14+
15+
const devkitRoot = (global as unknown as { _DevKitRoot: string})._DevKitRoot;
16+
const workspaceRoot = join(
17+
normalize(devkitRoot),
18+
'tests/angular_devkit/build_ng_packagr/ng-packaged/',
19+
);
20+
21+
describe('NgPackagr Builder', () => {
22+
const host = new TestProjectHost(workspaceRoot);
23+
let architect: Architect;
24+
25+
beforeEach(async () => {
26+
await host.initialize().toPromise();
27+
28+
const registry = new schema.CoreSchemaRegistry();
29+
registry.addPostTransform(schema.transforms.addUndefinedDefaults);
30+
31+
const workspace = await experimental.workspace.Workspace.fromPath(host, host.root(), registry);
32+
const architectHost = new TestingArchitectHost(
33+
host.root(),
34+
host.root(),
35+
new WorkspaceNodeModulesArchitectHost(workspace, host.root()),
36+
);
37+
architect = new Architect(architectHost, registry);
38+
});
39+
40+
afterEach(() => host.restore().toPromise());
41+
42+
it('builds and packages a library', async () => {
43+
const run = await architect.scheduleTarget({ project: 'lib', target: 'build' });
44+
45+
// Use this once Jasmine 3+ is available
46+
// await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: true }));
47+
const result = await run.result;
48+
expect(result.success).toBe(true);
49+
50+
await run.stop();
51+
52+
expect(host.scopedSync().exists(normalize('./dist/lib/fesm5/lib.js'))).toBe(true);
53+
const content = virtualFs.fileBufferToString(
54+
host.scopedSync().read(normalize('./dist/lib/fesm5/lib.js')),
55+
);
56+
expect(content).toContain('lib works');
57+
});
58+
59+
it('rebuilds on TS file changes', async () => {
60+
const goldenValueFiles: { [path: string]: string } = {
61+
'projects/lib/src/lib/lib.component.ts': `
62+
import { Component } from '@angular/core';
63+
64+
@Component({
65+
selector: 'lib',
66+
template: 'lib update works!'
67+
})
68+
export class LibComponent { }
69+
`,
70+
};
71+
72+
const run = await architect.scheduleTarget(
73+
{ project: 'lib', target: 'build' },
74+
{ watch: true },
75+
);
76+
77+
let buildNumber = 0;
78+
79+
await run.output.pipe(
80+
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
81+
map(() => {
82+
debugger;
83+
const fileName = './dist/lib/fesm5/lib.js';
84+
const content = virtualFs.fileBufferToString(
85+
host.scopedSync().read(normalize(fileName)),
86+
);
87+
88+
return content;
89+
}),
90+
tap(content => {
91+
buildNumber += 1;
92+
switch (buildNumber) {
93+
case 1:
94+
expect(content).toMatch(/lib works/);
95+
host.writeMultipleFiles(goldenValueFiles);
96+
break;
97+
98+
case 2:
99+
expect(content).toMatch(/lib update works/);
100+
break;
101+
default:
102+
break;
103+
}
104+
}),
105+
take(2),
106+
).toPromise();
107+
108+
await run.stop();
109+
});
110+
});

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
23
"title": "ng-packagr Target",
34
"description": "ng-packagr target options for Build Architect.",
45
"type": "object",

yarn.lock

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,10 +1647,10 @@ builtin-modules@^1.0.0, builtin-modules@^1.1.1:
16471647
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f"
16481648
integrity sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=
16491649

1650-
builtin-modules@^2.0.0:
1651-
version "2.0.0"
1652-
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-2.0.0.tgz#60b7ef5ae6546bd7deefa74b08b62a43a232648e"
1653-
integrity sha512-3U5kUA5VPsRUA3nofm/BXX7GVHKfxz0hOBAPxXrIvHzlDRkQVqEn6yi8QJegxl4LzOHLdvb7XF5dVawa/VVYBg==
1650+
builtin-modules@^3.0.0:
1651+
version "3.0.0"
1652+
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.0.0.tgz#1e587d44b006620d90286cc7a9238bbc6129cab1"
1653+
integrity sha512-hMIeU4K2ilbXV6Uv93ZZ0Avg/M91RaKXucQ+4me2Do1txxBDyDZWCBa5bJSLqoNTRpXTLwEzIk1KmloenDDjhg==
16541654

16551655
builtin-status-codes@^3.0.0:
16561656
version "3.0.0"
@@ -6266,10 +6266,10 @@ neo-async@^2.5.0:
62666266
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.0.tgz#b9d15e4d71c6762908654b5183ed38b753340835"
62676267
integrity sha512-MFh0d/Wa7vkKO3Y3LlacqAEeHK0mckVqzDieUKTT+KGxi+zIpeVsFxymkIiRpbpDziHc290Xr9A1O4Om7otoRA==
62686268

6269-
ng-packagr@^4.2.0:
6270-
version "4.4.0"
6271-
resolved "https://registry.yarnpkg.com/ng-packagr/-/ng-packagr-4.4.0.tgz#b868a829b5743870c6392491de3faf4e4cec6f4d"
6272-
integrity sha512-dLpC/kmQsdbkL96ZclGjNRhq/J4MwpPKwPYNom74lvXqFC2jbbT/fnwmxX9WKXjvE8MEGsg2D2x8MsRURiNscg==
6269+
ng-packagr@^4.0.0:
6270+
version "4.7.1"
6271+
resolved "https://registry.yarnpkg.com/ng-packagr/-/ng-packagr-4.7.1.tgz#a3e8fd2a7b70573a3a7759c4c57408a3b0e8ce11"
6272+
integrity sha512-MIPKxyrnV22fS3wSfst2XjwWOonFKujVVEnIehYJhiu8GOg37bCdbbr9plsE1jRDmDAUz6M1MvdKibUrJyRp6Q==
62736273
dependencies:
62746274
"@ngtools/json-schema" "^1.1.0"
62756275
autoprefixer "^9.0.0"
@@ -6285,14 +6285,15 @@ ng-packagr@^4.2.0:
62856285
less-plugin-npm-import "^2.1.0"
62866286
node-sass "^4.9.3"
62876287
node-sass-tilde-importer "^1.0.0"
6288+
opencollective-postinstall "^2.0.1"
62886289
postcss "^7.0.0"
62896290
postcss-url "^8.0.0"
62906291
read-pkg-up "^4.0.0"
62916292
rimraf "^2.6.1"
6292-
rollup "^0.66.0"
6293+
rollup "^0.67.0"
62936294
rollup-plugin-commonjs "^9.1.3"
62946295
rollup-plugin-json "^3.1.0"
6295-
rollup-plugin-node-resolve "^3.0.0"
6296+
rollup-plugin-node-resolve "^4.0.0"
62966297
rollup-plugin-sourcemaps "^0.4.2"
62976298
rxjs "^6.0.0"
62986299
stylus "^0.54.5"
@@ -6709,6 +6710,11 @@ onetime@^2.0.0:
67096710
dependencies:
67106711
mimic-fn "^1.0.0"
67116712

6713+
opencollective-postinstall@^2.0.1:
6714+
version "2.0.2"
6715+
resolved "https://registry.yarnpkg.com/opencollective-postinstall/-/opencollective-postinstall-2.0.2.tgz#5657f1bede69b6e33a45939b061eb53d3c6c3a89"
6716+
integrity sha512-pVOEP16TrAO2/fjej1IdOyupJY8KDUM1CvsaScRbw6oddvpQoOfGk4ywha0HKKVAD6RkW4x6Q+tNBwhf3Bgpuw==
6717+
67126718
[email protected], opn@^5.1.0:
67136719
version "5.4.0"
67146720
resolved "https://registry.yarnpkg.com/opn/-/opn-5.4.0.tgz#cb545e7aab78562beb11aa3bfabc7042e1761035"
@@ -7088,7 +7094,7 @@ path-key@^2.0.0, path-key@^2.0.1:
70887094
resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"
70897095
integrity sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=
70907096

7091-
path-parse@^1.0.5:
7097+
path-parse@^1.0.5, path-parse@^1.0.6:
70927098
version "1.0.6"
70937099
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c"
70947100
integrity sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==
@@ -7956,6 +7962,13 @@ resolve@^1.1.5, resolve@^1.1.6, resolve@^1.1.7, resolve@^1.3.2, resolve@^1.8.1:
79567962
dependencies:
79577963
path-parse "^1.0.5"
79587964

7965+
resolve@^1.10.0:
7966+
version "1.10.0"
7967+
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.10.0.tgz#3bdaaeaf45cc07f375656dfd2e54ed0810b101ba"
7968+
integrity sha512-3sUr9aq5OfSg2S9pNtPA9hL1FVEAjvfOC4leW0SNf/mpnaakz2a9femSd6LqAww2RaFctwyf1lCqnTHuF1rxDg==
7969+
dependencies:
7970+
path-parse "^1.0.6"
7971+
79597972
responselike@^1.0.2:
79607973
version "1.0.2"
79617974
resolved "https://registry.yarnpkg.com/responselike/-/responselike-1.0.2.tgz#918720ef3b631c5642be068f15ade5a46f4ba1e7"
@@ -8018,14 +8031,14 @@ rollup-plugin-json@^3.1.0:
80188031
dependencies:
80198032
rollup-pluginutils "^2.3.1"
80208033

8021-
rollup-plugin-node-resolve@^3.0.0:
8022-
version "3.4.0"
8023-
resolved "https://registry.yarnpkg.com/rollup-plugin-node-resolve/-/rollup-plugin-node-resolve-3.4.0.tgz#908585eda12e393caac7498715a01e08606abc89"
8024-
integrity sha512-PJcd85dxfSBWih84ozRtBkB731OjXk0KnzN0oGp7WOWcarAFkVa71cV5hTJg2qpVsV2U8EUwrzHP3tvy9vS3qg==
8034+
rollup-plugin-node-resolve@^4.0.0:
8035+
version "4.0.1"
8036+
resolved "https://registry.yarnpkg.com/rollup-plugin-node-resolve/-/rollup-plugin-node-resolve-4.0.1.tgz#f95765d174e5daeef9ea6268566141f53aa9d422"
8037+
integrity sha512-fSS7YDuCe0gYqKsr5OvxMloeZYUSgN43Ypi1WeRZzQcWtHgFayV5tUSPYpxuaioIIWaBXl6NrVk0T2/sKwueLg==
80258038
dependencies:
8026-
builtin-modules "^2.0.0"
8039+
builtin-modules "^3.0.0"
80278040
is-module "^1.0.0"
8028-
resolve "^1.1.6"
8041+
resolve "^1.10.0"
80298042

80308043
rollup-plugin-sourcemaps@^0.4.2:
80318044
version "0.4.2"
@@ -8043,10 +8056,10 @@ rollup-pluginutils@^2.0.1, rollup-pluginutils@^2.3.1, rollup-pluginutils@^2.3.3:
80438056
estree-walker "^0.5.2"
80448057
micromatch "^2.3.11"
80458058

8046-
rollup@^0.66.0:
8047-
version "0.66.6"
8048-
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.66.6.tgz#ce7d6185beb7acea644ce220c25e71ae03275482"
8049-
integrity sha512-J7/SWanrcb83vfIHqa8+aVVGzy457GcjA6GVZEnD0x2u4OnOd0Q1pCrEoNe8yLwM6z6LZP02zBT2uW0yh5TqOw==
8059+
rollup@^0.67.0:
8060+
version "0.67.4"
8061+
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.67.4.tgz#8ed6b0993337f84ec8a0387f824fa6c197e833ec"
8062+
integrity sha512-AVuP73mkb4BBMUmksQ3Jw0jTrBTU1i7rLiUYjFxLZGb3xiFmtVEg40oByphkZAsiL0bJC3hRAJUQos/e5EBd+w==
80508063
dependencies:
80518064
"@types/estree" "0.0.39"
80528065
"@types/node" "*"

0 commit comments

Comments
 (0)