@graphcommerce/next-config 10.1.0-canary.4 → 10.1.0-canary.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +102 -0
- package/__tests__/config/utils/__snapshots__/mergeEnvIntoConfig.ts.snap +3 -0
- package/__tests__/interceptors/generateInterceptors.ts +42 -7
- package/dist/config/loadConfig.js +5 -1
- package/dist/index.js +44 -20
- package/dist/{loadConfig-DFvwanrZ.js → loadConfig-B6AF2Z1i.js} +27 -1
- package/package.json +2 -2
- package/src/commands/cleanupInterceptors.ts +1 -2
- package/src/config/loadConfig.ts +40 -1
- package/src/interceptors/findOriginalSource.ts +5 -1
- package/src/interceptors/generateInterceptor.ts +35 -1
- package/src/interceptors/writeInterceptors.ts +12 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,107 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 10.1.0-canary.40
|
|
4
|
+
|
|
5
|
+
## 10.1.0-canary.39
|
|
6
|
+
|
|
7
|
+
## 10.1.0-canary.38
|
|
8
|
+
|
|
9
|
+
## 10.1.0-canary.37
|
|
10
|
+
|
|
11
|
+
## 10.1.0-canary.36
|
|
12
|
+
|
|
13
|
+
## 10.1.0-canary.35
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- [#2649](https://github.com/graphcommerce-org/graphcommerce/pull/2649) [`d81ac44`](https://github.com/graphcommerce-org/graphcommerce/commit/d81ac44f7749961aa518ee517b7e4699c19bb189) - Interceptors now forward the original module's default export. `export * from './X.original'` does not re-export `default` (ES semantics), so intercepting a module with a default export silently dropped it. This broke `gc-mesh build` when a plugin targeted `@graphcommerce/graphql-mesh/customFetch`: GraphQL Mesh resolves the fetch function via `exported.default || exported` and received the module namespace instead of the function, failing schema introspection with `Cannot read properties of undefined (reading '__schema')`. ([@paales](https://github.com/paales))
|
|
18
|
+
|
|
19
|
+
## 10.1.0-canary.34
|
|
20
|
+
|
|
21
|
+
### Patch Changes
|
|
22
|
+
|
|
23
|
+
- [#2647](https://github.com/graphcommerce-org/graphcommerce/pull/2647) [`789e69c`](https://github.com/graphcommerce-org/graphcommerce/commit/789e69c87ed260ee87f62e64c1bc830c0cf7f58d) - Fix plugin interceptor resolution for components exported via a non-aliased re-export.
|
|
24
|
+
|
|
25
|
+
The interceptor scanner (`findOriginalSource`) only inspected an export specifier's `exported` field, which SWC leaves `null` for a non-aliased re-export such as `export { Image }` (the name lives in `orig`; `exported` is only populated by an aliased `export { Foo as Image }`). As a result, any component re-exported without an alias — e.g. `Image` from `@graphcommerce/image` — could not be targeted by a plugin and failed with "Plugin target not found". The scanner now falls back to `orig` when `exported` is absent. ([@paales](https://github.com/paales))
|
|
26
|
+
|
|
27
|
+
## 10.1.0-canary.33
|
|
28
|
+
|
|
29
|
+
## 10.1.0-canary.32
|
|
30
|
+
|
|
31
|
+
## 10.1.0-canary.31
|
|
32
|
+
|
|
33
|
+
## 10.1.0-canary.30
|
|
34
|
+
|
|
35
|
+
## 10.1.0-canary.29
|
|
36
|
+
|
|
37
|
+
## 10.1.0-canary.28
|
|
38
|
+
|
|
39
|
+
## 10.1.0-canary.27
|
|
40
|
+
|
|
41
|
+
## 10.1.0-canary.26
|
|
42
|
+
|
|
43
|
+
## 10.1.0-canary.25
|
|
44
|
+
|
|
45
|
+
## 10.1.0-canary.24
|
|
46
|
+
|
|
47
|
+
### Patch Changes
|
|
48
|
+
|
|
49
|
+
- [#2636](https://github.com/graphcommerce-org/graphcommerce/pull/2636) [`78f9803`](https://github.com/graphcommerce-org/graphcommerce/commit/78f980311d4f8db0d99fe301c62553b4e0bce098) - Fix Turbopack panic ("Cannot find module …graphcommerce.config.cjs") when loading `graphcommerce.config.ts`. cosmiconfig's sync TypeScript loader transpiles the config to a fixed temp `.cjs` path on disk and deletes it again, which races between Next.js worker processes. The `.ts` loader now transpiles via SWC and writes to a per-process unique filename next to the source so concurrent loads never collide. ([@bramvanderholst](https://github.com/bramvanderholst))
|
|
50
|
+
|
|
51
|
+
## 10.1.0-canary.23
|
|
52
|
+
|
|
53
|
+
## 10.1.0-canary.22
|
|
54
|
+
|
|
55
|
+
## 10.1.0-canary.21
|
|
56
|
+
|
|
57
|
+
## 10.1.0-canary.20
|
|
58
|
+
|
|
59
|
+
### Patch Changes
|
|
60
|
+
|
|
61
|
+
- [#2632](https://github.com/graphcommerce-org/graphcommerce/pull/2632) [`c25106b`](https://github.com/graphcommerce-org/graphcommerce/commit/c25106bf212974bbfafafabbc650e368576f07d0) - Regenerate the `mergeEnvIntoConfig` snapshot so `yarn test` passes on canary. The test input already feeds `GC_DEMO_MODE` and `GC_STOREFRONT_<i>_HYGRAPH_LOCALES_0` into the env-schema parser, but the snapshot it was compared against still reflected the pre-`demoMode` / pre-flattened-`hygraphLocales` schema, so every CI run since those config fields were added has been failing the `test` job on every PR with a snapshot mismatch unrelated to the PR's own changes. ([@paales](https://github.com/paales))
|
|
62
|
+
|
|
63
|
+
## 10.1.0-canary.19
|
|
64
|
+
|
|
65
|
+
## 10.1.0-canary.18
|
|
66
|
+
|
|
67
|
+
## 10.1.0-canary.17
|
|
68
|
+
|
|
69
|
+
## 10.1.0-canary.16
|
|
70
|
+
|
|
71
|
+
## 10.1.0-canary.15
|
|
72
|
+
|
|
73
|
+
## 10.1.0-canary.14
|
|
74
|
+
|
|
75
|
+
## 10.1.0-canary.13
|
|
76
|
+
|
|
77
|
+
### Patch Changes
|
|
78
|
+
|
|
79
|
+
- [#2621](https://github.com/graphcommerce-org/graphcommerce/pull/2621) [`4eccccd`](https://github.com/graphcommerce-org/graphcommerce/commit/4eccccdad9418e713cb6959b20e9cde6ab26d9ae) - `graphcommerce cleanup-interceptors` now actually finds and restores `.original.tsx` / `.original.ts` files when run from a consumer project. Previously `findDotOriginalFiles` walked up looking for a `@graphcommerce/*` parent package; from a consumer project (where there is no such parent) `parentPath` ended up `null` and the glob expanded to literally `null/**/*.original.tsx`, so the command silently restored nothing. Now it falls back to `cwd` and `cwd/node_modules/@graphcommerce` where interceptors actually live for consumers.
|
|
80
|
+
|
|
81
|
+
Also fixes a display bug — the final `X files restored from .original` line printed an always-`0` counter (`restoredCount` was declared but never incremented; the now-removed `removedCount` was the one being incremented). Counter and message are now consistent. ([@paales](https://github.com/paales))
|
|
82
|
+
|
|
83
|
+
## 10.1.0-canary.12
|
|
84
|
+
|
|
85
|
+
## 10.1.0-canary.11
|
|
86
|
+
|
|
87
|
+
### Patch Changes
|
|
88
|
+
|
|
89
|
+
- [#2621](https://github.com/graphcommerce-org/graphcommerce/pull/2621) [`4eccccd`](https://github.com/graphcommerce-org/graphcommerce/commit/4eccccdad9418e713cb6959b20e9cde6ab26d9ae) - `graphcommerce cleanup-interceptors` now actually finds and restores `.original.tsx` / `.original.ts` files when run from a consumer project. Previously `findDotOriginalFiles` walked up looking for a `@graphcommerce/*` parent package; from a consumer project (where there is no such parent) `parentPath` ended up `null` and the glob expanded to literally `null/**/*.original.tsx`, so the command silently restored nothing. Now it falls back to `cwd` and `cwd/node_modules/@graphcommerce` where interceptors actually live for consumers.
|
|
90
|
+
|
|
91
|
+
Also fixes a display bug — the final `X files restored from .original` line printed an always-`0` counter (`restoredCount` was declared but never incremented; the now-removed `removedCount` was the one being incremented). Counter and message are now consistent. ([@paales](https://github.com/paales))
|
|
92
|
+
|
|
93
|
+
## 10.1.0-canary.10
|
|
94
|
+
|
|
95
|
+
## 10.1.0-canary.9
|
|
96
|
+
|
|
97
|
+
## 10.1.0-canary.8
|
|
98
|
+
|
|
99
|
+
## 10.1.0-canary.7
|
|
100
|
+
|
|
101
|
+
## 10.1.0-canary.6
|
|
102
|
+
|
|
103
|
+
## 10.1.0-canary.5
|
|
104
|
+
|
|
3
105
|
## 10.1.0-canary.4
|
|
4
106
|
|
|
5
107
|
## 10.1.0-canary.3
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
exports[`parses an env config object 1`] = `
|
|
4
4
|
{
|
|
5
|
+
"GC_DEMO_MODE": true,
|
|
5
6
|
"GC_STOREFRONT": [
|
|
6
7
|
{
|
|
7
8
|
"defaultLocale": true,
|
|
@@ -13,8 +14,10 @@ exports[`parses an env config object 1`] = `
|
|
|
13
14
|
},
|
|
14
15
|
],
|
|
15
16
|
"GC_STOREFRONT_0_DEFAULT_LOCALE": true,
|
|
17
|
+
"GC_STOREFRONT_0_HYGRAPH_LOCALES_0": "en",
|
|
16
18
|
"GC_STOREFRONT_0_LOCALE": "en",
|
|
17
19
|
"GC_STOREFRONT_0_MAGENTO_STORE_CODE": "en_us",
|
|
20
|
+
"GC_STOREFRONT_1_HYGRAPH_LOCALES_0": "de",
|
|
18
21
|
"GC_STOREFRONT_1_LOCALE": "de",
|
|
19
22
|
"GC_STOREFRONT_1_MAGENTO_STORE_CODE": "de_de",
|
|
20
23
|
}
|
|
@@ -75,7 +75,8 @@ it('it generates an interceptor', async () => {
|
|
|
75
75
|
type PluginAddMollieMethodsProps = OmitPrev<
|
|
76
76
|
React.ComponentProps<typeof PluginAddMollieMethods>,
|
|
77
77
|
'Prev'
|
|
78
|
-
>
|
|
78
|
+
> &
|
|
79
|
+
PluginAddBraintreeMethodsProps
|
|
79
80
|
|
|
80
81
|
const PluginAddMollieMethodsInterceptor = (props: PluginAddMollieMethodsProps) => (
|
|
81
82
|
<PluginAddMollieMethods {...props} Prev={PluginAddBraintreeMethodsInterceptor} />
|
|
@@ -91,7 +92,9 @@ it('it generates an interceptor', async () => {
|
|
|
91
92
|
* @see {PluginAddBraintreeMethods} for source of applied plugin
|
|
92
93
|
* @see {PluginAddMollieMethods} for source of applied plugin
|
|
93
94
|
*/
|
|
94
|
-
export const PaymentMethodContextProvider =
|
|
95
|
+
export const PaymentMethodContextProvider =
|
|
96
|
+
PluginAddMollieMethodsInterceptor as typeof PaymentMethodContextProviderOriginal &
|
|
97
|
+
React.FC<PluginAddMollieMethodsProps>
|
|
95
98
|
"
|
|
96
99
|
`)
|
|
97
100
|
})
|
|
@@ -176,7 +179,8 @@ it('it can apply multiple plugins to a single export', async () => {
|
|
|
176
179
|
type PluginAddMollieMethodsProps = OmitPrev<
|
|
177
180
|
React.ComponentProps<typeof PluginAddMollieMethods>,
|
|
178
181
|
'Prev'
|
|
179
|
-
>
|
|
182
|
+
> &
|
|
183
|
+
PluginAddAdyenMethodsProps
|
|
180
184
|
|
|
181
185
|
const PluginAddMollieMethodsInterceptor = (props: PluginAddMollieMethodsProps) => (
|
|
182
186
|
<PluginAddMollieMethods {...props} Prev={PluginAddAdyenMethodsInterceptor} />
|
|
@@ -192,7 +196,9 @@ it('it can apply multiple plugins to a single export', async () => {
|
|
|
192
196
|
* @see {PluginAddAdyenMethods} for source of applied plugin
|
|
193
197
|
* @see {PluginAddMollieMethods} for source of applied plugin
|
|
194
198
|
*/
|
|
195
|
-
export const PaymentMethodContextProvider =
|
|
199
|
+
export const PaymentMethodContextProvider =
|
|
200
|
+
PluginAddMollieMethodsInterceptor as typeof PaymentMethodContextProviderOriginal &
|
|
201
|
+
React.FC<PluginAddMollieMethodsProps>
|
|
196
202
|
"
|
|
197
203
|
`)
|
|
198
204
|
})
|
|
@@ -245,7 +251,8 @@ it('it handles on duplicates gracefully', async () => {
|
|
|
245
251
|
type PluginAddBraintreeMethodsProps = OmitPrev<
|
|
246
252
|
React.ComponentProps<typeof PluginAddBraintreeMethods>,
|
|
247
253
|
'Prev'
|
|
248
|
-
>
|
|
254
|
+
> &
|
|
255
|
+
PluginAddBraintreeMethodsProps
|
|
249
256
|
|
|
250
257
|
const PluginAddBraintreeMethodsInterceptor = (props: PluginAddBraintreeMethodsProps) => (
|
|
251
258
|
<PluginAddBraintreeMethods {...props} Prev={PluginAddBraintreeMethodsInterceptor} />
|
|
@@ -261,7 +268,9 @@ it('it handles on duplicates gracefully', async () => {
|
|
|
261
268
|
* @see {PluginAddBraintreeMethods} for source of applied plugin
|
|
262
269
|
* @see {PluginAddBraintreeMethods} for source of applied plugin
|
|
263
270
|
*/
|
|
264
|
-
export const PaymentMethodContextProvider =
|
|
271
|
+
export const PaymentMethodContextProvider =
|
|
272
|
+
PluginAddBraintreeMethodsInterceptor as typeof PaymentMethodContextProviderOriginal &
|
|
273
|
+
React.FC<PluginAddBraintreeMethodsProps>
|
|
265
274
|
"
|
|
266
275
|
`)
|
|
267
276
|
})
|
|
@@ -636,7 +645,9 @@ export const Plugin = ConfigurableProductPageName
|
|
|
636
645
|
* @see {ProductPageNameMyPlugin} for source of replaced component
|
|
637
646
|
* @see {PluginConfigurableProductPageName} for source of applied plugin
|
|
638
647
|
*/
|
|
639
|
-
export const ProductPageName =
|
|
648
|
+
export const ProductPageName =
|
|
649
|
+
PluginConfigurableProductPageNameInterceptor as typeof ProductPageNameMyPlugin &
|
|
650
|
+
React.FC<PluginConfigurableProductPageNameProps>
|
|
640
651
|
"
|
|
641
652
|
`)
|
|
642
653
|
})
|
|
@@ -709,3 +720,27 @@ it('Can correctly find exports that are default exports', async () => {
|
|
|
709
720
|
"export { iconChevronLeft as iconChevronLeft } from '../../plugins/MyProjectIcon'",
|
|
710
721
|
)
|
|
711
722
|
})
|
|
723
|
+
|
|
724
|
+
it('forwards the default export of the original module', async () => {
|
|
725
|
+
const resolve = resolveDependency(projectRoot)
|
|
726
|
+
const interceptors = await generateInterceptors(
|
|
727
|
+
[
|
|
728
|
+
{
|
|
729
|
+
type: 'function',
|
|
730
|
+
targetExport: 'fetch',
|
|
731
|
+
sourceExport: 'fetch',
|
|
732
|
+
enabled: true,
|
|
733
|
+
targetModule: '@graphcommerce/graphql-mesh/customFetch',
|
|
734
|
+
sourceModule: './plugins/MyCustomFetch',
|
|
735
|
+
},
|
|
736
|
+
],
|
|
737
|
+
resolve,
|
|
738
|
+
)
|
|
739
|
+
const result = interceptors['packages/graphql-mesh/customFetch']?.template
|
|
740
|
+
|
|
741
|
+
// `export *` does not include the default export; without an explicit forward, consumers that
|
|
742
|
+
// import the default (e.g. GraphQL Mesh resolving customFetch via `exported.default ||
|
|
743
|
+
// exported`) would receive the module namespace instead of the function.
|
|
744
|
+
expect(result).toContain("export * from './customFetch.original'")
|
|
745
|
+
expect(result).toContain("export { default } from './customFetch.original'")
|
|
746
|
+
})
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
import '@swc/core';
|
|
1
2
|
import 'cosmiconfig';
|
|
3
|
+
import 'node:fs';
|
|
4
|
+
import 'node:module';
|
|
5
|
+
import 'node:path';
|
|
2
6
|
import '../generated/config.js';
|
|
3
|
-
export { l as loadConfig, r as replaceConfigInString } from '../loadConfig-
|
|
7
|
+
export { l as loadConfig, r as replaceConfigInString } from '../loadConfig-B6AF2Z1i.js';
|
|
4
8
|
import 'zod';
|
|
5
9
|
import '@apollo/client/utilities/internal';
|
|
6
10
|
import 'chalk';
|
package/dist/index.js
CHANGED
|
@@ -4,8 +4,8 @@ import path from 'path';
|
|
|
4
4
|
import { glob, sync } from 'glob';
|
|
5
5
|
import { findParentPath } from './utils/findParentPath.js';
|
|
6
6
|
import { spawn } from 'child_process';
|
|
7
|
-
import { l as loadConfig, t as toEnvStr } from './loadConfig-
|
|
8
|
-
export { r as replaceConfigInString } from './loadConfig-
|
|
7
|
+
import { l as loadConfig, t as toEnvStr } from './loadConfig-B6AF2Z1i.js';
|
|
8
|
+
export { r as replaceConfigInString } from './loadConfig-B6AF2Z1i.js';
|
|
9
9
|
import { parseFileSync, parseSync as parseSync$1, transformFileSync } from '@swc/core';
|
|
10
10
|
import fs$1, { writeFileSync, readFileSync, existsSync, rmSync, mkdirSync } from 'fs';
|
|
11
11
|
import { resolve as resolve$2 } from 'import-meta-resolve';
|
|
@@ -20,6 +20,9 @@ import { generate } from '@graphql-codegen/cli';
|
|
|
20
20
|
import { GraphCommerceConfigSchema } from './generated/config.js';
|
|
21
21
|
export { GraphCommerceDebugConfigSchema, GraphCommerceStorefrontConfigSchema } from './generated/config.js';
|
|
22
22
|
import 'cosmiconfig';
|
|
23
|
+
import 'node:fs';
|
|
24
|
+
import 'node:module';
|
|
25
|
+
import 'node:path';
|
|
23
26
|
import '@apollo/client/utilities/internal';
|
|
24
27
|
import 'chalk';
|
|
25
28
|
|
|
@@ -52,9 +55,12 @@ async function findDotOriginalFiles(cwd) {
|
|
|
52
55
|
if (p) parentPath = p;
|
|
53
56
|
else break;
|
|
54
57
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
+
const roots = parentPath ? [parentPath] : [cwd, `${cwd}/node_modules/@graphcommerce`];
|
|
59
|
+
const patterns = roots.flatMap((root) => [
|
|
60
|
+
`${root}/**/*.original.tsx`,
|
|
61
|
+
`${root}/**/*.original.ts`
|
|
62
|
+
]);
|
|
63
|
+
return Promise.all((await glob(patterns, { cwd })).map((file) => fs.realpath(file)));
|
|
58
64
|
}
|
|
59
65
|
async function writeInterceptors(interceptors, cwd = process.cwd()) {
|
|
60
66
|
const processedFiles = [];
|
|
@@ -105,13 +111,12 @@ dotenv.config({ quiet: true });
|
|
|
105
111
|
async function cleanupInterceptors(cwd = process.cwd()) {
|
|
106
112
|
console.info("\u{1F9F9} Starting interceptor cleanup...");
|
|
107
113
|
let restoredCount = 0;
|
|
108
|
-
let removedCount = 0;
|
|
109
114
|
const originalFiles = await findDotOriginalFiles(cwd);
|
|
110
115
|
console.info(`\u{1F4C2} Found ${originalFiles.length} .original files to restore`);
|
|
111
116
|
for (const originalFile of originalFiles) {
|
|
112
117
|
try {
|
|
113
118
|
await restoreOriginalFile(originalFile);
|
|
114
|
-
|
|
119
|
+
restoredCount++;
|
|
115
120
|
} catch (error) {
|
|
116
121
|
console.error(`\u274C Failed to restore ${originalFile}:`, error);
|
|
117
122
|
}
|
|
@@ -428,7 +433,7 @@ function extractValue(node, path, optional = false) {
|
|
|
428
433
|
}
|
|
429
434
|
}
|
|
430
435
|
function extractExports(module) {
|
|
431
|
-
const exports
|
|
436
|
+
const exports = {};
|
|
432
437
|
const errors = [];
|
|
433
438
|
for (const moduleItem of module.body) {
|
|
434
439
|
switch (moduleItem.type) {
|
|
@@ -442,19 +447,19 @@ function extractExports(module) {
|
|
|
442
447
|
switch (moduleItem.declaration.type) {
|
|
443
448
|
case "ClassDeclaration":
|
|
444
449
|
case "FunctionDeclaration":
|
|
445
|
-
exports
|
|
450
|
+
exports[moduleItem.declaration.identifier.value] = RUNTIME_VALUE;
|
|
446
451
|
break;
|
|
447
452
|
case "VariableDeclaration":
|
|
448
453
|
moduleItem.declaration.declarations.forEach((decl) => {
|
|
449
454
|
if (isIdentifier(decl.id) && decl.init) {
|
|
450
|
-
exports
|
|
455
|
+
exports[decl.id.value] = extractValue(decl.init, void 0, true);
|
|
451
456
|
}
|
|
452
457
|
});
|
|
453
458
|
break;
|
|
454
459
|
}
|
|
455
460
|
}
|
|
456
461
|
}
|
|
457
|
-
return [exports
|
|
462
|
+
return [exports, errors];
|
|
458
463
|
}
|
|
459
464
|
|
|
460
465
|
const pluginConfigParsed = z.object({
|
|
@@ -468,7 +473,7 @@ function nonNullable(value) {
|
|
|
468
473
|
}
|
|
469
474
|
const isObject = (input) => typeof input === "object" && input !== null && !Array.isArray(input);
|
|
470
475
|
function parseStructure(ast, gcConfig, sourceModule) {
|
|
471
|
-
const [exports
|
|
476
|
+
const [exports, errors] = extractExports(ast);
|
|
472
477
|
if (errors.length) console.error("Plugin error for", errors.join("\n"));
|
|
473
478
|
const {
|
|
474
479
|
config: moduleConfig,
|
|
@@ -479,7 +484,7 @@ function parseStructure(ast, gcConfig, sourceModule) {
|
|
|
479
484
|
plugin,
|
|
480
485
|
Plugin,
|
|
481
486
|
...rest
|
|
482
|
-
} = exports
|
|
487
|
+
} = exports;
|
|
483
488
|
const exportVals = Object.keys(rest);
|
|
484
489
|
if (component && !moduleConfig) exportVals.push("Plugin");
|
|
485
490
|
if (func && !moduleConfig) exportVals.push("plugin");
|
|
@@ -513,7 +518,7 @@ function parseStructure(ast, gcConfig, sourceModule) {
|
|
|
513
518
|
}
|
|
514
519
|
}
|
|
515
520
|
const val = {
|
|
516
|
-
targetExport: exports
|
|
521
|
+
targetExport: exports.component || exports.func || parsed.data.export,
|
|
517
522
|
sourceModule,
|
|
518
523
|
sourceExport: parsed.data.export,
|
|
519
524
|
targetModule: parsed.data.module,
|
|
@@ -618,17 +623,17 @@ function parseAndFindExport(resolved, findExport, resolve) {
|
|
|
618
623
|
if (node.type === "ExportNamedDeclaration") {
|
|
619
624
|
for (const specifier of node.specifiers) {
|
|
620
625
|
if (specifier.type === "ExportSpecifier") {
|
|
621
|
-
if (specifier.exported?.value === findExport) return resolved;
|
|
626
|
+
if ((specifier.exported?.value ?? specifier.orig.value) === findExport) return resolved;
|
|
622
627
|
} else if (specifier.type === "ExportDefaultSpecifier") ; else if (specifier.type === "ExportNamespaceSpecifier") ;
|
|
623
628
|
}
|
|
624
629
|
}
|
|
625
630
|
}
|
|
626
|
-
const exports
|
|
631
|
+
const exports = ast.body.filter((node) => node.type === "ExportAllDeclaration").sort((a, b) => {
|
|
627
632
|
const probablyA = a.source.value.includes(findExport);
|
|
628
633
|
const probablyB = b.source.value.includes(findExport);
|
|
629
634
|
return probablyA === probablyB ? 0 : probablyA ? -1 : 1;
|
|
630
635
|
});
|
|
631
|
-
for (const node of exports
|
|
636
|
+
for (const node of exports) {
|
|
632
637
|
const isRelative = node.source.value.startsWith(".");
|
|
633
638
|
if (isRelative) {
|
|
634
639
|
const d = resolved.dependency === resolved.denormalized ? resolved.dependency.substring(0, resolved.dependency.lastIndexOf("/")) : resolved.dependency;
|
|
@@ -690,6 +695,22 @@ const stableStringify = (obj) => {
|
|
|
690
695
|
const pairs = keys.map((key) => `${JSON.stringify(key)}:${stableStringify(obj[key])}`);
|
|
691
696
|
return `{${pairs.join(",")}}`;
|
|
692
697
|
};
|
|
698
|
+
function hasDefaultExport(source) {
|
|
699
|
+
try {
|
|
700
|
+
return parseSync(source).body.some((node) => {
|
|
701
|
+
if (node.type === "ExportDefaultDeclaration" || node.type === "ExportDefaultExpression")
|
|
702
|
+
return true;
|
|
703
|
+
if (node.type === "ExportNamedDeclaration") {
|
|
704
|
+
return node.specifiers.some(
|
|
705
|
+
(s) => s.type === "ExportSpecifier" && (s.exported ?? s.orig).value === "default"
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
return false;
|
|
709
|
+
});
|
|
710
|
+
} catch {
|
|
711
|
+
return false;
|
|
712
|
+
}
|
|
713
|
+
}
|
|
693
714
|
async function generateInterceptor(interceptor, config, oldInterceptorSource) {
|
|
694
715
|
const hashInput = {
|
|
695
716
|
dependency: interceptor.dependency,
|
|
@@ -823,7 +844,10 @@ export const ${targetExport} = ${carry}`;
|
|
|
823
844
|
|
|
824
845
|
// Re-export everything from the original file except the intercepted exports
|
|
825
846
|
export * from './${interceptor.target.split("/").pop()}.original'
|
|
826
|
-
|
|
847
|
+
${hasDefaultExport(interceptor.source) ? `
|
|
848
|
+
// Forward the original module's default export: \`export *\` does not include it.
|
|
849
|
+
export { default } from './${interceptor.target.split("/").pop()}.original'
|
|
850
|
+
` : ""}
|
|
827
851
|
${logOnce}${pluginExports}
|
|
828
852
|
`;
|
|
829
853
|
const formatted = await prettier.format(template, {
|
|
@@ -1485,7 +1509,7 @@ async function createConfigSectionFile(sectionName, sectionValue, targetDir, tar
|
|
|
1485
1509
|
for (const key of schemaKeys) {
|
|
1486
1510
|
completeSectionValue[key] = sectionValue[key];
|
|
1487
1511
|
}
|
|
1488
|
-
const exports
|
|
1512
|
+
const exports = Object.entries(completeSectionValue).map(([key, value]) => {
|
|
1489
1513
|
const valueStr = generateValueLiteral(value);
|
|
1490
1514
|
const propertyPath = `'${sectionName}.${key}'`;
|
|
1491
1515
|
const typeAnnotation = `: Get<GraphCommerceConfig, ${propertyPath}>`;
|
|
@@ -1496,7 +1520,7 @@ import type { Get } from 'type-fest'` ;
|
|
|
1496
1520
|
const content = `// Auto-generated by 'yarn graphcommerce codegen-config-values'
|
|
1497
1521
|
${imports}
|
|
1498
1522
|
|
|
1499
|
-
${exports
|
|
1523
|
+
${exports}
|
|
1500
1524
|
`;
|
|
1501
1525
|
const formattedContent = await prettier.format(content, {
|
|
1502
1526
|
...prettierConf,
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
import { transformFileSync } from '@swc/core';
|
|
1
2
|
import { cosmiconfigSync } from 'cosmiconfig';
|
|
3
|
+
import { writeFileSync, existsSync, rmSync } from 'node:fs';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
import { join, dirname } from 'node:path';
|
|
2
6
|
import { GraphCommerceConfigSchema } from './generated/config.js';
|
|
3
7
|
import { cloneDeep, mergeDeep } from '@apollo/client/utilities/internal';
|
|
4
8
|
import chalk from 'chalk';
|
|
@@ -276,7 +280,29 @@ function replaceConfigInString(str, config) {
|
|
|
276
280
|
}
|
|
277
281
|
|
|
278
282
|
const moduleName = "graphcommerce";
|
|
279
|
-
const
|
|
283
|
+
const requireFromHere = createRequire(import.meta.url);
|
|
284
|
+
let tmpCounter = 0;
|
|
285
|
+
function loadTsConfig(filepath) {
|
|
286
|
+
const { code } = transformFileSync(filepath, {
|
|
287
|
+
jsc: { parser: { syntax: "typescript" }, target: "es2022" },
|
|
288
|
+
module: { type: "commonjs" }
|
|
289
|
+
});
|
|
290
|
+
const tmpFile = join(
|
|
291
|
+
dirname(filepath),
|
|
292
|
+
`.${moduleName}.tmp.${process.pid}-${tmpCounter++}.cjs`
|
|
293
|
+
);
|
|
294
|
+
try {
|
|
295
|
+
writeFileSync(tmpFile, code);
|
|
296
|
+
delete requireFromHere.cache[tmpFile];
|
|
297
|
+
const mod = requireFromHere(tmpFile);
|
|
298
|
+
return mod.default ?? mod;
|
|
299
|
+
} finally {
|
|
300
|
+
if (existsSync(tmpFile)) rmSync(tmpFile, { force: true });
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const loader = cosmiconfigSync(moduleName, {
|
|
304
|
+
loaders: { ".ts": loadTsConfig }
|
|
305
|
+
});
|
|
280
306
|
function loadConfig(cwd) {
|
|
281
307
|
const isMainProcess = !process.send;
|
|
282
308
|
try {
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@graphcommerce/next-config",
|
|
3
3
|
"homepage": "https://www.graphcommerce.org/",
|
|
4
4
|
"repository": "github:graphcommerce-org/graphcommerce",
|
|
5
|
-
"version": "10.1.0-canary.
|
|
5
|
+
"version": "10.1.0-canary.40",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
},
|
|
64
64
|
"peerDependencies": {
|
|
65
65
|
"@apollo/client": "*",
|
|
66
|
-
"@graphcommerce/prettier-config-pwa": "^10.1.0-canary.
|
|
66
|
+
"@graphcommerce/prettier-config-pwa": "^10.1.0-canary.40",
|
|
67
67
|
"@lingui/loader": "*",
|
|
68
68
|
"@lingui/macro": "*",
|
|
69
69
|
"@lingui/react": "*",
|
|
@@ -7,7 +7,6 @@ export async function cleanupInterceptors(cwd: string = process.cwd()) {
|
|
|
7
7
|
console.info('🧹 Starting interceptor cleanup...')
|
|
8
8
|
|
|
9
9
|
let restoredCount = 0
|
|
10
|
-
let removedCount = 0
|
|
11
10
|
|
|
12
11
|
const originalFiles = await findDotOriginalFiles(cwd)
|
|
13
12
|
console.info(`📂 Found ${originalFiles.length} .original files to restore`)
|
|
@@ -15,7 +14,7 @@ export async function cleanupInterceptors(cwd: string = process.cwd()) {
|
|
|
15
14
|
for (const originalFile of originalFiles) {
|
|
16
15
|
try {
|
|
17
16
|
await restoreOriginalFile(originalFile)
|
|
18
|
-
|
|
17
|
+
restoredCount++
|
|
19
18
|
} catch (error) {
|
|
20
19
|
console.error(`❌ Failed to restore ${originalFile}:`, error)
|
|
21
20
|
}
|
package/src/config/loadConfig.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
|
+
import { transformFileSync } from '@swc/core'
|
|
2
3
|
import { cosmiconfigSync } from 'cosmiconfig'
|
|
4
|
+
import { existsSync, rmSync, writeFileSync } from 'node:fs'
|
|
5
|
+
import { createRequire } from 'node:module'
|
|
6
|
+
import { dirname, join } from 'node:path'
|
|
3
7
|
import type { GraphCommerceConfig } from '../generated/config'
|
|
4
8
|
import { GraphCommerceConfigSchema } from '../generated/config'
|
|
5
9
|
import { demoConfig } from './demoConfig'
|
|
@@ -8,7 +12,42 @@ import { formatAppliedEnv, mergeEnvIntoConfig } from './utils/mergeEnvIntoConfig
|
|
|
8
12
|
export * from './utils/replaceConfigInString'
|
|
9
13
|
|
|
10
14
|
const moduleName = 'graphcommerce'
|
|
11
|
-
const
|
|
15
|
+
const requireFromHere = createRequire(import.meta.url)
|
|
16
|
+
let tmpCounter = 0
|
|
17
|
+
|
|
18
|
+
// cosmiconfig's built-in sync TypeScript loader transpiles the config to a
|
|
19
|
+
// fixed `.cjs` path on disk and removes it again — that races between
|
|
20
|
+
// Next.js worker processes (especially under Turbopack), producing
|
|
21
|
+
// "Cannot find module" panics. We transpile via SWC and write to a
|
|
22
|
+
// per-process unique filename so concurrent loads never collide. The temp
|
|
23
|
+
// file lives next to the source so relative `require()` calls in the
|
|
24
|
+
// config still resolve.
|
|
25
|
+
function loadTsConfig(filepath: string): Record<string, unknown> {
|
|
26
|
+
const { code } = transformFileSync(filepath, {
|
|
27
|
+
jsc: { parser: { syntax: 'typescript' }, target: 'es2022' },
|
|
28
|
+
module: { type: 'commonjs' },
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
const tmpFile = join(
|
|
32
|
+
dirname(filepath),
|
|
33
|
+
`.${moduleName}.tmp.${process.pid}-${tmpCounter++}.cjs`,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
writeFileSync(tmpFile, code)
|
|
38
|
+
delete requireFromHere.cache[tmpFile]
|
|
39
|
+
const mod = requireFromHere(tmpFile) as
|
|
40
|
+
| { default?: Record<string, unknown> }
|
|
41
|
+
| Record<string, unknown>
|
|
42
|
+
return (mod as { default?: Record<string, unknown> }).default ?? mod
|
|
43
|
+
} finally {
|
|
44
|
+
if (existsSync(tmpFile)) rmSync(tmpFile, { force: true })
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const loader = cosmiconfigSync(moduleName, {
|
|
49
|
+
loaders: { '.ts': loadTsConfig },
|
|
50
|
+
})
|
|
12
51
|
|
|
13
52
|
export function loadConfig(cwd: string): GraphCommerceConfig {
|
|
14
53
|
const isMainProcess = !process.send
|
|
@@ -37,7 +37,11 @@ function parseAndFindExport(
|
|
|
37
37
|
if (node.type === 'ExportNamedDeclaration') {
|
|
38
38
|
for (const specifier of node.specifiers) {
|
|
39
39
|
if (specifier.type === 'ExportSpecifier') {
|
|
40
|
-
|
|
40
|
+
// A non-aliased re-export (`export { Image }`) carries the name in
|
|
41
|
+
// `orig`, with `exported` left null; only an aliased re-export
|
|
42
|
+
// (`export { Foo as Image }`) populates `exported`. Fall back to
|
|
43
|
+
// `orig` so components exported without an alias remain interceptable.
|
|
44
|
+
if ((specifier.exported?.value ?? specifier.orig.value) === findExport) return resolved
|
|
41
45
|
} else if (specifier.type === 'ExportDefaultSpecifier') {
|
|
42
46
|
// todo
|
|
43
47
|
} else if (specifier.type === 'ExportNamespaceSpecifier') {
|
|
@@ -2,6 +2,7 @@ import prettierConf from '@graphcommerce/prettier-config-pwa'
|
|
|
2
2
|
import prettier from 'prettier'
|
|
3
3
|
import type { GraphCommerceDebugConfig } from '../generated/config'
|
|
4
4
|
import type { ResolveDependencyReturn } from '../utils/resolveDependency'
|
|
5
|
+
import { parseSync } from './swc'
|
|
5
6
|
|
|
6
7
|
type PluginBaseConfig = {
|
|
7
8
|
type: 'component' | 'function' | 'replace'
|
|
@@ -109,6 +110,34 @@ const stableStringify = (obj: any): string => {
|
|
|
109
110
|
return `{${pairs.join(',')}}`
|
|
110
111
|
}
|
|
111
112
|
|
|
113
|
+
/**
|
|
114
|
+
* `export * from './X.original'` does not re-export the original module's default export (ES
|
|
115
|
+
* semantics), so an intercepted module would silently lose it. Consumers that prefer the default
|
|
116
|
+
* export (e.g. GraphQL Mesh's getPackage does `exported.default || exported` to resolve
|
|
117
|
+
* `customFetch`) then receive the whole module namespace instead of the function, which breaks at
|
|
118
|
+
* runtime. Detect a default export so the interceptor can forward it explicitly.
|
|
119
|
+
*
|
|
120
|
+
* On regeneration the resolved source may be a previously generated interceptor; its emitted
|
|
121
|
+
* `export { default } from './X.original'` is an ExportNamedDeclaration with a `default` specifier
|
|
122
|
+
* and is detected the same way, keeping the output stable.
|
|
123
|
+
*/
|
|
124
|
+
function hasDefaultExport(source: string): boolean {
|
|
125
|
+
try {
|
|
126
|
+
return parseSync(source).body.some((node) => {
|
|
127
|
+
if (node.type === 'ExportDefaultDeclaration' || node.type === 'ExportDefaultExpression')
|
|
128
|
+
return true
|
|
129
|
+
if (node.type === 'ExportNamedDeclaration') {
|
|
130
|
+
return node.specifiers.some(
|
|
131
|
+
(s) => s.type === 'ExportSpecifier' && (s.exported ?? s.orig).value === 'default',
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
return false
|
|
135
|
+
})
|
|
136
|
+
} catch {
|
|
137
|
+
return false
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
112
141
|
/**
|
|
113
142
|
* Extract the identifier from the first line of the source file. The identifier is in the format:
|
|
114
143
|
* slash-star hash:${identifer} star-slash
|
|
@@ -340,7 +369,12 @@ export const ${targetExport} = ${carry}`
|
|
|
340
369
|
|
|
341
370
|
// Re-export everything from the original file except the intercepted exports
|
|
342
371
|
export * from './${interceptor.target.split('/').pop()}.original'
|
|
343
|
-
|
|
372
|
+
${
|
|
373
|
+
hasDefaultExport(interceptor.source)
|
|
374
|
+
? `\n // Forward the original module's default export: \`export *\` does not include it.
|
|
375
|
+
export { default } from './${interceptor.target.split('/').pop()}.original'\n`
|
|
376
|
+
: ''
|
|
377
|
+
}
|
|
344
378
|
${logOnce}${pluginExports}
|
|
345
379
|
`
|
|
346
380
|
|
|
@@ -40,11 +40,18 @@ export async function findDotOriginalFiles(cwd: string) {
|
|
|
40
40
|
else break
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
// Called from inside a @graphcommerce package (upstream dev) → scan from the
|
|
44
|
+
// outermost @graphcommerce parent. Called from a consumer project (no
|
|
45
|
+
// @graphcommerce parent) → scan the project AND node_modules/@graphcommerce,
|
|
46
|
+
// because the interceptor system writes .original files alongside the
|
|
47
|
+
// installed packages there.
|
|
48
|
+
const roots = parentPath ? [parentPath] : [cwd, `${cwd}/node_modules/@graphcommerce`]
|
|
49
|
+
const patterns = roots.flatMap((root) => [
|
|
50
|
+
`${root}/**/*.original.tsx`,
|
|
51
|
+
`${root}/**/*.original.ts`,
|
|
52
|
+
])
|
|
53
|
+
|
|
54
|
+
return Promise.all((await glob(patterns, { cwd })).map((file) => fs.realpath(file)))
|
|
48
55
|
}
|
|
49
56
|
|
|
50
57
|
export async function writeInterceptors(
|