@graphcommerce/next-config 10.1.0-canary.34 → 10.1.0-canary.36

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 CHANGED
@@ -1,5 +1,13 @@
1
1
  # Change Log
2
2
 
3
+ ## 10.1.0-canary.36
4
+
5
+ ## 10.1.0-canary.35
6
+
7
+ ### Patch Changes
8
+
9
+ - [#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))
10
+
3
11
  ## 10.1.0-canary.34
4
12
 
5
13
  ### Patch Changes
@@ -720,3 +720,27 @@ it('Can correctly find exports that are default exports', async () => {
720
720
  "export { iconChevronLeft as iconChevronLeft } from '../../plugins/MyProjectIcon'",
721
721
  )
722
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
+ })
package/dist/index.js CHANGED
@@ -695,6 +695,22 @@ const stableStringify = (obj) => {
695
695
  const pairs = keys.map((key) => `${JSON.stringify(key)}:${stableStringify(obj[key])}`);
696
696
  return `{${pairs.join(",")}}`;
697
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
+ }
698
714
  async function generateInterceptor(interceptor, config, oldInterceptorSource) {
699
715
  const hashInput = {
700
716
  dependency: interceptor.dependency,
@@ -828,7 +844,10 @@ export const ${targetExport} = ${carry}`;
828
844
 
829
845
  // Re-export everything from the original file except the intercepted exports
830
846
  export * from './${interceptor.target.split("/").pop()}.original'
831
-
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
+ ` : ""}
832
851
  ${logOnce}${pluginExports}
833
852
  `;
834
853
  const formatted = await prettier.format(template, {
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.34",
5
+ "version": "10.1.0-canary.36",
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.34",
66
+ "@graphcommerce/prettier-config-pwa": "^10.1.0-canary.36",
67
67
  "@lingui/loader": "*",
68
68
  "@lingui/macro": "*",
69
69
  "@lingui/react": "*",
@@ -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