Skip to content

Commit c969fc8

Browse files
devversionjosephperrott
authored andcommitted
feat(ng-dev): expose entry-point with code-splitting for runtime consumers (#430)
We recently moved all `dependencies` that we bundle into the ng-dev CLI to the `devDependencies`. This is expected but breaks consumers deeply importing from `ng-dev` files that do not benefit from the bundling. We should remove all these deep files and just expose public entry-points with code splitting. This fixes the dependency issue and also allows us to limit access to deep code. We would rather want to expose necessary utilities explicitly, _knowing_ that they are used in consumer projects. PR Close #430
1 parent 86f2a07 commit c969fc8

File tree

8 files changed

+84
-46
lines changed

8 files changed

+84
-46
lines changed

BUILD.bazel

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ pkg_npm(
3535
],
3636
substitutions = NPM_PACKAGE_SUBSTITUTIONS,
3737
deps = [
38-
"//ng-dev",
39-
"//ng-dev:lib",
38+
"//ng-dev:bundles",
39+
"//ng-dev:types",
4040
"//tslint-rules:lib",
4141
],
4242
)

ng-dev/BUILD.bazel

+22-36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("//tools:defaults.bzl", "esbuild", "ts_library")
2+
load("//bazel:extract_types.bzl", "extract_types")
23

