Skip to content

Commit

Permalink
Merge pull request #14333 from storybookjs/fix-angular-inlinerenderer
Browse files Browse the repository at this point in the history
Angular: Improve docs inline rendering setup
  • Loading branch information
shilman authored Mar 26, 2021
2 parents 9637904 + 1e20a89 commit 18c1143
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
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

0 comments on commit 18c1143

Please sign in to comment.