Skip to content

Commit 34e5062

Browse files
committed
feat: add precreated client transformer support
1 parent 997e564 commit 34e5062

32 files changed

Lines changed: 1603 additions & 179 deletions

e2e/projects/tree-shaking-bundlers/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"type": "module",
77
"sideEffects": false,
88
"scripts": {
9-
"codegen": "openapi-qraft --plugin tanstack-query-react --plugin openapi-typescript ./openapi.yaml --clean -o src/generated-api --openapi-types-import-path '../schema.ts' --openapi-types-file-name schema.ts --explicit-import-extensions --create-api-client-fn createBarrelAPIClient filename:create-barrel-api-client context:BarrelAPIClientContext --create-api-client-fn createRelativeAPIClient filename:create-relative-api-client context:RelativeAPIClientContext --create-api-client-fn createRelativeExtAPIClient filename:create-relative-ts-api-client context:RelativeExtAPIClientContext --create-api-client-fn createAliasAPIClient filename:create-alias-api-client context:AliasAPIClientContext --create-api-client-fn createAliasDirectAPIClient filename:create-alias-direct-api-client context:AliasDirectAPIClientContext",
9+
"codegen": "openapi-qraft --plugin tanstack-query-react --plugin openapi-typescript ./openapi.yaml --clean -o src/generated-api --openapi-types-import-path '../schema.ts' --openapi-types-file-name schema.ts --explicit-import-extensions --create-api-client-fn createBarrelAPIClient filename:create-barrel-api-client context:BarrelAPIClientContext --create-api-client-fn createRelativeAPIClient filename:create-relative-api-client context:RelativeAPIClientContext --create-api-client-fn createRelativeExtAPIClient filename:create-relative-ts-api-client context:RelativeExtAPIClientContext --create-api-client-fn createAliasAPIClient filename:create-alias-api-client context:AliasAPIClientContext --create-api-client-fn createAliasDirectAPIClient filename:create-alias-direct-api-client context:AliasDirectAPIClientContext --create-api-client-fn createBarrelPrecreatedAPIClient filename:create-barrel-precreated-api-client --create-api-client-fn createRelativePrecreatedAPIClient filename:create-relative-precreated-api-client --create-api-client-fn createRelativeExtPrecreatedAPIClient filename:create-relative-ts-precreated-api-client --create-api-client-fn createAliasDirectPrecreatedAPIClient filename:create-alias-direct-precreated-api-client",
1010
"build": "node ./scripts/build.mjs",
11-
"build:rspack": "QRAFT_TREE_SHAKE_SCENARIO=barrel-alias node ./scripts/build-rspack.mjs",
11+
"build:rspack": "QRAFT_TREE_SHAKE_SCENARIO=mixed-context-precreated-mirrors node ./scripts/build-rspack.mjs",
1212
"e2e:pre-build": "npm run codegen",
1313
"e2e:post-build": "node ./scripts/assert-dist.mjs"
1414
},

e2e/projects/tree-shaking-bundlers/rollup.config.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { resolve } from 'node:path';
2+
import { qraftTreeShakeRollup } from '@openapi-qraft/tree-shaking-plugin/rollup';
23
import alias from '@rollup/plugin-alias';
34
import nodeResolve from '@rollup/plugin-node-resolve';
4-
import { qraftTreeShakeRollup } from '@openapi-qraft/tree-shaking-plugin/rollup';
55
import esbuild from 'rollup-plugin-esbuild';
66
import {
7+
apiClient,
78
createAPIClientFn,
89
getBundlerOutputDir,
910
getScenario,
@@ -25,7 +26,10 @@ export default {
2526
extensions: ['.ts', '.tsx', '.mts', '.cts', '.mjs', '.js'],
2627
}),
2728
}),
28-
qraftTreeShakeRollup({ createAPIClientFn }),
29+
qraftTreeShakeRollup({
30+
createAPIClientFn,
31+
apiClient,
32+
}),
2933
esbuild({
3034
include: /\.[cm]?[jt]sx?$/,
3135
sourceMap: false,

e2e/projects/tree-shaking-bundlers/rspack.config.mjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { resolve } from 'node:path';
22
import { qraftTreeShakeRspack } from '@openapi-qraft/tree-shaking-plugin/rspack';
33
import TerserPlugin from 'terser-webpack-plugin';
44
import {
5+
apiClient,
56
createAPIClientFn,
67
getBundlerOutputDir,
78
getScenario,
@@ -88,5 +89,10 @@ export default {
8889
}),
8990
],
9091
},
91-
plugins: [qraftTreeShakeRspack({ createAPIClientFn })],
92+
plugins: [
93+
qraftTreeShakeRspack({
94+
createAPIClientFn,
95+
apiClient,
96+
}),
97+
],
9298
};

