Skip to content

Commit 5e82075

Browse files
committed
refactor(cdk/private): set up private package and style loader
Creates a new `cdk/private` package in which we can put shared code that isn't meant to be public and creates a new style loader service that can be used to load structural styles for components.
1 parent 7cf8c6c commit 5e82075

File tree

9 files changed

+132
-0
lines changed

9 files changed

+132
-0
lines changed

.ng-dev/commit-message.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const commitMessage: CommitMessageConfig = {
3131
'cdk/overlay',
3232
'cdk/platform',
3333
'cdk/portal',
34+
'cdk/private',
3435
'cdk/schematics',
3536
'cdk/scrolling',
3637
'cdk/stepper',

scripts/check-entry-point-setup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const packagesDir = join(__dirname, '../src');
2323
*/
2424
const excludeGlobs = [
2525
'cdk/testing/private',
26+
'cdk/private',
2627
'*/schematics/**',
2728
// The protractor testing entry-point is no longer publicly available,
2829
// but exists in the repository until it can be removed in g3.

src/cdk/config.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ CDK_ENTRYPOINTS = [
2626
"testing",
2727
"testing/testbed",
2828
"testing/selenium-webdriver",
29+
"private",
2930
]
3031

3132
# List of all entry-point targets of the Angular Material package.

src/cdk/private/BUILD.bazel

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
load("//tools:defaults.bzl", "markdown_to_html", "ng_module")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
ng_module(
6+
name = "private",
7+
srcs = glob(
8+
["**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
11+
deps = [
12+
"@npm//@angular/core",
13+
],
14+
)
15+
16+
markdown_to_html(
17+
name = "overview",
18+
srcs = [":private.md"],
19+
)
20+
21+
filegroup(
22+
name = "source-files",
23+
srcs = glob(["**/*.ts"]),
24+
)

src/cdk/private/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
export * from './public-api';

src/cdk/private/private.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Private utilities meant to be used only within the CDK and Material.

src/cdk/private/public-api.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
export * from './style-loader';

src/cdk/private/style-loader.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
import {
10+
ApplicationRef,
11+
ComponentRef,
12+
createComponent,
13+
EnvironmentInjector,
14+
inject,
15+
Injectable,
16+
Type,
17+
} from '@angular/core';
18+
19+
/** Apps in which we've loaded styles. */
20+
const appsWithLoaders = new WeakMap<
21+
ApplicationRef,
22+
{
23+
/** Style loaders that have been added. */
24+
loaders: Set<Type<unknown>>;
25+
26+
/** References to the instantiated loaders. */
27+
refs: ComponentRef<unknown>[];
28+
}
29+
>();
30+
31+
/**
32+
* Service that loads structural styles dynamically
33+
* and ensures that they're only loaded once per app.
34+
*/
35+
@Injectable({providedIn: 'root'})
36+
export class _CdkPrivateStyleLoader {
37+
private _appRef = inject(ApplicationRef);
38+
private _environmentInjector = inject(EnvironmentInjector);
39+
40+
/**
41+
* Loads a set of styles.
42+
* @param loader Component which will be instantiated to load the styles.
43+
*/
44+
load(loader: Type<unknown>): void {
45+
let data = appsWithLoaders.get(this._appRef);
46+
47+
// If we haven't loaded for this app before, we have to initialize it.
48+
if (!data) {
49+
data = {loaders: new Set(), refs: []};
50+
appsWithLoaders.set(this._appRef, data);
51+
52+
// When the app is destroyed, we need to clean up all the related loaders.
53+
this._appRef.onDestroy(() => {
54+
appsWithLoaders.get(this._appRef)?.refs.forEach(ref => ref.destroy());
55+
appsWithLoaders.delete(this._appRef);
56+
});
57+
}
58+
59+
// If the loader hasn't been loaded before, we need to instatiate it.
60+
if (!data.loaders.has(loader)) {
61+
data.loaders.add(loader);
62+
data.refs.push(createComponent(loader, {environmentInjector: this._environmentInjector}));
63+
}
64+
}
65+
}

tools/public_api_guard/cdk/private.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## API Report File for "components-srcs"
2+
3+
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
4+
5+
```ts
6+
7+
import * as i0 from '@angular/core';
8+
import { Type } from '@angular/core';
9+
10+
// @public
11+
export class _CdkPrivateStyleLoader {
12+
load(loader: Type<unknown>): void;
13+
// (undocumented)
14+
static ɵfac: i0.ɵɵFactoryDeclaration<_CdkPrivateStyleLoader, never>;
15+
// (undocumented)
16+
static ɵprov: i0.ɵɵInjectableDeclaration<_CdkPrivateStyleLoader>;
17+
}
18+
19+
// (No @packageDocumentation comment for this package)
20+
21+
```

0 commit comments

Comments
 (0)