Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit a76475b

Browse files
authored
feat: simplify stackblitz examples using standalone (#1211)
Updates the Stackblitz examples to incorporate the standalone changes from the main repo and to reduce the amount of files we need to generate.
1 parent 3b4f58d commit a76475b

File tree

22 files changed

+57
-246
lines changed

22 files changed

+57
-246
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Input hashes for repository rule npm_translate_lock(name = "npm", pnpm_lock = "//:pnpm-lock.yaml").
22
# This file should be checked into version control along with the pnpm-lock.yaml file.
33
.npmrc=-1306950124
4-
pnpm-lock.yaml=-22712478
5-
yarn.lock=902473453
6-
package.json=861975370
4+
pnpm-lock.yaml=-1654047057
5+
yarn.lock=-726322771
6+
package.json=1829293188

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@angular/cdk-experimental": "^16.0.0",
3636
"@angular/common": "^16.0.0",
3737
"@angular/compiler": "^16.0.0",
38-
"@angular/components-examples": "https://github.com/angular/material2-docs-content.git#5d7786c0abcb58e0dfe2e4922f1cf58e6e55e61f",
38+
"@angular/components-examples": "https://github.com/angular/material2-docs-content.git#c29cd4e5f092902d2cca0f589c8de6c379e96789",
3939
"@angular/core": "^16.0.0",
4040
"@angular/forms": "^16.0.0",
4141
"@angular/google-maps": "^16.0.0",

pnpm-lock.yaml

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/pages/component-viewer/component-viewer.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {MatButtonModule} from '@angular/material/button';
77
import {MatIconModule} from '@angular/material/icon';
88
import {NgModule} from '@angular/core';
99
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
10-
import {ButtonExamplesModule} from '@angular/components-examples/material/button';
10+
import {ButtonOverviewExample} from '@angular/components-examples/material/button';
1111

1212
const docItemsId = 'button';
1313

@@ -56,7 +56,7 @@ describe('ComponentViewer', () => {
5656
MatButtonModule,
5757
MatIconModule,
5858
NoopAnimationsModule,
59-
ButtonExamplesModule,
59+
ButtonOverviewExample,
6060
],
6161
})
6262
class TestExampleModule { }

src/app/shared/example-viewer/example-viewer.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@
6565
</div>
6666

6767
<div class="docs-example-viewer-body" *ngIf="view !== 'snippet'">
68-
<ng-template *ngIf="_exampleComponentType && _exampleModuleFactory && !example?.includes('harness')"
69-
[ngComponentOutlet]="_exampleComponentType"
70-
[ngComponentOutletNgModuleFactory]="_exampleModuleFactory"></ng-template>
68+
<ng-template *ngIf="_exampleComponentType && !example?.includes('harness')"
69+
[ngComponentOutlet]="_exampleComponentType"></ng-template>
7170
<div *ngIf="example?.includes('harness')">
7271
This example contains tests. Open in Stackblitz to run the tests.
7372
</div>