e2e/projects/tree-shaking-bundlers/scripts/assert-dist.mjs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,51 @@ import assert from 'node:assert/strict';
22
import { readFile } from 'node:fs/promises';
33
import { bundlers, getBundlePath, scenarios } from './scenarios.mjs';
44

5+
const modeExpectations = {
6+
context: () => ({
7+
include: [/qraftReactAPIClient(?:__|\()/],
8+
exclude: [/qraftAPIClient(?:__|\()/],
9+
}),
10+
precreated: (scenario) => ({
11+
include: [/qraftAPIClient(?:__|\()/],
12+
exclude: [/qraftReactAPIClient(?:__|\()/],
13+
}),
14+
mixed: () => ({
15+
include: [/qraftReactAPIClient(?:__|\()/, /qraftAPIClient(?:__|\()/],
16+
exclude: [],
17+
}),
18+
};
19+
20+
const tokenMatches = (bundle, token) =>
21+
token instanceof RegExp ? token.test(bundle) : bundle.includes(token);
22+
523
for (const bundler of bundlers) {
624
for (const scenario of scenarios) {
725
const bundlePath = getBundlePath(bundler, scenario);
826
const bundle = await readFile(bundlePath, 'utf8');
27+
const resolvedModeExpectation = modeExpectations[scenario.mode](scenario);
28+
const includeTokens = [
29+
...new Set([...scenario.include, ...resolvedModeExpectation.include]),
30+
];
31+
const excludeTokens = [
32+
...new Set([...scenario.exclude, ...resolvedModeExpectation.exclude]),
33+
];
934

1035
assert.ok(
1136
bundle.length > 0,
1237
`Expected non-empty bundle for ${bundler} / ${scenario.name}`
1338
);
1439

15-
for (const token of scenario.include) {
40+
for (const token of includeTokens) {
1641
assert.ok(
17-
bundle.includes(token),
42+
tokenMatches(bundle, token),
1843
`Expected ${bundler} / ${scenario.name} bundle at ${bundlePath} to include "${token}"`
1944
);
2045
}
2146

22-
for (const token of scenario.exclude) {
47+
for (const token of excludeTokens) {
2348
assert.ok(
24-
!bundle.includes(token),
49+
!tokenMatches(bundle, token),
2550
`Expected ${bundler} / ${scenario.name} bundle at ${bundlePath} not to include "${token}"`
2651
);
2752
}

e2e/projects/tree-shaking-bundlers/scripts/build-esbuild.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { TsconfigPathsPlugin } from '@esbuild-plugins/tsconfig-paths';
33
import { qraftTreeShakeEsbuild } from '@openapi-qraft/tree-shaking-plugin/esbuild';
44
import { build } from 'esbuild';
55
import {
6+
apiClient,
67
createAPIClientFn,
78
getBundlerOutputDir,
89
getScenario,
@@ -31,7 +32,10 @@ await build({
3132
assetNames: 'assets/[name][ext]',
3233
plugins: [
3334
TsconfigPathsPlugin({ tsconfig: resolve(process.cwd(), 'tsconfig.json') }),
34-
qraftTreeShakeEsbuild({ createAPIClientFn }),
35+
qraftTreeShakeEsbuild({
36+
createAPIClientFn,
37+
apiClient,
38+
}),
3539
{
3640
name: 'external-dependencies',
3741
setup(build) {

0 commit comments

Comments
 (0)