34
NG_DEV_EXTERNALS = [
45
# `ts-node` is optional for users who write their configurations in TypeScript.
@@ -12,6 +13,7 @@ ts_library(
1213
name = "ng-dev",
1314
srcs = [
1415
"cli.ts",
16+
"index.ts",
1517
],
1618
visibility = [
1719
"//:npm",
@@ -25,55 +27,39 @@ ts_library(
2527
"//ng-dev/misc",
2628
"//ng-dev/ngbot",
2729
"//ng-dev/pr",
30+
"//ng-dev/pr/config",
2831
"//ng-dev/pullapprove",
2932
"//ng-dev/release",
33+
"//ng-dev/release/config",
34+
"//ng-dev/release/publish",
35+
"//ng-dev/release/versioning",
3036
"//ng-dev/ts-circular-dependencies",
3137
"//ng-dev/utils",
3238
"@npm//@types/yargs",
3339
"@npm//yargs",
3440
],
3541
)
3642

37-
esbuild(
38-
name = "cli-bundle",
39-
entry_point = "cli.ts",
40-
external = NG_DEV_EXTERNALS,
41-
output = "cli-bundle.js",
42-
deps = [
43-
":ng-dev",
44-
],
43+
extract_types(
44+
name = "types",
45+
visibility = ["//:npm"],
46+
deps = [":ng-dev"],
4547
)
4648

4749
esbuild(
48-
name = "build-worker-bundle",
49-
entry_point = "//ng-dev/release/build:build-worker.ts",
50+
name = "bundles",
51+
entry_points = [
52+
"cli.ts",
53+
"index.ts",
54+
# These additional entry-points need to be generated since the `ng-dev` tool tries
55+
# to launch these files/scripts dynamically (through e.g. `spawn` or `fork`).
56+
"//ng-dev/release/build:build-worker.ts",
57+
"//ng-dev/pr/merge:strategies/commit-message-filter.ts",
58+
],
5059
external = NG_DEV_EXTERNALS,
51-
# Note: Needs to be named `build-worker.js` as this is the external path
52-
# the release build command resolves the script from.
53-
output = "build-worker.js",
60+
splitting = True,
61+
visibility = ["//:npm"],
5462
deps = [
55-
"//ng-dev/release/build",
56-
],
57-
)
58-
59-
genrule(
60-
name = "commit-message-filter",
61-
srcs = ["//ng-dev/pr/merge:strategies/commit-message-filter.js"],
62-
# Note: Needs to be named `commit-message-filter.js` as this is the name
63-
# the autosquash merge strategy expects the external file to be named.
64-
outs = ["commit-message-filter.js"],
65-
cmd = """cp $< $@""",
66-
)
67-
68-
filegroup(
69-
name = "lib",
70-
srcs = [
71-
":cli-bundle",
72-
# The bundle of `ng-dev` relies on the following files to be available next to the `ng-dev`
73-
# bundle. This is necessary because the CLI resolves these files using relative paths and
74-
# requires these to be external, compared to bundling those up.
75-
":build-worker-bundle",
76-
":commit-message-filter",
63+
":ng-dev",
7764
],
78-
visibility = ["//:npm"],
7965
)

ng-dev/index.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC 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+
9+
// Exposes config types, helpers for the individual commands.
10+
export * from './utils/config';
11+
export * from './caretaker/config';
12+
export * from './commit-message/config';
13+
export * from './format/config';
14+
export * from './pr/config';
15+
export * from './release/config';
16+
17+
// Exposes versioning utilities which are useful for building scripts with
18+
// respect to Angular's branching/versioning and release process.
19+
export * from './release/versioning';
20+
21+
// Exposes console utils that can be used by consumers to e.g. add messages
22+
// to the dev-infra log which is stored on failures.
23+
export * from './utils/console';
24+
25+
// Additional exports for adding custom release pre-staging, post-build checks.
26+
// TODO: Remove this once we have a public API for release hooks/checks
27+
export {ReleaseAction} from './release/publish/actions';
28+
export {
29+
FatalReleaseActionError,
30+
UserAbortedReleaseActionError,
31+
} from './release/publish/actions-error';

ng-dev/pr/merge/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("//tools:defaults.bzl", "jasmine_node_test", "ts_library")
22

33
exports_files([
4-
"strategies/commit-message-filter.js",
4+
"strategies/commit-message-filter.ts",
55
])
66

77
ts_library(

ng-dev/pr/merge/strategies/autosquash-merge.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {join} from 'path';
109
import {PullRequestFailure} from '../../common/validation/failures';
1110
import {PullRequest} from '../pull-request';
1211
import {MergeStrategy, TEMP_PR_HEAD_BRANCH} from './strategy';
1312

14-
/** Path to the commit message filter script. Git expects this paths to use forward slashes. */
15-
const MSG_FILTER_SCRIPT = join(__dirname, './commit-message-filter.js').replace(/\\/g, '/');
16-
1713
/**
1814
* Merge strategy that does not use the Github API for merging. Instead, it fetches
1915
* all target branches and the PR locally. The PR is then cherry-picked with autosquash
@@ -80,7 +76,7 @@ export class AutosquashMergeStrategy extends MergeStrategy {
8076
'filter-branch',
8177
'-f',
8278
'--msg-filter',
83-
`${MSG_FILTER_SCRIPT} ${prNumber}`,
79+
`${getCommitMessageFilterScriptPath()} ${prNumber}`,
8480
revisionRange,
8581
]);
8682

@@ -122,3 +118,12 @@ export class AutosquashMergeStrategy extends MergeStrategy {
122118
return null;
123119
}
124120
}
121+
122+
/** Gets the absolute file path to the commit-message filter script. */
123+
function getCommitMessageFilterScriptPath(): string {
124+
// We resolve the script using module resolution as in the package output
125+
// the worker might be bundled but exposed through a subpath export mapping.
126+
return require.resolve(
127+
'@angular/dev-infra-private/ng-dev/pr/merge/strategies/commit-message-filter',
128+
);
129+
}

ng-dev/pr/merge/strategies/commit-message-filter.js renamed to ng-dev/pr/merge/strategies/commit-message-filter.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
23
/**
34
* @license
45
* Copyright Google LLC All Rights Reserved.
@@ -34,7 +35,7 @@ if (require.main === module) {
3435
});
3536
}
3637

37-
function rewriteCommitMessage(message, prNumber) {
38+
function rewriteCommitMessage(message: string, prNumber: string) {
3839
const lines = message.split(/\n/);
3940
// Add the pull request number to the commit message title. This matches what
4041
// Github does when PRs are merged on the web through the `Squash and Merge` button.

ng-dev/release/build/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {BuiltPackage} from '../config/index';
1818
*/
1919
export async function buildReleaseOutput(): Promise<BuiltPackage[] | null> {
2020
return new Promise((resolve) => {
21-
const buildProcess = fork(require.resolve('./build-worker'), {
21+
const buildProcess = fork(getBuildWorkerScriptPath(), {
2222
// The stdio option is set to redirect any "stdout" output directly to the "stderr" file
2323
// descriptor. An additional "ipc" file descriptor is created to support communication with
2424
// the build process. https://nodejs.org/api/child_process.html#child_process_options_stdio.
@@ -34,3 +34,10 @@ export async function buildReleaseOutput(): Promise<BuiltPackage[] | null> {
3434
buildProcess.on('exit', () => resolve(builtPackages));
3535
});
3636
}
37+
38+
/** Gets the absolute file path to the build worker script. */
39+
function getBuildWorkerScriptPath(): string {
40+
// We resolve the worker script using module resolution as in the package output
41+
// the worker might be bundled but exposed through a subpath export mapping.
42+
return require.resolve('@angular/dev-infra-private/ng-dev/release/build/build-worker');
43+
}

package.json

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
"ng-dev": "ts-node --transpile-only ./ng-dev/cli.ts",
1111
"update-generated-files": "ts-node --transpile-only ./tools/update-generated-files.ts"
1212
},
13+
"exports": {
14+
"./ng-dev/*": {
15+
"default": "./ng-dev/bundles/*"
16+
},
17+
"./*": {
18+
"default": "./'"
19+
}
20+
},
1321
"packageManager": "yarn@3.2.0",
1422
"dependencies": {
1523
"@angular-devkit/build-angular": "14.0.0-next.3",

0 commit comments

Comments
 (0)