Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular: Improve docs inline rendering setup #14333

Merged
merged 1 commit into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions addons/docs/angular/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,10 @@ Then update `.storybook/preview.js`:

```js
import { addParameters } from '@storybook/angular';
import { prepareForInline } from '@storybook/addon-docs/angular/inline';

addParameters({
docs: {
inlineStories: true,
prepareForInline,
},
});
```
Expand Down
1 change: 0 additions & 1 deletion addons/docs/angular/inline.js

This file was deleted.

4 changes: 4 additions & 0 deletions addons/docs/src/frameworks/angular/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { SourceType } from '../../shared';
import { extractArgTypes, extractComponentDescription } from './compodoc';
import { sourceDecorator } from './sourceDecorator';
import { prepareForInline } from './prepareForInline';

export const parameters = {
docs: {
// probably set this to true by default once it's battle-tested
inlineStories: true,
prepareForInline,
extractArgTypes,
extractComponentDescription,
source: {
Expand Down
11 changes: 10 additions & 1 deletion addons/docs/src/frameworks/angular/prepareForInline.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { IStory, StoryContext } from '@storybook/angular';
import { ElementRendererService } from '@storybook/angular/element-renderer';
import { StoryFn } from '@storybook/addons';
import { logger } from '@storybook/client-logger';

const customElementsVersions: Record<string, number> = {};

Expand All @@ -27,6 +27,15 @@ export const prepareForInline = (storyFn: StoryFn<IStory>, { id, parameters }: S
}

async componentDidMount() {
const { ElementRendererService } = await import('@storybook/angular/element-renderer').catch(
(error) => {
logger.error(
'Check the documentation to activate `inlineStories`. The `@angular/elements` & `@webcomponents/custom-elements` dependencies are required.'
);
throw error;
}
);

// eslint-disable-next-line no-undef
customElements.define(
customElementsName,
Expand Down
29 changes: 29 additions & 0 deletions addons/docs/src/frameworks/angular/preset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Configuration, IgnorePlugin, Plugin } from 'webpack';

export function webpackFinal(webpackConfig: Configuration = {}): Configuration {
return {
...webpackConfig,
plugins: [...webpackConfig.plugins, ...makeAngularElementRendererOptional()],
};
}
/**
* Ignore `@storybook/angular/element-renderer` import if `@angular/elements` is not available
*/
function makeAngularElementRendererOptional(): Plugin[] {
if (
moduleIsAvailable('@angular/elements') &&
moduleIsAvailable('@webcomponents/custom-elements')
) {
return [];
}
return [new IgnorePlugin(/@storybook(\\|\/)angular(\\|\/)element-renderer/)];
}

function moduleIsAvailable(moduleName: string): boolean {
try {
require.resolve(moduleName);
return true;
} catch (e) {
return false;
}
}
7 changes: 2 additions & 5 deletions examples/angular-cli/.storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { addParameters } from '@storybook/angular';
import { setCompodocJson } from '@storybook/addon-docs/angular';
import { prepareForInline } from '@storybook/addon-docs/angular/inline';
import addCssWarning from '../src/cssWarning';

// @ts-ignore
Expand All @@ -17,17 +15,16 @@ setCompodocJson(filtered);

addCssWarning();

addParameters({
export const parameters = {
docs: {
inlineStories: true,
prepareForInline,
},
options: {
storySort: {
order: ['Welcome', 'Core ', 'Addons ', 'Basics '],
},
},
});
};

export const globalTypes = {
theme: {
Expand Down