Skip to content

Commit

Permalink
feat(addon-docs): improve inline rendering setup
Browse files Browse the repository at this point in the history
Co-authored-by: Gaëtan Maisse <gmaisse@solocal.com>
Co-authored-by: ThibaudAv <thibaud.avenier@gmail.com>
  • Loading branch information
3 people committed Mar 25, 2021
1 parent 4a03740 commit 0782b12
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
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 0782b12

Please sign in to comment.