Skip to content

Commit 4eb322f

Browse files
webjunkiemellowareCopilot
authored
feat(core): add external option to mutator config (orval-labs#2735)
* feat(core): add external option to mutator config Add support for esbuild's `external` option in mutator configuration. This allows users to exclude specific modules or patterns from bundling during mutator parsing. Use case: Mutators that import HTTP clients from larger codebases often have transitive dependencies on non-JS assets (scss, css, images). Esbuild cannot bundle these, causing silent failures. With `external`, users can skip problematic imports: ```js mutator: { path: './api-mutator.ts', name: 'apiMutator', external: ['*.scss', '*.css'], } ``` The user-provided patterns are merged with the existing `['*']` default that externalizes bare module imports. * Update packages/core/src/generators/mutator-info.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Melloware <mellowaredev@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent eb1c6d6 commit 4eb322f

File tree

6 files changed

+43
-1
lines changed

6 files changed

+43
-1
lines changed

packages/core/src/generators/mutator-info.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@ export async function getMutatorInfo(
1616
root?: string;
1717
namedExport?: string;
1818
alias?: Record<string, string>;
19+
external?: string[];
1920
tsconfig?: Tsconfig;
2021
},
2122
): Promise<GeneratorMutatorParsingInfo | undefined> {
2223
const {
2324
root = process.cwd(),
2425
namedExport = 'default',
2526
alias,
27+
external,
2628
tsconfig,
2729
} = options ?? {};
2830

2931
const code = await bundleFile(
3032
root,
3133
path.resolve(filePath),
3234
alias,
35+
external,
3336
tsconfig?.compilerOptions,
3437
);
3538

@@ -44,6 +47,7 @@ async function bundleFile(
4447
root: string,
4548
fileName: string,
4649
alias?: Record<string, string>,
50+
external?: string[],
4751
compilerOptions?: Tsconfig['compilerOptions'],
4852
): Promise<string> {
4953
const result = await build({
@@ -62,7 +66,7 @@ async function bundleFile(
6266
treeShaking: false,
6367
keepNames: false,
6468
alias,
65-
external: ['*'],
69+
external: external || ['*'],
6670
} satisfies BuildOptions);
6771
const { text } = result.outputFiles[0];
6872

packages/core/src/generators/mutator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export async function generateMutator({
7171
root: path.resolve(workspace),
7272
namedExport: mutatorInfoName,
7373
alias: mutator.alias,
74+
external: mutator.external,
7475
tsconfig,
7576
});
7677

packages/core/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export type NormalizedMutator = {
114114
name?: string;
115115
default: boolean;
116116
alias?: Record<string, string>;
117+
external?: string[];
117118
extension?: string;
118119
};
119120

@@ -373,6 +374,7 @@ export type MutatorObject = {
373374
name?: string;
374375
default?: boolean;
375376
alias?: Record<string, string>;
377+
external?: string[];
376378
extension?: string;
377379
};
378380

tests/configs/fetch.config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,21 @@ export default defineConfig({
380380
target: '../specifications/wildcard-responses.yaml',
381381
},
382382
},
383+
mutatorWithExternal: {
384+
output: {
385+
target: '../generated/fetch/mutator-external/endpoints.ts',
386+
schemas: '../generated/fetch/mutator-external/model',
387+
client: 'fetch',
388+
override: {
389+
mutator: {
390+
path: '../mutators/with-scss-import.ts',
391+
name: 'customFetchWithScss',
392+
external: ['*.scss'],
393+
},
394+
},
395+
},
396+
input: {
397+
target: '../specifications/petstore.yaml',
398+
},
399+
},
383400
});

tests/mutators/styles.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.test {
2+
color: red;
3+
}

tests/mutators/with-scss-import.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Mutator that imports a scss file.
3+
* Tests the user-configurable `external` option for mutator bundling.
4+
*/
5+
import './styles.scss';
6+
7+
export const customFetchWithScss = async <T>(
8+
url: string,
9+
options: RequestInit,
10+
): Promise<T> => {
11+
const response = await fetch(url, options);
12+
return response.json();
13+
};
14+
15+
export default customFetchWithScss;

0 commit comments

Comments
 (0)