-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathconfig.ts
More file actions
243 lines (235 loc) · 7.21 KB
/
config.ts
File metadata and controls
243 lines (235 loc) · 7.21 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import {
RsdoctorRspackPluginOptions,
RsdoctorRspackPluginOptionsNormalized,
} from '@/types';
import { Config, Linter, Plugin, SDK } from '@rsdoctor/types';
import { chalk, logger } from '@rsdoctor/utils/logger';
import assert from 'assert';
import {
convertReportCodeTypeObject,
processModeConfigurations,
} from './normalize-config';
function defaultBoolean(v: unknown, dft: boolean): boolean {
return typeof v === 'boolean' ? v : dft;
}
function getDefaultOutput() {
return {
mode: undefined,
reportCodeType: {
noModuleSource: false,
noAssetsAndModuleSource: false,
noCode: false,
},
options: undefined,
reportDir: '',
compressData: undefined,
};
}
function getDefaultSupports() {
return {
parseBundle: true,
banner: undefined,
gzip: true, // change the gzip to true by default.
};
}
function isJsonOutputEnv(value: unknown): boolean {
return value === 'json';
}
function normalizeFeatures(features: any, mode: keyof typeof SDK.IMode) {
if (Array.isArray(features)) {
return {
loader: features.includes('loader'),
plugins: features.includes('plugins'),
resolver: features.includes('resolver'),
bundle: features.includes('bundle'),
treeShaking: features.includes('treeShaking'),
lite: features.includes('lite') || mode === SDK.IMode[SDK.IMode.lite],
};
}
return {
loader: defaultBoolean(features.loader, true),
plugins: defaultBoolean(features.plugins, true),
resolver: defaultBoolean(features.resolver, false),
bundle: defaultBoolean(features.bundle, true),
treeShaking: defaultBoolean(features.treeShaking, false),
lite:
defaultBoolean(features.lite, false) ||
mode === SDK.IMode[SDK.IMode.lite],
};
}
function normalizeLinter(linter: any) {
return {
rules: {} as any,
extends: [] as any,
level: 'Error',
...linter,
};
}
function isValidMode(mode: any): mode is keyof typeof SDK.IMode {
return typeof mode === 'string' && ['brief', 'normal', 'lite'].includes(mode);
}
export function normalizeUserConfig<Rules extends Linter.ExtendRuleData[]>(
config: Plugin.RsdoctorWebpackPluginOptions<Rules> = {},
): Plugin.RsdoctorPluginOptionsNormalized<Rules> {
const userOutput = config.output;
const defaultOutput = getDefaultOutput();
const outputConfig: Config.IOutput<'brief' | 'normal'> = isJsonOutputEnv(
process.env.RSDOCTOR_OUTPUT,
)
? {
reportDir: userOutput?.reportDir,
compressData: userOutput?.compressData,
mode: 'brief' as const,
options: {
type: ['json'] as Array<'json'>,
},
}
: (userOutput ?? defaultOutput);
const normalizedConfig = {
...config,
output: outputConfig,
};
const {
linter = {},
features = {},
loaderInterceptorOptions = {},
disableClientServer: userDisableClientServer = false,
sdkInstance,
innerClientPath = '',
output = outputConfig,
supports = getDefaultSupports(),
port,
printLog = { serverUrls: true },
mode = undefined,
brief = undefined,
} = normalizedConfig;
// If process.env.RSTEST is set to true, disableClientServer should be false
// Otherwise, if process.env.CI is set, disableClientServer should be true
const disableClientServer =
process.env.RSTEST === 'true'
? false
: process.env.CI
? true
: userDisableClientServer;
assert(typeof linter === 'object');
assert(typeof features === 'object' || Array.isArray(features));
assert(typeof loaderInterceptorOptions === 'object');
assert(typeof disableClientServer === 'boolean');
let finalMode: keyof typeof SDK.IMode =
('mode' in output && isValidMode(output.mode)
? output.mode === ('lite' as SDK.IMode.normal)
? SDK.IMode[SDK.IMode.normal]
: output.mode
: undefined) ||
mode ||
SDK.IMode[SDK.IMode.normal];
if (mode) {
logger.info(
chalk.yellow(
`The 'mode' configuration will be deprecated in a future version. Please use 'output.mode' instead.`,
),
);
}
const _features = normalizeFeatures(features, finalMode);
const _linter = normalizeLinter(linter);
if (_features.lite || finalMode === SDK.IMode[SDK.IMode.lite]) {
logger.info(
chalk.yellow(
`Lite features will be deprecated in a future version. Please use 'output: { reportCodeType: { noAssetsAndModuleSource: true }}' instead.`,
),
);
}
// Process mode-specific configurations
const { finalBrief, finalNormalOptions } = processModeConfigurations(
finalMode,
output,
brief,
);
// If lite mode is enabled and mode is not brief: finalBrief, set mode to lite
if (_features.lite && finalMode !== SDK.IMode[SDK.IMode.brief]) {
finalMode = SDK.IMode[SDK.IMode.lite] as keyof typeof SDK.IMode;
}
const reportCodeType = output.reportCodeType
? normalizeReportType(output.reportCodeType, finalMode)
: normalizeReportType(getDefaultOutput().reportCodeType, finalMode);
const res: Plugin.RsdoctorPluginOptionsNormalized<Rules> = {
linter: _linter,
features: _features,
loaderInterceptorOptions: {
skipLoaders: Array.isArray(loaderInterceptorOptions.skipLoaders)
? loaderInterceptorOptions.skipLoaders
: [],
},
disableClientServer,
sdkInstance,
output: {
mode: finalMode,
options: finalMode === 'brief' ? finalBrief : finalNormalOptions,
reportCodeType,
reportDir: output.reportDir || '',
},
innerClientPath,
supports,
port,
printLog,
};
// Add deprecation warning for compressData
if (output.compressData !== undefined) {
logger.info(
chalk.yellow(
`The 'compressData' configuration will be deprecated in a future version.`,
),
);
}
return res;
}
export const normalizeReportType = (
reportCodeType: Plugin.IReportCodeType | Plugin.NewReportCodeType,
mode: keyof typeof SDK.IMode,
): SDK.ToDataType => {
const convertedReportCodeType =
typeof reportCodeType === 'object'
? convertReportCodeTypeObject(reportCodeType)
: reportCodeType;
if (convertedReportCodeType === 'noCode') {
return SDK.ToDataType.NoCode;
}
if (mode === SDK.IMode[SDK.IMode.brief]) {
return SDK.ToDataType.NoCode;
}
if (mode === SDK.IMode[SDK.IMode.lite]) {
return SDK.ToDataType.NoSourceAndAssets;
}
if (convertedReportCodeType === 'noAssetsAndModuleSource') {
return SDK.ToDataType.NoSourceAndAssets;
}
if (convertedReportCodeType === 'noModuleSource') {
return SDK.ToDataType.NoSource;
}
return SDK.ToDataType.Normal;
};
export function normalizeRspackUserOptions<
Rules extends Linter.ExtendRuleData[],
>(
options: RsdoctorRspackPluginOptions<Rules>,
): RsdoctorRspackPluginOptionsNormalized<Rules> {
const config: RsdoctorRspackPluginOptionsNormalized<Rules> =
normalizeUserConfig(options);
config.experiments ??= {
enableNativePlugin: {
moduleGraph: true,
chunkGraph: true,
},
};
// Default to true, only set to false when explicitly set to false
if (
typeof options.experiments?.enableNativePlugin === 'boolean' &&
options.experiments?.enableNativePlugin === false
) {
config.experiments.enableNativePlugin = {
moduleGraph: false,
chunkGraph: false,
};
}
return config;
}