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

CLI: Add Angular 12 + docs inline rendering support #14928

Merged
merged 3 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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: 1 addition & 1 deletion addons/docs/src/frameworks/angular/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function makeAngularElementRendererOptional(): Plugin[] {
) {
return [];
}
return [new IgnorePlugin(/@storybook(\\|\/)angular(\\|\/)element-renderer/)];
return [new IgnorePlugin({ resourceRegExp: /@storybook(\\|\/)angular(\\|\/)element-renderer/ })];
}

function moduleIsAvailable(moduleName: string): boolean {
Expand Down
10 changes: 8 additions & 2 deletions lib/cli/src/generators/ANGULAR/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import semver from '@storybook/semver';
import {
isDefaultProjectSet,
editStorybookTsConfig,
Expand All @@ -8,6 +9,7 @@ import {
} from './angular-helpers';
import { writeFileAsJson, copyTemplate } from '../../helpers';
import { baseGenerator, Generator } from '../baseGenerator';
import { CoreBuilder } from '../../project_types';

function editAngularAppTsConfig() {
const tsConfigJson = getAngularAppTsConfigJson();
Expand All @@ -31,8 +33,12 @@ const generator: Generator = async (packageManager, npmOptions, options) => {
'Could not find a default project in your Angular workspace.\nSet a defaultProject in your angular.json and re-run the installation.'
);
}
baseGenerator(packageManager, npmOptions, options, 'angular', {
extraPackages: ['@compodoc/compodoc'],
const angularVersion = semver.coerce(await packageManager.getVersion('@angular/core'))?.version;
const isWebpack5 = semver.gte(angularVersion, '12.0.0');
const updatedOptions = isWebpack5 ? { ...options, builder: CoreBuilder.Webpack5 } : options;

baseGenerator(packageManager, npmOptions, updatedOptions, 'angular', {
extraPackages: ['@compodoc/compodoc', '@angular/elements', '@webcomponents/custom-elements'],
addScripts: false,
});
copyTemplate(__dirname, options.storyFormat);
Expand Down
45 changes: 27 additions & 18 deletions lib/cli/src/generators/configure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fse from 'fs-extra';
import dedent from 'ts-dedent';
import { SupportedFrameworks } from '../project_types';

interface ConfigureMainOptions {
Expand Down Expand Up @@ -43,27 +44,35 @@ function configureMain({
});
}

function configurePreview(framework: SupportedFrameworks, commonJs: boolean) {
const parameters = `
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
const frameworkToPreviewParts: Partial<Record<SupportedFrameworks, any>> = {
angular: {
prefix: dedent`
import { setCompodocJson } from "@storybook/addon-docs/angular";
import docJson from "../documentation.json";
setCompodocJson(docJson);

`.trimStart(),
extraParameters: 'docs: { inlineStories: true },',
},
}`;
};

const preview =
framework === 'angular'
? `
import { setCompodocJson } from "@storybook/addon-docs/angular";
import docJson from "../documentation.json";
setCompodocJson(docJson);
function configurePreview(framework: SupportedFrameworks, commonJs: boolean) {
const { prefix = '', extraParameters = '' } = frameworkToPreviewParts[framework] || {};

${parameters}`
: parameters;
const preview = dedent`
${prefix}
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
${extraParameters}
}`
.replace(' \n', '')
.trim();

fse.writeFileSync(`./.storybook/preview.${commonJs ? 'cjs' : 'js'}`, preview, {
encoding: 'utf8',
Expand Down