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

fix(storybook): better webpack5 detection logging #10662

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
114 changes: 114 additions & 0 deletions packages/storybook/src/executors/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import * as ts from 'typescript';
import { stripIndents } from '@nrwl/devkit';
import { findBuilderInMainJsTs } from './utils';
import { logger } from '@nrwl/devkit';

describe('testing utilities', () => {
beforeEach(() => {
jest.spyOn(logger, 'warn');
});

it('should not log the webpack5 warning if builder is webpack5', () => {
const sourceCode = stripIndents`
const rootMain = require('../../../.storybook/main');

module.exports = {
...rootMain,
core: { ...rootMain.core, builder: 'webpack5' },
};
`;

const source = ts.createSourceFile(
'.storybook/main.js',
sourceCode,
ts.ScriptTarget.Latest,
true
);

findBuilderInMainJsTs(source);
expect(logger.warn).not.toHaveBeenCalled();
});

it('should not log the webpack5 warning if builder is @storybook/webpack5', () => {
const sourceCode = stripIndents`
const rootMain = require('../../../.storybook/main');

module.exports = {
...rootMain,
core: { ...rootMain.core, builder: '@storybook/webpack5' },
};
`;

const source = ts.createSourceFile(
'.storybook/main.js',
sourceCode,
ts.ScriptTarget.Latest,
true
);

findBuilderInMainJsTs(source);
expect(logger.warn).not.toHaveBeenCalled();
});

it('should not log the webpack5 warning if builder exists but does not contain webpack', () => {
const sourceCode = stripIndents`
const rootMain = require('../../../.storybook/main');

module.exports = {
...rootMain,
core: { ...rootMain.core, builder: '@storybook/vite-builder' },
};
`;

const source = ts.createSourceFile(
'.storybook/main.js',
sourceCode,
ts.ScriptTarget.Latest,
true
);

findBuilderInMainJsTs(source);
expect(logger.warn).not.toHaveBeenCalled();
});

it('should log the webpack5 warning if builder is webpack4', () => {
const sourceCode = stripIndents`
const rootMain = require('../../../.storybook/main');

module.exports = {
...rootMain,
core: { ...rootMain.core, builder: 'webpack4' },
};
`;

const source = ts.createSourceFile(
'.storybook/main.js',
sourceCode,
ts.ScriptTarget.Latest,
true
);

findBuilderInMainJsTs(source);
expect(logger.warn).toHaveBeenCalled();
});

it('should log the webpack5 warning if builder does not exist', () => {
const sourceCode = stripIndents`
const rootMain = require('../../../.storybook/main');

module.exports = {
...rootMain,
};
`;

const source = ts.createSourceFile(
'.storybook/main.js',
sourceCode,
ts.ScriptTarget.Latest,
true
);

findBuilderInMainJsTs(source);
expect(logger.warn).toHaveBeenCalled();
});
});
56 changes: 42 additions & 14 deletions packages/storybook/src/executors/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ExecutorContext, joinPathFragments, logger } from '@nrwl/devkit';
import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes';
import 'dotenv/config';
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import { gte } from 'semver';
import ts = require('typescript');
import { findOrCreateConfig } from '../utils/utilities';
import { CommonNxStorybookConfig } from './models';

Expand Down Expand Up @@ -67,19 +69,14 @@ function reactWebpack5Check(options: CommonNxStorybookConfig) {
encoding: 'utf8',
});

if (
!storybookConfig.match(/builder: ('webpack5'|"webpack5"|`webpack5`)/g)
) {
// storybook needs to be upgraded to webpack 5
logger.warn(`
It looks like you use Webpack 5 but your Storybook setup is not configured to leverage that
and thus falls back to Webpack 4.
Make sure you upgrade your Storybook config to use Webpack 5.

- https://gist.github.com/shilman/8856ea1786dcd247139b47b270912324#upgrade

`);
}
const source = ts.createSourceFile(
storybookConfigFilePath,
storybookConfig,
ts.ScriptTarget.Latest,
true
);

findBuilderInMainJsTs(source);
}
}

Expand Down Expand Up @@ -110,7 +107,7 @@ function webpackFinalPropertyCheck(options: CommonNxStorybookConfig) {
You have a webpack.config.js files in your Storybook configuration:
${placesToCheck.map((x) => `- "${x.path}"`).join('\n ')}

Consider switching to the "webpackFinal" property declared in "main.js" instead.
Consider switching to the "webpackFinal" property declared in "main.js" (or "main.ts") instead.
${
options.uiFramework === '@storybook/react'
? 'https://nx.dev/storybook/migrate-webpack-final-react'
Expand All @@ -137,3 +134,34 @@ export function resolveCommonStorybookOptionMapper(

return storybookOptions;
}

export function findBuilderInMainJsTs(storybookConfig: ts.SourceFile) {
const importArray = findNodes(storybookConfig, [
ts.SyntaxKind.PropertyAssignment,
]);
let builderIsSpecified = false;
importArray.forEach((parent) => {
const identifier = findNodes(parent, ts.SyntaxKind.Identifier);
const sbBuilder = findNodes(parent, ts.SyntaxKind.StringLiteral);
const builderText = sbBuilder?.[0]?.getText() ?? '';
if (identifier[0].getText() === 'builder') {
builderIsSpecified = true;
if (
builderText.includes('webpack') &&
!builderText.includes('webpack5')
) {
builderIsSpecified = false;
}
}
});
if (!builderIsSpecified) {
logger.warn(`
It looks like you use Webpack 5 but your Storybook setup is not configured to leverage that
and thus falls back to Webpack 4.
Make sure you upgrade your Storybook config to use Webpack 5.

- https://gist.github.com/shilman/8856ea1786dcd247139b47b270912324#upgrade

`);
}
}