Skip to content

Commit

Permalink
Remove legacy way of running Storybook in Angular projects
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Jan 10, 2023
1 parent 4f4b4e4 commit 4b68d75
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 394 deletions.
4 changes: 0 additions & 4 deletions code/frameworks/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
"@angular/platform-browser": ">=13.0.0",
"@angular/platform-browser-dynamic": ">=13.0.0",
"@babel/core": "*",
"@nrwl/workspace": "14.6.1",
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0",
"rxjs": "^6.0.0 || ^7.4.0",
Expand All @@ -112,9 +111,6 @@
"peerDependenciesMeta": {
"@angular/cli": {
"optional": true
},
"@nrwl/workspace": {
"optional": true
}
},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ export const isStandaloneComponent = (component: any): component is Type<unknown

const decorators = reflectionCapabilities.annotations(component);

// TODO: `standalone` is only available in Angular v14. Remove cast to `any` once
// Angular deps are updated to v14.x.x.
return (decorators || []).some((d) => d instanceof Component && (d as any).standalone);
return (decorators || []).some((d) => d instanceof Component && d.standalone);
};

/**
Expand Down
94 changes: 0 additions & 94 deletions code/frameworks/angular/src/server/angular-read-workspace.ts

This file was deleted.

108 changes: 9 additions & 99 deletions code/frameworks/angular/src/server/framework-preset-angular-cli.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,29 @@
import webpack from 'webpack';
import { logger } from '@storybook/node-logger';
import { BuilderContext, Target, targetFromTargetString } from '@angular-devkit/architect';
import { BuilderContext, targetFromTargetString } from '@angular-devkit/architect';
import { sync as findUpSync } from 'find-up';
import semver from 'semver';
import { dedent } from 'ts-dedent';

import { JsonObject, logging } from '@angular-devkit/core';
import { getWebpackConfig as getCustomWebpackConfig } from './angular-cli-webpack';
import { moduleIsAvailable } from './utils/module-is-available';
import { PresetOptions } from './preset-options';
import {
getDefaultProjectName,
findAngularProjectTarget,
readAngularWorkspaceConfig,
} from './angular-read-workspace';

export async function webpackFinal(baseConfig: webpack.Configuration, options: PresetOptions) {
if (!moduleIsAvailable('@angular-devkit/build-angular')) {
logger.info('=> Using base config because "@angular-devkit/build-angular" is not installed');
return baseConfig;
}

const angularCliVersion = await import('@angular/cli').then((m) => semver.coerce(m.VERSION.full));
const builderContext = getBuilderContext(options);
const builderOptions = await getBuilderOptions(options, builderContext);

/**
* Ordered array to use the specific getWebpackConfig according to some condition like angular-cli version
*/
const webpackGetterByVersions: {
info: string;
condition: boolean;
getWebpackConfig(
baseConfig: webpack.Configuration,
options: PresetOptions
): Promise<webpack.Configuration> | webpack.Configuration;
}[] = [
{
info: '=> Loading angular-cli config for angular >= 13.0.0',
condition: semver.satisfies(angularCliVersion, '>=13.0.0'),
getWebpackConfig: async (_baseConfig, _options) => {
const builderContext = getBuilderContext(_options);
const builderOptions = await getBuilderOptions(_options, builderContext);
const legacyDefaultOptions = await getLegacyDefaultBuildOptions(_options);

return getCustomWebpackConfig(_baseConfig, {
builderOptions: {
watch: options.configType === 'DEVELOPMENT',
...legacyDefaultOptions,
...builderOptions,
},
builderContext,
});
},
return getCustomWebpackConfig(baseConfig, {
builderOptions: {
watch: options.configType === 'DEVELOPMENT',
...builderOptions,
},
];

const webpackGetter = webpackGetterByVersions.find((wg) => wg.condition);

logger.info(webpackGetter.info);
return Promise.resolve(webpackGetter.getWebpackConfig(baseConfig, options));
builderContext,
});
}

/**
Expand Down Expand Up @@ -115,59 +81,3 @@ async function getBuilderOptions(

return builderOptions;
}

/**
* Get options from legacy way
* /!\ This is only for backward compatibility and would be removed on Storybook 7.0
* only work for angular.json with [defaultProject].build or "storybook.build" config
*/
async function getLegacyDefaultBuildOptions(options: PresetOptions) {
if (options.angularBrowserTarget !== undefined) {
// Not use legacy way with builder (`angularBrowserTarget` is defined or null with builder and undefined without)
return {};
}

logger.warn(dedent`Your Storybook startup uses a solution that will not be supported in version 7.0.
You must use angular builder to have an explicit configuration on the project used in angular.json
Read more at:
- https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#sb-angular-builder)
- https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#angular13)
`);
const dirToSearch = process.cwd();

// Read angular workspace
let workspaceConfig;
try {
workspaceConfig = await readAngularWorkspaceConfig(dirToSearch);
} catch (error) {
logger.error(
`=> Could not find angular workspace config (angular.json) on this path "${dirToSearch}"`
);
logger.info(`=> Fail to load angular-cli config. Using base config`);
return {};
}

// Find angular project target
try {
const browserTarget = {
configuration: undefined,
project: getDefaultProjectName(workspaceConfig),
target: 'build',
} as Target;

const { target } = findAngularProjectTarget(
workspaceConfig,
browserTarget.project,
browserTarget.target
);

logger.info(
`=> Using angular project "${browserTarget.project}:${browserTarget.target}" for configuring Storybook`
);
return { ...target.options };
} catch (error) {
logger.error(`=> Could not find angular project: ${error.message}`);
logger.info(`=> Fail to load angular-cli config. Using base config`);
return {};
}
}

This file was deleted.

26 changes: 0 additions & 26 deletions code/frameworks/angular/src/server/utils/normalize-optimization.ts

This file was deleted.

Loading

0 comments on commit 4b68d75

Please sign in to comment.