182
environment?: PartialEnvironmentConfig;
183
},
184
): PluginOptions {
185
+ const overridePragma = parseConfigPragmaAsString(pragma);
186
+ if (overridePragma !== '') {
187
+ return parseConfigStringAsJS(overridePragma, defaults);
188
+ }
189
+
190
const environment = parseConfigPragmaEnvironmentForTest(
191
pragma,
192
defaults.environment ?? {},
222
}
223
return parsePluginOptions(options);
224
}
225
+
226
+export function parseConfigPragmaAsString(pragma: string): string {
227
+ // Check if it's in JS override format
228
+ for (const {key, value: val} of splitPragma(pragma)) {
229
+ if (key === 'OVERRIDE' && val != null) {
230
+ return val;
231
+ }
232
+ }
233
+ return '';
234
+}
235
+
236
+function parseConfigStringAsJS(
237
+ configString: string,
238
+ defaults: {
239
+ compilationMode: CompilationMode;
240
+ environment?: PartialEnvironmentConfig;
241
+ },
242
+): PluginOptions {
243
+ let parsedConfig: any;
244
+ try {
245
+ // Parse the JavaScript object literal
246
+ parsedConfig = new Function(`return ${configString}`)();
247
+ } catch (error) {
248
+ CompilerError.invariant(false, {
249
+ reason: 'Failed to parse config pragma as JavaScript object',
250
+ description: `Could not parse: ${configString}. Error: ${error}`,
251
+ loc: null,
252
+ suggestions: null,
253
+ });
254
+ }
255
+
256
+ console.log('OVERRIDE:', parsedConfig);
257
+
258
+ const options: Record<keyof PluginOptions, unknown> = {
259
+ ...defaultOptions,
260
+ panicThreshold: 'all_errors',
261
+ compilationMode: defaults.compilationMode,
262
+ environment: defaults.environment ?? defaultOptions.environment,
263
+ };
264
+
265
+ // Apply parsed config, merging environment if it exists
266
+ if (parsedConfig.environment) {
267
+ const mergedEnvironment = {
268
+ ...(options.environment as Record<string, unknown>),
269
+ ...parsedConfig.environment,
270
+ };
271
+
272
+ // Apply complex defaults for environment flags that are set to true
273
+ const environmentConfig: Partial<Record<keyof EnvironmentConfig, unknown>> =
274
+ {};
275
+ for (const [key, value] of Object.entries(mergedEnvironment)) {
276
+ if (hasOwnProperty(EnvironmentConfigSchema.shape, key)) {
277
+ if (value === true && key in testComplexConfigDefaults) {
278
+ environmentConfig[key] = testComplexConfigDefaults[key];
279
+ } else {
280
+ environmentConfig[key] = value;
281
+ }
282
+ }
283
+ }
284
+
285
+ // Validate environment config
286
+ const validatedEnvironment =
287
+ EnvironmentConfigSchema.safeParse(environmentConfig);
288
+ if (!validatedEnvironment.success) {
289
+ CompilerError.invariant(false, {
290
+ reason: 'Invalid environment configuration in config pragma',
291
+ description: `${fromZodError(validatedEnvironment.error)}`,
292
+ loc: null,
293
+ suggestions: null,
294
+ });
295
+ }
296
+
297
+ if (validatedEnvironment.data.enableResetCacheOnSourceFileChanges == null) {
298
+ validatedEnvironment.data.enableResetCacheOnSourceFileChanges = false;
299
+ }
300
+
301
+ options.environment = validatedEnvironment.data;
302
+ }
303
+
304
+ // Apply other config options
305
+ for (const [key, value] of Object.entries(parsedConfig)) {
306
+ if (key === 'environment') {
307
+ continue;
308
+ }
309
+
310
+ if (hasOwnProperty(defaultOptions, key)) {
311
+ if (value === true && key in testComplexPluginOptionDefaults) {
312
+ options[key] = testComplexPluginOptionDefaults[key];
313
+ } else if (key === 'target' && value === 'donotuse_meta_internal') {
314
+ options[key] = {
315
+ kind: value,
316
+ runtimeModule: 'react',
317
+ };
318
+ } else {
319
+ options[key] = value;
320
+ }
321
+ }
322
+ }
323
+
324
+ return parsePluginOptions(options);
325
+}