@storybook/angular 9.0.0-beta.6 → 9.0.0-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/builders/build-storybook/index.mjs +78 -0
- package/dist/builders/build-storybook/index.spec.mjs +187 -0
- package/dist/builders/start-storybook/index.mjs +99 -0
- package/dist/builders/start-storybook/index.spec.mjs +186 -0
- package/dist/builders/utils/error-handler.mjs +33 -0
- package/dist/builders/utils/run-compodoc.mjs +31 -0
- package/dist/builders/utils/run-compodoc.spec.mjs +74 -0
- package/dist/builders/utils/standalone-options.mjs +1 -0
- package/dist/client/angular-beta/AbstractRenderer.mjs +164 -0
- package/dist/client/angular-beta/CanvasRenderer.mjs +9 -0
- package/dist/client/angular-beta/ComputesTemplateFromComponent.mjs +154 -0
- package/dist/client/angular-beta/ComputesTemplateFromComponent.test.mjs +728 -0
- package/dist/client/angular-beta/DocsRenderer.mjs +35 -0
- package/dist/client/angular-beta/RendererFactory.mjs +50 -0
- package/dist/client/angular-beta/RendererFactory.test.mjs +233 -0
- package/dist/client/angular-beta/StorybookModule.mjs +23 -0
- package/dist/client/angular-beta/StorybookModule.test.mjs +319 -0
- package/dist/client/angular-beta/StorybookProvider.mjs +22 -0
- package/dist/client/angular-beta/StorybookWrapperComponent.mjs +123 -0
- package/dist/client/angular-beta/__testfixtures__/input.component.mjs +73 -0
- package/dist/client/angular-beta/__testfixtures__/test.module.mjs +17 -0
- package/dist/client/angular-beta/utils/BootstrapQueue.mjs +49 -0
- package/dist/client/angular-beta/utils/BootstrapQueue.test.mjs +162 -0
- package/dist/client/angular-beta/utils/NgComponentAnalyzer.mjs +84 -0
- package/dist/client/angular-beta/utils/NgComponentAnalyzer.test.mjs +386 -0
- package/dist/client/angular-beta/utils/NgModulesAnalyzer.mjs +37 -0
- package/dist/client/angular-beta/utils/NgModulesAnalyzer.test.mjs +22 -0
- package/dist/client/angular-beta/utils/PropertyExtractor.mjs +158 -0
- package/dist/client/angular-beta/utils/PropertyExtractor.test.mjs +175 -0
- package/dist/client/angular-beta/utils/StoryUID.mjs +38 -0
- package/dist/client/argsToTemplate.mjs +55 -0
- package/dist/client/argsToTemplate.test.mjs +100 -0
- package/dist/client/config.mjs +4 -0
- package/dist/client/decorateStory.mjs +45 -0
- package/dist/client/decorateStory.test.mjs +301 -0
- package/dist/client/decorators.mjs +63 -0
- package/dist/client/decorators.test.mjs +157 -0
- package/dist/client/docs/__testfixtures__/doc-button/input.mjs +201 -0
- package/dist/client/docs/angular-properties.test.mjs +34 -0
- package/dist/client/docs/compodoc.mjs +244 -0
- package/dist/client/docs/compodoc.test.mjs +130 -0
- package/dist/client/docs/config.mjs +16 -0
- package/dist/client/docs/index.mjs +1 -0
- package/dist/client/docs/sourceDecorator.mjs +48 -0
- package/dist/client/docs/types.mjs +1 -0
- package/dist/client/globals.mjs +31 -0
- package/dist/client/index.mjs +9 -0
- package/dist/client/portable-stories.mjs +26 -0
- package/dist/client/preview-prod.mjs +2 -0
- package/dist/client/public-types.mjs +1 -0
- package/dist/client/render.mjs +14 -0
- package/dist/client/types.mjs +1 -0
- package/dist/node/index.mjs +3 -0
- package/dist/server/__mocks-ng-workspace__/minimal-config/src/main.mjs +2 -0
- package/dist/server/__mocks-ng-workspace__/some-config/src/main.mjs +2 -0
- package/dist/server/__mocks-ng-workspace__/with-angularBrowserTarget/src/main.mjs +2 -0
- package/dist/server/__mocks-ng-workspace__/with-lib/projects/pattern-lib/src/main.mjs +2 -0
- package/dist/server/__mocks-ng-workspace__/with-nx/src/main.mjs +2 -0
- package/dist/server/__mocks-ng-workspace__/with-nx-workspace/src/main.mjs +2 -0
- package/dist/server/__mocks-ng-workspace__/with-options-styles/src/main.mjs +2 -0
- package/dist/server/__mocks-ng-workspace__/without-projects-entry/projects/pattern-lib/src/main.mjs +2 -0
- package/dist/server/__mocks-ng-workspace__/without-tsConfig/src/main.mjs +2 -0
- package/dist/server/angular-cli-webpack.mjs +80 -0
- package/dist/server/framework-preset-angular-cli.mjs +81 -0
- package/dist/server/framework-preset-angular-docs.mjs +6 -0
- package/dist/server/framework-preset-angular-ivy.mjs +56 -0
- package/dist/server/plugins/storybook-normalize-angular-entry-plugin.mjs +52 -0
- package/dist/server/preset-options.mjs +1 -0
- package/dist/server/utils/filter-out-styling-rules.mjs +13 -0
- package/dist/server/utils/module-is-available.mjs +9 -0
- package/package.json +4 -4
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
import { Component, ComponentFactoryResolver, Directive, EventEmitter, HostBinding, Injectable, Input, Output, Pipe, input, output, } from '@angular/core';
|
|
12
|
+
import { TestBed } from '@angular/core/testing';
|
|
13
|
+
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
|
|
14
|
+
import { describe, expect, it } from 'vitest';
|
|
15
|
+
import { getComponentInputsOutputs, isComponent, isDeclarable, getComponentDecoratorMetadata, isStandaloneComponent, } from './NgComponentAnalyzer';
|
|
16
|
+
describe('getComponentInputsOutputs', () => {
|
|
17
|
+
it('should return empty if no I/O found', () => {
|
|
18
|
+
let FooComponent = class FooComponent {
|
|
19
|
+
};
|
|
20
|
+
FooComponent = __decorate([
|
|
21
|
+
Component({
|
|
22
|
+
standalone: false,
|
|
23
|
+
})
|
|
24
|
+
], FooComponent);
|
|
25
|
+
expect(getComponentInputsOutputs(FooComponent)).toEqual({
|
|
26
|
+
inputs: [],
|
|
27
|
+
outputs: [],
|
|
28
|
+
});
|
|
29
|
+
class BarComponent {
|
|
30
|
+
}
|
|
31
|
+
expect(getComponentInputsOutputs(BarComponent)).toEqual({
|
|
32
|
+
inputs: [],
|
|
33
|
+
outputs: [],
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
it('should return I/O', () => {
|
|
37
|
+
let FooComponent = class FooComponent {
|
|
38
|
+
constructor() {
|
|
39
|
+
this.signalInput = input();
|
|
40
|
+
this.signalInputAliased = input('signalInputAliased', {
|
|
41
|
+
alias: 'signalInputAliasedAlias',
|
|
42
|
+
});
|
|
43
|
+
this.output = new EventEmitter();
|
|
44
|
+
this.outputWithBindingPropertyName = new EventEmitter();
|
|
45
|
+
this.signalOutput = output();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
__decorate([
|
|
49
|
+
Input(),
|
|
50
|
+
__metadata("design:type", String)
|
|
51
|
+
], FooComponent.prototype, "input", void 0);
|
|
52
|
+
__decorate([
|
|
53
|
+
Input('inputPropertyName'),
|
|
54
|
+
__metadata("design:type", String)
|
|
55
|
+
], FooComponent.prototype, "inputWithBindingPropertyName", void 0);
|
|
56
|
+
__decorate([
|
|
57
|
+
Output(),
|
|
58
|
+
__metadata("design:type", Object)
|
|
59
|
+
], FooComponent.prototype, "output", void 0);
|
|
60
|
+
__decorate([
|
|
61
|
+
Output('outputPropertyName'),
|
|
62
|
+
__metadata("design:type", Object)
|
|
63
|
+
], FooComponent.prototype, "outputWithBindingPropertyName", void 0);
|
|
64
|
+
FooComponent = __decorate([
|
|
65
|
+
Component({
|
|
66
|
+
template: '',
|
|
67
|
+
inputs: ['inputInComponentMetadata'],
|
|
68
|
+
outputs: ['outputInComponentMetadata'],
|
|
69
|
+
standalone: false,
|
|
70
|
+
})
|
|
71
|
+
], FooComponent);
|
|
72
|
+
const fooComponentFactory = resolveComponentFactory(FooComponent);
|
|
73
|
+
const { inputs, outputs } = getComponentInputsOutputs(FooComponent);
|
|
74
|
+
expect({ inputs, outputs }).toEqual({
|
|
75
|
+
inputs: [
|
|
76
|
+
{ propName: 'inputInComponentMetadata', templateName: 'inputInComponentMetadata' },
|
|
77
|
+
{ propName: 'input', templateName: 'input' },
|
|
78
|
+
{ propName: 'inputWithBindingPropertyName', templateName: 'inputPropertyName' },
|
|
79
|
+
],
|
|
80
|
+
outputs: [
|
|
81
|
+
{ propName: 'outputInComponentMetadata', templateName: 'outputInComponentMetadata' },
|
|
82
|
+
{ propName: 'output', templateName: 'output' },
|
|
83
|
+
{ propName: 'outputWithBindingPropertyName', templateName: 'outputPropertyName' },
|
|
84
|
+
],
|
|
85
|
+
});
|
|
86
|
+
expect(sortByPropName(inputs)).toEqual(sortByPropName(fooComponentFactory.inputs.map(({ isSignal, ...rest }) => rest)));
|
|
87
|
+
expect(sortByPropName(outputs)).toEqual(sortByPropName(fooComponentFactory.outputs));
|
|
88
|
+
});
|
|
89
|
+
it("should return I/O when some of component metadata has the same name as one of component's properties", () => {
|
|
90
|
+
let FooComponent = class FooComponent {
|
|
91
|
+
constructor() {
|
|
92
|
+
this.output = new EventEmitter();
|
|
93
|
+
this.outputWithBindingPropertyName = new EventEmitter();
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
__decorate([
|
|
97
|
+
Input(),
|
|
98
|
+
__metadata("design:type", String)
|
|
99
|
+
], FooComponent.prototype, "input", void 0);
|
|
100
|
+
__decorate([
|
|
101
|
+
Input('inputPropertyName'),
|
|
102
|
+
__metadata("design:type", String)
|
|
103
|
+
], FooComponent.prototype, "inputWithBindingPropertyName", void 0);
|
|
104
|
+
__decorate([
|
|
105
|
+
Output(),
|
|
106
|
+
__metadata("design:type", Object)
|
|
107
|
+
], FooComponent.prototype, "output", void 0);
|
|
108
|
+
__decorate([
|
|
109
|
+
Output('outputPropertyName'),
|
|
110
|
+
__metadata("design:type", Object)
|
|
111
|
+
], FooComponent.prototype, "outputWithBindingPropertyName", void 0);
|
|
112
|
+
FooComponent = __decorate([
|
|
113
|
+
Component({
|
|
114
|
+
template: '',
|
|
115
|
+
inputs: ['input', 'inputWithBindingPropertyName'],
|
|
116
|
+
outputs: ['outputWithBindingPropertyName'],
|
|
117
|
+
standalone: false,
|
|
118
|
+
})
|
|
119
|
+
], FooComponent);
|
|
120
|
+
const fooComponentFactory = resolveComponentFactory(FooComponent);
|
|
121
|
+
const { inputs, outputs } = getComponentInputsOutputs(FooComponent);
|
|
122
|
+
expect(sortByPropName(inputs)).toEqual(sortByPropName(fooComponentFactory.inputs.map(({ isSignal, ...rest }) => rest)));
|
|
123
|
+
expect(sortByPropName(outputs)).toEqual(sortByPropName(fooComponentFactory.outputs));
|
|
124
|
+
});
|
|
125
|
+
it('should return I/O in the presence of multiple decorators', () => {
|
|
126
|
+
let FooComponent = class FooComponent {
|
|
127
|
+
};
|
|
128
|
+
__decorate([
|
|
129
|
+
Input(),
|
|
130
|
+
HostBinding('class.preceeding-first'),
|
|
131
|
+
__metadata("design:type", String)
|
|
132
|
+
], FooComponent.prototype, "inputPreceedingHostBinding", void 0);
|
|
133
|
+
__decorate([
|
|
134
|
+
HostBinding('class.following-binding'),
|
|
135
|
+
Input(),
|
|
136
|
+
__metadata("design:type", String)
|
|
137
|
+
], FooComponent.prototype, "inputFollowingHostBinding", void 0);
|
|
138
|
+
FooComponent = __decorate([
|
|
139
|
+
Component({
|
|
140
|
+
template: '',
|
|
141
|
+
standalone: false,
|
|
142
|
+
})
|
|
143
|
+
], FooComponent);
|
|
144
|
+
const fooComponentFactory = resolveComponentFactory(FooComponent);
|
|
145
|
+
const { inputs, outputs } = getComponentInputsOutputs(FooComponent);
|
|
146
|
+
expect({ inputs, outputs }).toEqual({
|
|
147
|
+
inputs: [
|
|
148
|
+
{ propName: 'inputPreceedingHostBinding', templateName: 'inputPreceedingHostBinding' },
|
|
149
|
+
{ propName: 'inputFollowingHostBinding', templateName: 'inputFollowingHostBinding' },
|
|
150
|
+
],
|
|
151
|
+
outputs: [],
|
|
152
|
+
});
|
|
153
|
+
expect(sortByPropName(inputs)).toEqual(sortByPropName(fooComponentFactory.inputs.map(({ isSignal, ...rest }) => rest)));
|
|
154
|
+
expect(sortByPropName(outputs)).toEqual(sortByPropName(fooComponentFactory.outputs));
|
|
155
|
+
});
|
|
156
|
+
it('should return I/O with extending classes', () => {
|
|
157
|
+
let BarComponent = class BarComponent {
|
|
158
|
+
};
|
|
159
|
+
__decorate([
|
|
160
|
+
Input(),
|
|
161
|
+
__metadata("design:type", String)
|
|
162
|
+
], BarComponent.prototype, "a", void 0);
|
|
163
|
+
__decorate([
|
|
164
|
+
Input(),
|
|
165
|
+
__metadata("design:type", String)
|
|
166
|
+
], BarComponent.prototype, "b", void 0);
|
|
167
|
+
BarComponent = __decorate([
|
|
168
|
+
Component({
|
|
169
|
+
template: '',
|
|
170
|
+
standalone: false,
|
|
171
|
+
})
|
|
172
|
+
], BarComponent);
|
|
173
|
+
let FooComponent = class FooComponent extends BarComponent {
|
|
174
|
+
};
|
|
175
|
+
__decorate([
|
|
176
|
+
Input(),
|
|
177
|
+
__metadata("design:type", String)
|
|
178
|
+
], FooComponent.prototype, "b", void 0);
|
|
179
|
+
__decorate([
|
|
180
|
+
Input(),
|
|
181
|
+
__metadata("design:type", String)
|
|
182
|
+
], FooComponent.prototype, "c", void 0);
|
|
183
|
+
FooComponent = __decorate([
|
|
184
|
+
Component({
|
|
185
|
+
template: '',
|
|
186
|
+
standalone: false,
|
|
187
|
+
})
|
|
188
|
+
], FooComponent);
|
|
189
|
+
const fooComponentFactory = resolveComponentFactory(FooComponent);
|
|
190
|
+
const { inputs, outputs } = getComponentInputsOutputs(FooComponent);
|
|
191
|
+
expect({ inputs, outputs }).toEqual({
|
|
192
|
+
inputs: [
|
|
193
|
+
{ propName: 'a', templateName: 'a' },
|
|
194
|
+
{ propName: 'b', templateName: 'b' },
|
|
195
|
+
{ propName: 'c', templateName: 'c' },
|
|
196
|
+
],
|
|
197
|
+
outputs: [],
|
|
198
|
+
});
|
|
199
|
+
expect(sortByPropName(inputs)).toEqual(sortByPropName(fooComponentFactory.inputs.map(({ isSignal, ...rest }) => rest)));
|
|
200
|
+
expect(sortByPropName(outputs)).toEqual(sortByPropName(fooComponentFactory.outputs));
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
describe('isDeclarable', () => {
|
|
204
|
+
it('should return true with a Component', () => {
|
|
205
|
+
let FooComponent = class FooComponent {
|
|
206
|
+
};
|
|
207
|
+
FooComponent = __decorate([
|
|
208
|
+
Component({})
|
|
209
|
+
], FooComponent);
|
|
210
|
+
expect(isDeclarable(FooComponent)).toEqual(true);
|
|
211
|
+
});
|
|
212
|
+
it('should return true with a Directive', () => {
|
|
213
|
+
let FooDirective = class FooDirective {
|
|
214
|
+
};
|
|
215
|
+
FooDirective = __decorate([
|
|
216
|
+
Directive({})
|
|
217
|
+
], FooDirective);
|
|
218
|
+
expect(isDeclarable(FooDirective)).toEqual(true);
|
|
219
|
+
});
|
|
220
|
+
it('should return true with a Pipe', () => {
|
|
221
|
+
let FooPipe = class FooPipe {
|
|
222
|
+
};
|
|
223
|
+
FooPipe = __decorate([
|
|
224
|
+
Pipe({ name: 'pipe' })
|
|
225
|
+
], FooPipe);
|
|
226
|
+
expect(isDeclarable(FooPipe)).toEqual(true);
|
|
227
|
+
});
|
|
228
|
+
it('should return false with simple class', () => {
|
|
229
|
+
class FooPipe {
|
|
230
|
+
}
|
|
231
|
+
expect(isDeclarable(FooPipe)).toEqual(false);
|
|
232
|
+
});
|
|
233
|
+
it('should return false with Injectable', () => {
|
|
234
|
+
let FooInjectable = class FooInjectable {
|
|
235
|
+
};
|
|
236
|
+
FooInjectable = __decorate([
|
|
237
|
+
Injectable()
|
|
238
|
+
], FooInjectable);
|
|
239
|
+
expect(isDeclarable(FooInjectable)).toEqual(false);
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
describe('isComponent', () => {
|
|
243
|
+
it('should return true with a Component', () => {
|
|
244
|
+
let FooComponent = class FooComponent {
|
|
245
|
+
};
|
|
246
|
+
FooComponent = __decorate([
|
|
247
|
+
Component({})
|
|
248
|
+
], FooComponent);
|
|
249
|
+
expect(isComponent(FooComponent)).toEqual(true);
|
|
250
|
+
});
|
|
251
|
+
it('should return false with simple class', () => {
|
|
252
|
+
class FooPipe {
|
|
253
|
+
}
|
|
254
|
+
expect(isComponent(FooPipe)).toEqual(false);
|
|
255
|
+
});
|
|
256
|
+
it('should return false with Directive', () => {
|
|
257
|
+
let FooDirective = class FooDirective {
|
|
258
|
+
};
|
|
259
|
+
FooDirective = __decorate([
|
|
260
|
+
Directive()
|
|
261
|
+
], FooDirective);
|
|
262
|
+
expect(isComponent(FooDirective)).toEqual(false);
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
describe('isStandaloneComponent', () => {
|
|
266
|
+
it('should return true with a Component with "standalone: true"', () => {
|
|
267
|
+
let FooComponent = class FooComponent {
|
|
268
|
+
};
|
|
269
|
+
FooComponent = __decorate([
|
|
270
|
+
Component({ standalone: true })
|
|
271
|
+
], FooComponent);
|
|
272
|
+
expect(isStandaloneComponent(FooComponent)).toEqual(true);
|
|
273
|
+
});
|
|
274
|
+
it('should return false with a Component with "standalone: false"', () => {
|
|
275
|
+
let FooComponent = class FooComponent {
|
|
276
|
+
};
|
|
277
|
+
FooComponent = __decorate([
|
|
278
|
+
Component({ standalone: false })
|
|
279
|
+
], FooComponent);
|
|
280
|
+
expect(isStandaloneComponent(FooComponent)).toEqual(false);
|
|
281
|
+
});
|
|
282
|
+
it('should return false with a Component without the "standalone" property', () => {
|
|
283
|
+
let FooComponent = class FooComponent {
|
|
284
|
+
};
|
|
285
|
+
FooComponent = __decorate([
|
|
286
|
+
Component({})
|
|
287
|
+
], FooComponent);
|
|
288
|
+
expect(isStandaloneComponent(FooComponent)).toEqual(false);
|
|
289
|
+
});
|
|
290
|
+
it('should return false with simple class', () => {
|
|
291
|
+
class FooPipe {
|
|
292
|
+
}
|
|
293
|
+
expect(isStandaloneComponent(FooPipe)).toEqual(false);
|
|
294
|
+
});
|
|
295
|
+
it('should return true with a Directive with "standalone: true"', () => {
|
|
296
|
+
let FooDirective = class FooDirective {
|
|
297
|
+
};
|
|
298
|
+
FooDirective = __decorate([
|
|
299
|
+
Directive({ standalone: true })
|
|
300
|
+
], FooDirective);
|
|
301
|
+
expect(isStandaloneComponent(FooDirective)).toEqual(true);
|
|
302
|
+
});
|
|
303
|
+
it('should return false with a Directive with "standalone: false"', () => {
|
|
304
|
+
let FooDirective = class FooDirective {
|
|
305
|
+
};
|
|
306
|
+
FooDirective = __decorate([
|
|
307
|
+
Directive({ standalone: false })
|
|
308
|
+
], FooDirective);
|
|
309
|
+
expect(isStandaloneComponent(FooDirective)).toEqual(false);
|
|
310
|
+
});
|
|
311
|
+
it('should return false with Directive without the "standalone" property', () => {
|
|
312
|
+
let FooDirective = class FooDirective {
|
|
313
|
+
};
|
|
314
|
+
FooDirective = __decorate([
|
|
315
|
+
Directive()
|
|
316
|
+
], FooDirective);
|
|
317
|
+
expect(isStandaloneComponent(FooDirective)).toEqual(false);
|
|
318
|
+
});
|
|
319
|
+
it('should return true with a Pipe with "standalone: true"', () => {
|
|
320
|
+
let FooPipe = class FooPipe {
|
|
321
|
+
};
|
|
322
|
+
FooPipe = __decorate([
|
|
323
|
+
Pipe({ name: 'FooPipe', standalone: true })
|
|
324
|
+
], FooPipe);
|
|
325
|
+
expect(isStandaloneComponent(FooPipe)).toEqual(true);
|
|
326
|
+
});
|
|
327
|
+
it('should return false with a Pipe with "standalone: false"', () => {
|
|
328
|
+
let FooPipe = class FooPipe {
|
|
329
|
+
};
|
|
330
|
+
FooPipe = __decorate([
|
|
331
|
+
Pipe({ name: 'FooPipe', standalone: false })
|
|
332
|
+
], FooPipe);
|
|
333
|
+
expect(isStandaloneComponent(FooPipe)).toEqual(false);
|
|
334
|
+
});
|
|
335
|
+
it('should return false with Pipe without the "standalone" property', () => {
|
|
336
|
+
let FooPipe = class FooPipe {
|
|
337
|
+
};
|
|
338
|
+
FooPipe = __decorate([
|
|
339
|
+
Pipe({
|
|
340
|
+
name: 'fooPipe',
|
|
341
|
+
})
|
|
342
|
+
], FooPipe);
|
|
343
|
+
expect(isStandaloneComponent(FooPipe)).toEqual(false);
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
describe('getComponentDecoratorMetadata', () => {
|
|
347
|
+
it('should return Component with a Component', () => {
|
|
348
|
+
let FooComponent = class FooComponent {
|
|
349
|
+
};
|
|
350
|
+
FooComponent = __decorate([
|
|
351
|
+
Component({ selector: 'foo' })
|
|
352
|
+
], FooComponent);
|
|
353
|
+
expect(getComponentDecoratorMetadata(FooComponent)).toBeInstanceOf(Component);
|
|
354
|
+
expect(getComponentDecoratorMetadata(FooComponent)).toEqual({
|
|
355
|
+
changeDetection: 1,
|
|
356
|
+
selector: 'foo',
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
it('should return Component with extending classes', () => {
|
|
360
|
+
let BarComponent = class BarComponent {
|
|
361
|
+
};
|
|
362
|
+
BarComponent = __decorate([
|
|
363
|
+
Component({ selector: 'bar' })
|
|
364
|
+
], BarComponent);
|
|
365
|
+
let FooComponent = class FooComponent extends BarComponent {
|
|
366
|
+
};
|
|
367
|
+
FooComponent = __decorate([
|
|
368
|
+
Component({ selector: 'foo' })
|
|
369
|
+
], FooComponent);
|
|
370
|
+
expect(getComponentDecoratorMetadata(FooComponent)).toBeInstanceOf(Component);
|
|
371
|
+
expect(getComponentDecoratorMetadata(FooComponent)).toEqual({
|
|
372
|
+
changeDetection: 1,
|
|
373
|
+
selector: 'foo',
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
function sortByPropName(array) {
|
|
378
|
+
return array.sort((a, b) => a.propName.localeCompare(b.propName));
|
|
379
|
+
}
|
|
380
|
+
function resolveComponentFactory(component) {
|
|
381
|
+
TestBed.configureTestingModule({
|
|
382
|
+
declarations: [component],
|
|
383
|
+
}).overrideModule(BrowserDynamicTestingModule, {});
|
|
384
|
+
const componentFactoryResolver = TestBed.inject(ComponentFactoryResolver);
|
|
385
|
+
return componentFactoryResolver.resolveComponentFactory(component);
|
|
386
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { NgModule, ɵReflectionCapabilities as ReflectionCapabilities } from '@angular/core';
|
|
2
|
+
const reflectionCapabilities = new ReflectionCapabilities();
|
|
3
|
+
/**
|
|
4
|
+
* Avoid component redeclaration
|
|
5
|
+
*
|
|
6
|
+
* Checks recursively if the component has already been declared in all import Module
|
|
7
|
+
*/
|
|
8
|
+
export const isComponentAlreadyDeclared = (componentToFind, moduleDeclarations, moduleImports) => {
|
|
9
|
+
if (moduleDeclarations &&
|
|
10
|
+
moduleDeclarations.flat().some((declaration) => declaration === componentToFind)) {
|
|
11
|
+
// Found component in declarations array
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
if (!moduleImports) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
return moduleImports.flat().some((importItem) => {
|
|
18
|
+
const extractedNgModuleMetadata = extractNgModuleMetadata(importItem);
|
|
19
|
+
if (!extractedNgModuleMetadata) {
|
|
20
|
+
// Not an NgModule
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return isComponentAlreadyDeclared(componentToFind, extractedNgModuleMetadata.declarations, extractedNgModuleMetadata.imports);
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
const extractNgModuleMetadata = (importItem) => {
|
|
27
|
+
const target = importItem && importItem.ngModule ? importItem.ngModule : importItem;
|
|
28
|
+
const decorators = reflectionCapabilities.annotations(target);
|
|
29
|
+
if (!decorators || decorators.length === 0) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const ngModuleDecorator = decorators.find((decorator) => decorator instanceof NgModule);
|
|
33
|
+
if (!ngModuleDecorator) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
return ngModuleDecorator;
|
|
37
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Component, NgModule } from '@angular/core';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { isComponentAlreadyDeclared } from './NgModulesAnalyzer';
|
|
4
|
+
const FooComponent = Component({})(class {
|
|
5
|
+
});
|
|
6
|
+
const BarComponent = Component({})(class {
|
|
7
|
+
});
|
|
8
|
+
const BetaModule = NgModule({ declarations: [FooComponent] })(class {
|
|
9
|
+
});
|
|
10
|
+
const AlphaModule = NgModule({ imports: [BetaModule] })(class {
|
|
11
|
+
});
|
|
12
|
+
describe('isComponentAlreadyDeclaredInModules', () => {
|
|
13
|
+
it('should return true when the component is already declared in one of modules', () => {
|
|
14
|
+
expect(isComponentAlreadyDeclared(FooComponent, [], [AlphaModule])).toEqual(true);
|
|
15
|
+
});
|
|
16
|
+
it('should return true if the component is in moduleDeclarations', () => {
|
|
17
|
+
expect(isComponentAlreadyDeclared(BarComponent, [BarComponent], [AlphaModule])).toEqual(true);
|
|
18
|
+
});
|
|
19
|
+
it('should return false if the component is not declared', () => {
|
|
20
|
+
expect(isComponentAlreadyDeclared(BarComponent, [], [AlphaModule])).toEqual(false);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { CommonModule } from '@angular/common';
|
|
3
|
+
import { Component, Directive, Injectable, InjectionToken, Input, Output, Pipe, ɵReflectionCapabilities as ReflectionCapabilities, VERSION, } from '@angular/core';
|
|
4
|
+
import { BrowserModule } from '@angular/platform-browser';
|
|
5
|
+
import { dedent } from 'ts-dedent';
|
|
6
|
+
import { isComponentAlreadyDeclared } from './NgModulesAnalyzer';
|
|
7
|
+
export const reflectionCapabilities = new ReflectionCapabilities();
|
|
8
|
+
export const REMOVED_MODULES = new InjectionToken('REMOVED_MODULES');
|
|
9
|
+
export const uniqueArray = (arr) => {
|
|
10
|
+
return arr
|
|
11
|
+
.flat(Number.MAX_VALUE)
|
|
12
|
+
.filter(Boolean)
|
|
13
|
+
.filter((value, index, self) => self.indexOf(value) === index);
|
|
14
|
+
};
|
|
15
|
+
export class PropertyExtractor {
|
|
16
|
+
constructor(metadata, component) {
|
|
17
|
+
this.metadata = metadata;
|
|
18
|
+
this.component = component;
|
|
19
|
+
this.declarations = [];
|
|
20
|
+
/**
|
|
21
|
+
* Analyze NgModule Metadata
|
|
22
|
+
*
|
|
23
|
+
* - Removes Restricted Imports
|
|
24
|
+
* - Extracts providers from ModuleWithProviders
|
|
25
|
+
* - Returns a new NgModuleMetadata object
|
|
26
|
+
*/
|
|
27
|
+
this.analyzeMetadata = async (metadata) => {
|
|
28
|
+
const declarations = [...(metadata?.declarations || [])];
|
|
29
|
+
const providers = [...(metadata?.providers || [])];
|
|
30
|
+
const applicationProviders = [];
|
|
31
|
+
const imports = await Promise.all([...(metadata?.imports || [])].map(async (imported) => {
|
|
32
|
+
const [isRestricted, restrictedProviders] = await _a.analyzeRestricted(imported);
|
|
33
|
+
if (isRestricted) {
|
|
34
|
+
applicationProviders.unshift(restrictedProviders || []);
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
return imported;
|
|
38
|
+
})).then((results) => results.filter(Boolean));
|
|
39
|
+
return { ...metadata, imports, providers, applicationProviders, declarations };
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
// With the new way of mounting standalone components to the DOM via bootstrapApplication API,
|
|
43
|
+
// we should now pass ModuleWithProviders to the providers array of the bootstrapApplication function.
|
|
44
|
+
static warnImportsModuleWithProviders(propertyExtractor) {
|
|
45
|
+
const hasModuleWithProvidersImport = propertyExtractor.imports.some((importedModule) => 'ngModule' in importedModule);
|
|
46
|
+
if (hasModuleWithProvidersImport) {
|
|
47
|
+
console.warn(dedent(`
|
|
48
|
+
Storybook Warning:
|
|
49
|
+
moduleMetadata property 'imports' contains one or more ModuleWithProviders, likely the result of a 'Module.forRoot()'-style call.
|
|
50
|
+
In Storybook 7.0 we use Angular's new 'bootstrapApplication' API to mount the component to the DOM, which accepts a list of providers to set up application-wide providers.
|
|
51
|
+
Use the 'applicationConfig' decorator from '@storybook/angular' to pass your ModuleWithProviders to the 'providers' property in combination with the importProvidersFrom helper function from '@angular/core' to extract all the necessary providers.
|
|
52
|
+
Visit https://angular.io/guide/standalone-components#configuring-dependency-injection for more information
|
|
53
|
+
`));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async init() {
|
|
57
|
+
const analyzed = await this.analyzeMetadata(this.metadata);
|
|
58
|
+
this.imports = uniqueArray([CommonModule, analyzed.imports]);
|
|
59
|
+
this.providers = uniqueArray(analyzed.providers);
|
|
60
|
+
this.applicationProviders = uniqueArray(analyzed.applicationProviders);
|
|
61
|
+
this.declarations = uniqueArray(analyzed.declarations);
|
|
62
|
+
if (this.component) {
|
|
63
|
+
const { isDeclarable, isStandalone } = _a.analyzeDecorators(this.component);
|
|
64
|
+
const isDeclared = isComponentAlreadyDeclared(this.component, analyzed.declarations, this.imports);
|
|
65
|
+
if (isStandalone) {
|
|
66
|
+
this.imports.push(this.component);
|
|
67
|
+
}
|
|
68
|
+
else if (isDeclarable && !isDeclared) {
|
|
69
|
+
this.declarations.push(this.component);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
_a = PropertyExtractor;
|
|
75
|
+
PropertyExtractor.analyzeRestricted = async (ngModule) => {
|
|
76
|
+
if (ngModule === BrowserModule) {
|
|
77
|
+
console.warn(dedent `
|
|
78
|
+
Storybook Warning:
|
|
79
|
+
You have imported the "BrowserModule", which is not necessary anymore.
|
|
80
|
+
In Storybook v7.0 we are using Angular's new bootstrapApplication API to mount an Angular application to the DOM.
|
|
81
|
+
Note that the BrowserModule providers are automatically included when starting an application with bootstrapApplication()
|
|
82
|
+
Please remove the "BrowserModule" from the list of imports in your moduleMetadata definition to remove this warning.
|
|
83
|
+
`);
|
|
84
|
+
return [true];
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
const animations = await import('@angular/platform-browser/animations');
|
|
88
|
+
if (ngModule === animations.BrowserAnimationsModule) {
|
|
89
|
+
console.warn(dedent `
|
|
90
|
+
Storybook Warning:
|
|
91
|
+
You have added the "BrowserAnimationsModule" to the list of "imports" in your moduleMetadata definition of your Story.
|
|
92
|
+
In Storybook 7.0 we use Angular's new 'bootstrapApplication' API to mount the component to the DOM, which accepts a list of providers to set up application-wide providers.
|
|
93
|
+
Use the 'applicationConfig' decorator from '@storybook/angular' and add the "provideAnimations" function to the list of "providers".
|
|
94
|
+
If your Angular version does not support "provide-like" functions, use the helper function importProvidersFrom instead to set up animations. For this case, please add "importProvidersFrom(BrowserAnimationsModule)" to the list of providers of your applicationConfig definition.
|
|
95
|
+
Please visit https://angular.io/guide/standalone-components#configuring-dependency-injection for more information.
|
|
96
|
+
`);
|
|
97
|
+
return [true, animations.provideAnimations()];
|
|
98
|
+
}
|
|
99
|
+
if (ngModule === animations.NoopAnimationsModule) {
|
|
100
|
+
console.warn(dedent `
|
|
101
|
+
Storybook Warning:
|
|
102
|
+
You have added the "NoopAnimationsModule" to the list of "imports" in your moduleMetadata definition of your Story.
|
|
103
|
+
In Storybook v7.0 we are using Angular's new bootstrapApplication API to mount an Angular application to the DOM, which accepts a list of providers to set up application-wide providers.
|
|
104
|
+
Use the 'applicationConfig' decorator from '@storybook/angular' and add the "provideNoopAnimations" function to the list of "providers".
|
|
105
|
+
If your Angular version does not support "provide-like" functions, use the helper function importProvidersFrom instead to set up noop animations and to extract all necessary providers from NoopAnimationsModule. For this case, please add "importProvidersFrom(NoopAnimationsModule)" to the list of providers of your applicationConfig definition.
|
|
106
|
+
Please visit https://angular.io/guide/standalone-components#configuring-dependency-injection for more information.
|
|
107
|
+
`);
|
|
108
|
+
return [true, animations.provideNoopAnimations()];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
return [false];
|
|
113
|
+
}
|
|
114
|
+
return [false];
|
|
115
|
+
};
|
|
116
|
+
PropertyExtractor.analyzeDecorators = (component) => {
|
|
117
|
+
const decorators = reflectionCapabilities.annotations(component);
|
|
118
|
+
const isComponent = decorators.some((d) => _a.isDecoratorInstanceOf(d, 'Component'));
|
|
119
|
+
const isDirective = decorators.some((d) => _a.isDecoratorInstanceOf(d, 'Directive'));
|
|
120
|
+
const isPipe = decorators.some((d) => _a.isDecoratorInstanceOf(d, 'Pipe'));
|
|
121
|
+
const isDeclarable = isComponent || isDirective || isPipe;
|
|
122
|
+
// Check if the hierarchically lowest Component or Directive decorator (the only relevant for importing dependencies) is standalone.
|
|
123
|
+
let isStandalone = (isComponent || isDirective) &&
|
|
124
|
+
[...decorators]
|
|
125
|
+
.reverse() // reflectionCapabilities returns decorators in a hierarchically top-down order
|
|
126
|
+
.find((d) => _a.isDecoratorInstanceOf(d, 'Component') || _a.isDecoratorInstanceOf(d, 'Directive'))?.standalone;
|
|
127
|
+
//Starting in Angular 19 the default (in case it's undefined) value for standalone is true
|
|
128
|
+
if (isStandalone === undefined) {
|
|
129
|
+
isStandalone = !!(VERSION.major && Number(VERSION.major) >= 19);
|
|
130
|
+
}
|
|
131
|
+
return { isDeclarable, isStandalone };
|
|
132
|
+
};
|
|
133
|
+
PropertyExtractor.isDecoratorInstanceOf = (decorator, name) => {
|
|
134
|
+
let factory;
|
|
135
|
+
switch (name) {
|
|
136
|
+
case 'Component':
|
|
137
|
+
factory = Component;
|
|
138
|
+
break;
|
|
139
|
+
case 'Directive':
|
|
140
|
+
factory = Directive;
|
|
141
|
+
break;
|
|
142
|
+
case 'Pipe':
|
|
143
|
+
factory = Pipe;
|
|
144
|
+
break;
|
|
145
|
+
case 'Injectable':
|
|
146
|
+
factory = Injectable;
|
|
147
|
+
break;
|
|
148
|
+
case 'Input':
|
|
149
|
+
factory = Input;
|
|
150
|
+
break;
|
|
151
|
+
case 'Output':
|
|
152
|
+
factory = Output;
|
|
153
|
+
break;
|
|
154
|
+
default:
|
|
155
|
+
throw new Error(`Unknown decorator type: ${name}`);
|
|
156
|
+
}
|
|
157
|
+
return decorator instanceof factory || decorator.ngMetadataName === name;
|
|
158
|
+
};
|