src/app/shared/example-viewer/example-viewer.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {NoopAnimationsModule} from '@angular/platform-browser/animations';
1515
import {DocsAppTestingModule} from '../../testing/testing-module';
1616
import {DocViewerModule} from '../doc-viewer/doc-viewer-module';
1717
import {ExampleViewer} from './example-viewer';
18-
import {AutocompleteExamplesModule} from '@angular/components-examples/material/autocomplete';
18+
import {AutocompleteOverviewExample} from '@angular/components-examples/material/autocomplete';
1919
import {MatTabGroupHarness} from '@angular/material/tabs/testing';
2020
import {HarnessLoader} from '@angular/cdk/testing';
2121
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
@@ -243,7 +243,7 @@ describe('ExampleViewer', () => {
243243
CommonModule,
244244
ReactiveFormsModule,
245245
NoopAnimationsModule,
246-
AutocompleteExamplesModule,
246+
AutocompleteOverviewExample,
247247
],
248248
})
249249
class TestExampleModule {}

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import {
33
ElementRef,
44
HostBinding,
55
Input,
6-
NgModuleFactory,
76
OnInit,
87
QueryList,
98
Type,
109
ViewChildren,
11-
ɵNgModuleFactory,
1210
} from '@angular/core';
1311
import {MatSnackBar} from '@angular/material/snack-bar';
1412
import {Clipboard} from '@angular/cdk/clipboard';
@@ -66,9 +64,6 @@ export class ExampleViewer implements OnInit {
6664
/** Component type for the current example. */
6765
_exampleComponentType: Type<any> | null = null;
6866

69-
/** Module factory that declares the example component. */
70-
_exampleModuleFactory: NgModuleFactory<any> | null = null;
71-
7267
/** View of the example component. */
7368
@Input() view: Views | undefined;
7469

@@ -206,15 +201,10 @@ export class ExampleViewer implements OnInit {
206201
/** Loads the component and module factory for the currently selected example. */
207202
private async _loadExampleComponent() {
208203
if (this._example != null) {
209-
const {componentName, module} = EXAMPLE_COMPONENTS[this._example];
204+
const {componentName} = EXAMPLE_COMPONENTS[this._example];
210205
// Lazily loads the example package that contains the requested example.
211206
const moduleExports = await loadExample(this._example);
212207
this._exampleComponentType = moduleExports[componentName];
213-
// The components examples package is built with Ivy. This means that no factory files are
214-
// generated. To retrieve the factory of the AOT compiled module, we simply pass the module
215-
// class symbol to Ivy's module factory constructor. There is no equivalent for View Engine,
216-
// where factories are stored in separate files. Hence the API is currently Ivy-only.
217-
this._exampleModuleFactory = new ɵNgModuleFactory(moduleExports[module.name]);
218208

219209
// Since the data is loaded asynchronously, we can't count on the native behavior
220210
// that scrolls the element into view automatically. We do it ourselves while giving

src/app/shared/stack-blitz/stack-blitz-writer.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const testExampleBasePath = `/docs-content/examples-source/cdk/my-comp/${testExa
1010
const FAKE_DOCS: {[key: string]: string} = {
1111
/* eslint-disable @typescript-eslint/naming-convention */
1212
'/assets/stack-blitz/src/index.html': '<material-docs-example></material-docs-example>',
13-
'/assets/stack-blitz/src/app/app.module.ts':
13+
'/assets/stack-blitz/src/main.ts':
1414
`import {MaterialDocsExample} from './material-docs-example';`,
1515
/* eslint-enable @typescript-eslint/naming-convention */
1616
[`${testExampleBasePath}/test.ts`]: 'ExampleComponent',
@@ -50,7 +50,7 @@ describe('StackBlitzWriter', () => {
5050

5151
// Fake the example in the `EXAMPLE_COMPONENTS`. The stack blitz writer relies on
5252
// module information for the example in order to read the example sources from disk.
53-
EXAMPLE_COMPONENTS[testExampleId] = {module: {importSpecifier: 'cdk/my-comp'}} as LiveExample;
53+
EXAMPLE_COMPONENTS[testExampleId] = {importPath: 'cdk/my-comp'} as LiveExample;
5454
});
5555

5656
afterEach(() => {
@@ -109,11 +109,10 @@ describe('StackBlitzWriter', () => {
109109
const expectedFiles = jasmine.objectContaining({
110110
/* eslint-disable @typescript-eslint/naming-convention */
111111
'angular.json': 'fake',
112-
'src/main.ts': 'fake',
112+
'src/main.ts': `import {ExampleComponent} from './test';`,
113113
'src/test.ts': 'fake',
114114
'src/index.html': `<this-is-the-comp-name></this-is-the-comp-name>`,
115-
'src/app/app.module.ts': `import {ExampleComponent, AdditionalComp} from './test';`,
116-
'src/app/test.ts': `ExampleComponent
115+
'src/example/test.ts': `ExampleComponent
117116
118117
/** Copyright ${new Date().getFullYear()} Google LLC. All Rights Reserved.
119118
Use of this source code is governed by an MIT-style license that

src/app/shared/stack-blitz/stack-blitz-writer.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,9 @@ export const TEMPLATE_FILES = [
4242
'tsconfig.spec.json',
4343
'src/index.html',
4444
'src/main.ts',
45-
'src/material.module.ts',
46-
'src/polyfills.ts',
4745
'src/styles.scss',
4846
'src/test.ts',
4947
'src/theme.scss',
50-
'src/app/app.module.ts',
51-
'src/environments/environment.prod.ts',
52-
'src/environments/environment.ts',
5348
];
5449

5550
const PROJECT_TAGS = ['angular', 'material', 'cdk', 'web', 'example'];
@@ -130,7 +125,7 @@ export class StackBlitzWriter {
130125
const tasks: Promise<unknown>[] = [];
131126
const liveExample = EXAMPLE_COMPONENTS[exampleId];
132127
const exampleBaseContentPath =
133-
`${DOCS_CONTENT_PATH}/${liveExample.module.importSpecifier}/${exampleId}/`;
128+
`${DOCS_CONTENT_PATH}/${liveExample.importPath}/${exampleId}/`;
134129

135130
for (const relativeFilePath of TEMPLATE_FILES) {
136131
tasks.push(
@@ -147,7 +142,7 @@ export class StackBlitzWriter {
147142
// Note: Since we join with paths from the example data, we normalize
148143
// the final target path. This is necessary because StackBlitz does
149144
// not and paths like `./bla.ts` would result in a directory called `.`.
150-
const targetPath = normalizePath(`src/app/${relativeFilePath}`);
145+
const targetPath = normalizePath(`src/example/${relativeFilePath}`);
151146

152147
tasks.push(
153148
this._loadFile(exampleBaseContentPath + relativeFilePath)
@@ -209,26 +204,19 @@ export class StackBlitzWriter {
209204
.replace(/\${title}/g, data.description);
210205
} else if (fileName === '.stackblitzrc') {
211206
fileContent = fileContent.replace(/\${startCommand}/, isTest ? 'turbo test' : 'turbo start');
212-
} else if (fileName === 'src/app/app.module.ts') {
213-
const joinedComponentNames = data.componentNames.join(', ');
207+
} else if (fileName === 'src/main.ts') {
208+
const mainComponentName = data.componentNames[0];
209+
214210
// Replace the component name in `main.ts`.
215211
// Replace `import {MaterialDocsExample} from 'material-docs-example'`
216212
// will be replaced as `import {ButtonDemo} from './button-demo'`
217-
fileContent = fileContent.replace(/{MaterialDocsExample}/g, `{${joinedComponentNames}}`);
218-
219-
// Replace `declarations: [MaterialDocsExample]`
220-
// will be replaced as `declarations: [ButtonDemo]`
221-
fileContent = fileContent.replace(
222-
/declarations: \[MaterialDocsExample]/g,
223-
`declarations: [${joinedComponentNames}]`
224-
);
213+
fileContent = fileContent.replace(/{MaterialDocsExample}/g, `{${mainComponentName}}`);
225214

226-
// Replace `bootstrap: [MaterialDocsExample]`
227-
// will be replaced as `bootstrap: [ButtonDemo]`
228-
// This assumes the first component listed in the main component
215+
// Replace `bootstrapApplication(MaterialDocsExample,`
216+
// will be replaced as `bootstrapApplication(ButtonDemo,`
229217
fileContent = fileContent.replace(
230-
/bootstrap: \[MaterialDocsExample]/g,
231-
`bootstrap: [${data.componentNames[0]}]`
218+
/bootstrapApplication\(MaterialDocsExample,/g,
219+
`bootstrapApplication(${mainComponentName},`
232220
);
233221

234222
const dotIndex = data.indexFilename.lastIndexOf('.');

src/assets/stack-blitz/angular.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"outputPath": "dist/example-app",
2727
"index": "src/index.html",
2828
"main": "src/main.ts",
29-
"polyfills": "src/polyfills.ts",
29+
"polyfills": ["zone.js"],
3030
"tsConfig": "tsconfig.app.json",
3131
"inlineStyleLanguage": "scss",
3232
"assets": [
@@ -96,7 +96,7 @@
9696
"builder": "@angular-devkit/build-angular:karma",
9797
"options": {
9898
"main": "src/test.ts",
99-
"polyfills": "src/polyfills.ts",
99+
"polyfills": ["zone.js", "zone.js/testing"],
100100
"tsConfig": "tsconfig.spec.json",
101101
"karmaConfig": "karma.conf.js",
102102
"inlineStyleLanguage": "scss",

0 commit comments

Comments
 (0)