Skip to content

Commit 0590af6

Browse files
authored
build: do not ship load-example code to npm package (#18547) (#18554)
We don't want to ship the `load-example` code that is internal to our dev-app and e2e-app to the `@angular/components-examples` package. The logic for loading examples lazily can never be generic enough to work in all environments. i.e. in Webpack, such dynamic imports which are not statically analyzable cannot be resolved. This could result in errors being thrown by Webpack about critical dependencies.
1 parent c07adea commit 0590af6

File tree

13 files changed

+40
-24
lines changed

13 files changed

+40
-24
lines changed

src/components-examples/BUILD.bazel

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ load("//tools:defaults.bzl", "ng_module", "ng_package")
44
load("//tools/highlight-files:index.bzl", "highlight_files")
55
load("//tools/package-docs-content:index.bzl", "package_docs_content")
66

7-
EXAMPLE_PACKAGES = [
7+
ALL_EXAMPLES = [
88
# TODO(devversion): try to have for each entry-point a bazel package so that
99
# we can automate this using the "package.bzl" variables. Currently generated
1010
# with "bazel query 'kind("ng_module", //src/components-examples/...:*)' --output="label"
@@ -61,12 +61,12 @@ ng_module(
6161
name = "components-examples",
6262
srcs = glob(["*.ts"]) + [":example-module.ts"],
6363
module_name = "@angular/components-examples",
64-
deps = EXAMPLE_PACKAGES,
64+
deps = ALL_EXAMPLES,
6565
)
6666

6767
filegroup(
6868
name = "example-source-files",
69-
srcs = ["%s:source-files" % pkg for pkg in EXAMPLE_PACKAGES],
69+
srcs = ["%s:source-files" % pkg for pkg in ALL_EXAMPLES],
7070
)
7171

7272
highlight_files(
@@ -106,7 +106,7 @@ ng_package(
106106
data = [":docs-content"],
107107
entry_point = ":public-api.ts",
108108
tags = ["docs-package"],
109-
deps = [":components-examples"] + EXAMPLE_PACKAGES,
109+
deps = [":components-examples"] + ALL_EXAMPLES,
110110
)
111111

112112
genrule(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("//tools:defaults.bzl", "ts_library")
4+
5+
ts_library(
6+
name = "private",
7+
srcs = glob(["*.ts"]),
8+
module_name = "@angular/components-examples/private",
9+
deps = [
10+
"//src/components-examples",
11+
"@npm//@angular/core",
12+
],
13+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './load-example';

src/components-examples/load-example.ts renamed to src/components-examples/private/load-example.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
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-
91
import {ComponentFactory, Injector, NgModuleFactory, Type} from '@angular/core';
10-
import {EXAMPLE_COMPONENTS} from './example-module';
2+
import {EXAMPLE_COMPONENTS} from '../example-module';
113

124
/** Asynchronously loads the specified example and returns its component factory. */
135
export async function loadExampleFactory(name: string, injector: Injector)
146
: Promise<ComponentFactory<any>> {
157
const {componentName, module} = EXAMPLE_COMPONENTS[name];
8+
const importSpecifier = `@angular/components-examples/${module.importSpecifier}`;
169
// TODO(devversion): remove the NgFactory import when the `--config=view-engine` switch is gone.
1710
const [moduleFactoryExports, moduleExports] = await Promise.all([
18-
import(module.importSpecifier + '/index.ngfactory'),
19-
import(module.importSpecifier)
11+
import(importSpecifier + '/index.ngfactory'),
12+
import(importSpecifier)
2013
]);
2114
const moduleFactory: NgModuleFactory<any> = moduleFactoryExports[`${module.name}NgFactory`];
2215
const componentType: Type<any> = moduleExports[componentName];

src/components-examples/public-api.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export * from './example-data';
2-
export * from './load-example';
32

43
// The example-module file will be auto-generated. As soon as the
54
// examples are being compiled, the module file will be generated.

src/dev-app/example/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ng_module(
88
deps = [
99
"//src/cdk/coercion",
1010
"//src/components-examples",
11+
"//src/components-examples/private",
1112
"//src/material/expansion",
1213
"@npm//@angular/elements",
1314
],

src/dev-app/example/example.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
*/
88

99
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
10-
import {EXAMPLE_COMPONENTS, loadExampleFactory} from '@angular/components-examples';
10+
import {EXAMPLE_COMPONENTS} from '@angular/components-examples';
11+
import {loadExampleFactory} from '@angular/components-examples/private';
1112
import {Component, Injector, Input, OnInit, ViewContainerRef} from '@angular/core';
1213

1314
@Component({

src/dev-app/system-config-tmpl.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ configureEntryPoint('components-examples');
3434
configureEntryPoint('material');
3535
configureEntryPoint('material-experimental');
3636
configureEntryPoint('material-moment-adapter');
37+
configureEntryPoint('google-maps');
38+
configureEntryPoint('youtube-player');
3739

3840
// Configure all secondary entry-points.
3941
CDK_PACKAGES.forEach(function(pkgName) {
@@ -48,8 +50,9 @@ MATERIAL_EXPERIMENTAL_PACKAGES.forEach(function(pkgName) {
4850
MATERIAL_PACKAGES.forEach(function(pkgName) {
4951
configureEntryPoint('material', pkgName);
5052
});
51-
configureEntryPoint('google-maps');
52-
configureEntryPoint('youtube-player');
53+
54+
// Private secondary entry-points.
55+
configureEntryPoint('components-examples', 'private');
5356

5457
/** Configures the specified package, its entry-point and its examples. */
5558
function configureEntryPoint(pkgName, entryPoint) {

src/e2e-app/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ ng_module(
3131
"//src/cdk/overlay",
3232
"//src/cdk/scrolling",
3333
"//src/cdk/testing/tests:test_components",
34-
"//src/components-examples",
34+
"//src/components-examples/private",
3535
"//src/material-experimental/mdc-button",
3636
"//src/material-experimental/mdc-card",
3737
"//src/material-experimental/mdc-checkbox",

src/e2e-app/example-viewer/example-viewer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {loadExampleFactory} from '@angular/components-examples';
9+
import {loadExampleFactory} from '@angular/components-examples/private';
1010
import {Component, Injector, Input, OnInit, ViewContainerRef} from '@angular/core';
1111

1212
/** Loads an example component from `@angular/components-examples` */

src/e2e-app/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"@angular/cdk-experimental/*": ["../cdk-experimental/*"],
1313
"@angular/cdk-experimental": ["../cdk-experimental/"],
1414
"@angular/material-moment-adapter": ["../material-moment-adapter/"],
15-
"@angular/components-examples": ["../components-examples/"]
15+
"@angular/components-examples": ["../components-examples/"],
16+
"@angular/components-examples/*": ["../components-examples/*"]
1617
}
1718
},
1819
"include": ["./**/*.ts"]

tools/example-module/example-module.template

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ export interface LiveExample {
1919
export interface NgModuleInfo {
2020
/** Name of the NgModule. */
2121
name: string;
22-
/** Import specifier that resolves to this NgModule. */
22+
/**
23+
* Import specifier that resolves to this module. The specifier is not scoped to
24+
* `@angular/components-examples` because it's up to the consumer how the module is
25+
* imported. For example, in the docs app, modules are lazily imported from `fesm2015/`.
26+
*/
2327
importSpecifier: string;
2428
}
2529

tools/example-module/generate-example-module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function inlineExampleModuleTemplate(parsedData: AnalyzedExamples): string {
4444
additionalComponents: data.additionalComponents,
4545
module: {
4646
name: data.module.name,
47-
importSpecifier: `@angular/components-examples/${data.module.packagePath}`,
47+
importSpecifier: data.module.packagePath,
4848
},
4949
};
5050

0 commit comments

Comments
 (0)