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: Add back-compat method to find options (styles) in angular.json #16832

Merged
merged 4 commits into from Dec 1, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 71 additions & 3 deletions app/angular/src/server/framework-preset-angular-cli.ts
@@ -1,15 +1,20 @@
import webpack from 'webpack';
import { logger } from '@storybook/node-logger';
import { targetFromTargetString, BuilderContext } from '@angular-devkit/architect';
import { targetFromTargetString, BuilderContext, Target } from '@angular-devkit/architect';
import { sync as findUpSync } from 'find-up';
import semver from '@storybook/semver';

import { logging, JsonObject } from '@angular-devkit/core';
import { logging, JsonObject, workspaces } from '@angular-devkit/core';
import { moduleIsAvailable } from './utils/module-is-available';
import { getWebpackConfig as getWebpackConfig12_2_x } from './angular-cli-webpack-12.2.x';
import { getWebpackConfig as getWebpackConfig13_x_x } from './angular-cli-webpack-13.x.x';
import { getWebpackConfig as getWebpackConfigOlder } from './angular-cli-webpack-older';
import { PresetOptions } from './options';
import {
getDefaultProjectName,
findAngularProjectTarget,
readAngularWorkspaceConfig,
} from './angular-read-workspace';

export async function webpackFinal(baseConfig: webpack.Configuration, options: PresetOptions) {
if (!moduleIsAvailable('@angular-devkit/build-angular')) {
Expand All @@ -36,9 +41,10 @@ export async function webpackFinal(baseConfig: webpack.Configuration, options: P
getWebpackConfig: async (_baseConfig, _options) => {
const builderContext = getBuilderContext(_options);
const builderOptions = await getBuilderOptions(_options, builderContext);
const legacyDefaultOptions = await getLegacyDefaultBuildOptions(_options);

return getWebpackConfig13_x_x(_baseConfig, {
builderOptions,
builderOptions: { ...builderOptions, ...legacyDefaultOptions },
builderContext,
});
},
Expand Down Expand Up @@ -124,3 +130,65 @@ async function getBuilderOptions(

return builderOptions;
}

/**
* Get options from legacy way
* /!\ This is only for backward compatibility and wild 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.info(`=> TODO ~ legacy way to get default options`);
logger.info(`=> TODO ~ Add deprecation warning and ex for builder use ? `);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets remove those and merge the PR to hotfix the current issue and improve the situation afterwards

Suggested change
logger.info(`=> TODO ~ legacy way to get default options`);
logger.info(`=> TODO ~ Add deprecation warning and ex for builder use ? `);

Copy link

@Tim-arts Tim-arts Nov 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo here: "and wild be removed on Storybook 7.0"

Only matters if you're going to create the docs based on that

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
let project: workspaces.ProjectDefinition;
let target: workspaces.TargetDefinition;
let confName: string;
try {
const browserTarget = {
configuration: undefined,
project: getDefaultProjectName(workspaceConfig),
target: 'build',
} as Target;

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

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

const projectBuildOptions = { ...target.options };

return projectBuildOptions;
}