-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
utils.ts
169 lines (147 loc) · 4.92 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { ExecutorContext, joinPathFragments, logger } from '@nrwl/devkit';
import { findNodes } from 'nx/src/utils/typescript';
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';
export interface NodePackage {
name: string;
version: string;
}
export function getStorybookFrameworkPath(uiFramework) {
const serverOptionsPaths = {
'@storybook/react': '@storybook/react/dist/cjs/server/options',
'@storybook/html': '@storybook/html/dist/cjs/server/options',
'@storybook/vue': '@storybook/vue/dist/cjs/server/options',
'@storybook/vue3': '@storybook/vue3/dist/cjs/server/options',
'@storybook/web-components':
'@storybook/web-components/dist/cjs/server/options',
'@storybook/svelte': '@storybook/svelte/dist/cjs/server/options',
};
if (isStorybookV62onwards(uiFramework)) {
return serverOptionsPaths[uiFramework];
} else {
return `${uiFramework}/dist/server/options`;
}
}
function isStorybookV62onwards(uiFramework) {
const storybookPackageVersion = require(join(
uiFramework,
'package.json'
)).version;
return gte(storybookPackageVersion, '6.2.0-rc.4');
}
export function runStorybookSetupCheck(options: CommonNxStorybookConfig) {
webpackFinalPropertyCheck(options);
reactWebpack5Check(options);
}
function reactWebpack5Check(options: CommonNxStorybookConfig) {
if (options.uiFramework === '@storybook/react') {
const source = mainJsTsFileContent(options.config.configFolder);
const rootSource = mainJsTsFileContent('.storybook');
// check whether the current Storybook configuration has the webpack 5 builder enabled
if (
builderIsWebpackButNotWebpack5(source) &&
builderIsWebpackButNotWebpack5(rootSource)
) {
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
`);
}
}
}
function mainJsTsFileContent(configFolder: string): ts.SourceFile {
let storybookConfigFilePath = joinPathFragments(configFolder, 'main.js');
if (!existsSync(storybookConfigFilePath)) {
storybookConfigFilePath = joinPathFragments(configFolder, 'main.ts');
}
if (!existsSync(storybookConfigFilePath)) {
// looks like there's no main config file, so skip
return;
}
const storybookConfig = readFileSync(storybookConfigFilePath, {
encoding: 'utf8',
});
return ts.createSourceFile(
storybookConfigFilePath,
storybookConfig,
ts.ScriptTarget.Latest,
true
);
}
function webpackFinalPropertyCheck(options: CommonNxStorybookConfig) {
let placesToCheck = [
{
path: joinPathFragments('.storybook', 'webpack.config.js'),
result: false,
},
{
path: joinPathFragments(options.config.configFolder, 'webpack.config.js'),
result: false,
},
];
placesToCheck = placesToCheck
.map((entry) => {
return {
...entry,
result: existsSync(entry.path),
};
})
.filter((x) => x.result === true);
if (placesToCheck.length > 0) {
logger.warn(
`
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" (or "main.ts") instead.
${
options.uiFramework === '@storybook/react'
? 'https://nx.dev/storybook/migrate-webpack-final-react'
: 'https://nx.dev/storybook/migrate-webpack-final-angular'
}
`
);
}
}
export function resolveCommonStorybookOptionMapper(
builderOptions: CommonNxStorybookConfig,
frameworkOptions: any,
context: ExecutorContext
) {
const storybookConfig = findOrCreateConfig(builderOptions.config, context);
const storybookOptions = {
workspaceRoot: context.root,
configDir: storybookConfig,
...frameworkOptions,
frameworkPresets: [...(frameworkOptions.frameworkPresets || [])],
watch: false,
};
return storybookOptions;
}
export function builderIsWebpackButNotWebpack5(
storybookConfig: ts.SourceFile
): boolean {
const importArray = findNodes(storybookConfig, [
ts.SyntaxKind.PropertyAssignment,
]);
let builderIsWebpackNot5 = 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' &&
builderText.includes('webpack') &&
!builderText.includes('webpack5')
) {
builderIsWebpackNot5 = true;
}
});
return builderIsWebpackNot5;
}