@graphcommerce/next-config 8.1.0-canary.2 → 8.1.0-canary.5

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.
Files changed (44) hide show
  1. package/CHANGELOG.md +133 -1
  2. package/Config.graphqls +4 -2
  3. package/__tests__/config/utils/__snapshots__/mergeEnvIntoConfig.ts.snap +19 -2
  4. package/__tests__/config/utils/replaceConfigInString.ts +4 -0
  5. package/__tests__/interceptors/findPlugins.ts +473 -113
  6. package/__tests__/interceptors/generateInterceptors.ts +610 -322
  7. package/__tests__/interceptors/parseStructure.ts +158 -0
  8. package/__tests__/interceptors/writeInterceptors.ts +23 -14
  9. package/__tests__/utils/resolveDependenciesSync.ts +28 -25
  10. package/dist/config/commands/generateConfig.js +5 -2
  11. package/dist/config/demoConfig.js +19 -4
  12. package/dist/generated/config.js +8 -1
  13. package/dist/interceptors/InterceptorPlugin.js +70 -25
  14. package/dist/interceptors/RenameVisitor.js +19 -0
  15. package/dist/interceptors/Visitor.js +1418 -0
  16. package/dist/interceptors/extractExports.js +201 -0
  17. package/dist/interceptors/findOriginalSource.js +87 -0
  18. package/dist/interceptors/findPlugins.js +21 -53
  19. package/dist/interceptors/generateInterceptor.js +200 -0
  20. package/dist/interceptors/generateInterceptors.js +38 -179
  21. package/dist/interceptors/parseStructure.js +71 -0
  22. package/dist/interceptors/swc.js +16 -0
  23. package/dist/interceptors/writeInterceptors.js +19 -10
  24. package/dist/utils/resolveDependency.js +27 -5
  25. package/dist/withGraphCommerce.js +2 -1
  26. package/package.json +4 -1
  27. package/src/config/commands/generateConfig.ts +5 -2
  28. package/src/config/demoConfig.ts +19 -4
  29. package/src/config/index.ts +4 -2
  30. package/src/generated/config.ts +25 -3
  31. package/src/index.ts +16 -6
  32. package/src/interceptors/InterceptorPlugin.ts +90 -32
  33. package/src/interceptors/RenameVisitor.ts +17 -0
  34. package/src/interceptors/Visitor.ts +1847 -0
  35. package/src/interceptors/extractExports.ts +230 -0
  36. package/src/interceptors/findOriginalSource.ts +105 -0
  37. package/src/interceptors/findPlugins.ts +36 -87
  38. package/src/interceptors/generateInterceptor.ts +271 -0
  39. package/src/interceptors/generateInterceptors.ts +67 -237
  40. package/src/interceptors/parseStructure.ts +82 -0
  41. package/src/interceptors/swc.ts +13 -0
  42. package/src/interceptors/writeInterceptors.ts +26 -10
  43. package/src/utils/resolveDependency.ts +51 -12
  44. package/src/withGraphCommerce.ts +2 -1
@@ -1,23 +1,34 @@
1
- import fs from 'node:fs'
1
+ import fs from 'node:fs/promises'
2
2
  import path from 'path'
3
3
  // eslint-disable-next-line import/no-extraneous-dependencies
4
- import glob from 'glob'
4
+ import { sync as globSync } from 'glob'
5
5
  import { resolveDependenciesSync } from '../utils/resolveDependenciesSync'
6
6
  import { GenerateInterceptorsReturn } from './generateInterceptors'
7
7
 
8
- export function writeInterceptors(
8
+ function checkFileExists(file: string) {
9
+ return fs
10
+ .access(file, fs.constants.F_OK)
11
+ .then(() => true)
12
+ .catch(() => false)
13
+ }
14
+
15
+ export async function writeInterceptors(
9
16
  interceptors: GenerateInterceptorsReturn,
10
17
  cwd: string = process.cwd(),
11
18
  ) {
12
19
  const dependencies = resolveDependenciesSync(cwd)
13
20
  const existing: string[] = []
14
21
  dependencies.forEach((dependency) => {
15
- const files = glob.sync(`${dependency}/**/*.interceptor.tsx`, { cwd })
22
+ const files = globSync(
23
+ [`${dependency}/**/*.interceptor.tsx`, `${dependency}/**/*.interceptor.ts`],
24
+ { cwd },
25
+ )
16
26
  existing.push(...files)
17
27
  })
18
28
 
19
- Object.entries(interceptors).forEach(([, plugin]) => {
20
- const relativeFile = `${plugin.fromRoot}.interceptor.tsx`
29
+ const written = Object.entries(interceptors).map(async ([, plugin]) => {
30
+ const extension = plugin.sourcePath.endsWith('.tsx') ? '.tsx' : '.ts'
31
+ const relativeFile = `${plugin.fromRoot}.interceptor${extension}`
21
32
 
22
33
  if (existing.includes(relativeFile)) {
23
34
  delete existing[existing.indexOf(relativeFile)]
@@ -29,12 +40,17 @@ export function writeInterceptors(
29
40
  const fileToWrite = path.join(cwd, relativeFile)
30
41
 
31
42
  const isSame =
32
- fs.existsSync(fileToWrite) &&
33
- fs.readFileSync(fileToWrite, 'utf8').toString() === plugin.template
43
+ (await checkFileExists(fileToWrite)) &&
44
+ (await fs.readFile(fileToWrite, 'utf8')).toString() === plugin.template
34
45
 
35
- if (!isSame) fs.writeFileSync(fileToWrite, plugin.template)
46
+ if (!isSame) await fs.writeFile(fileToWrite, plugin.template)
36
47
  })
37
48
 
38
49
  // Cleanup unused interceptors
39
- existing.forEach((file) => fs.existsSync(file) && fs.unlinkSync(file))
50
+ const cleaned = existing.map(
51
+ async (file) => (await checkFileExists(file)) && (await fs.unlink(file)),
52
+ )
53
+
54
+ await Promise.all(written)
55
+ await Promise.all(cleaned)
40
56
  }
@@ -1,21 +1,36 @@
1
1
  import fs from 'node:fs'
2
2
  import { resolveDependenciesSync } from './resolveDependenciesSync'
3
3
 
4
- export type ResolveDependencyReturn = {
5
- dependency: string
6
- denormalized: string
7
- root: string
8
- fromRoot: string
9
- fromModule: string
10
- }
4
+ export type ResolveDependencyReturn =
5
+ | undefined
6
+ | {
7
+ dependency: string
8
+ denormalized: string
9
+ root: string
10
+ fromRoot: string
11
+ fromModule: string
12
+ source: string
13
+ sourcePath: string
14
+ }
11
15
 
12
- export type ResolveDependency = (req: string) => ResolveDependencyReturn
16
+ export type ResolveDependency = (
17
+ req: string,
18
+ options?: { includeSources?: boolean; optional?: boolean },
19
+ ) => ResolveDependencyReturn
13
20
 
14
21
  export const resolveDependency = (cwd: string = process.cwd()) => {
15
22
  const dependencies = resolveDependenciesSync(cwd)
16
- return (dependency: string): ResolveDependencyReturn => {
23
+
24
+ function resolve(
25
+ dependency: string,
26
+ options: { includeSources?: boolean } = {},
27
+ ): ResolveDependencyReturn {
28
+ const { includeSources = false } = options
29
+
17
30
  let dependencyPaths = {
18
31
  root: '.',
32
+ source: '',
33
+ sourcePath: '',
19
34
  dependency,
20
35
  fromRoot: dependency,
21
36
  fromModule: dependency,
@@ -27,15 +42,29 @@ export const resolveDependency = (cwd: string = process.cwd()) => {
27
42
  const relative = dependency.replace(depCandidate, '')
28
43
 
29
44
  const rootCandidate = dependency.replace(depCandidate, root)
45
+
46
+ let source = ''
47
+ let sourcePath = ''
48
+
30
49
  const fromRoot = [
31
50
  `${rootCandidate}`,
32
51
  `${rootCandidate}/index`,
33
52
  `${rootCandidate}/src/index`,
34
53
  ].find((location) =>
35
- ['ts', 'tsx'].find((extension) => fs.existsSync(`${location}.${extension}`)),
54
+ ['ts', 'tsx'].find((extension) => {
55
+ const candidatePath = `${location}.${extension}`
56
+ const exists = fs.existsSync(candidatePath)
57
+
58
+ if (includeSources && exists) {
59
+ source = fs.readFileSync(candidatePath, 'utf-8')
60
+ sourcePath = candidatePath
61
+ }
62
+ return exists
63
+ }),
36
64
  )
65
+
37
66
  if (!fromRoot) {
38
- throw Error(`Can't find plugin ${dependency}`)
67
+ return
39
68
  }
40
69
 
41
70
  const denormalized = fromRoot.replace(root, depCandidate)
@@ -46,9 +75,19 @@ export const resolveDependency = (cwd: string = process.cwd()) => {
46
75
 
47
76
  if (dependency.startsWith('./')) fromModule = `.${relative}`
48
77
 
49
- dependencyPaths = { root, dependency, denormalized, fromRoot, fromModule }
78
+ dependencyPaths = {
79
+ root,
80
+ dependency,
81
+ denormalized,
82
+ fromRoot,
83
+ fromModule,
84
+ source,
85
+ sourcePath,
86
+ }
50
87
  }
51
88
  })
52
89
  return dependencyPaths
53
90
  }
91
+
92
+ return resolve
54
93
  }
@@ -56,6 +56,7 @@ export function withGraphCommerce(nextConfig: NextConfig, cwd: string): NextConf
56
56
  experimental: {
57
57
  ...nextConfig.experimental,
58
58
  scrollRestoration: true,
59
+ bundlePagesExternals: true,
59
60
  swcPlugins: [...(nextConfig.experimental?.swcPlugins ?? []), ['@lingui/swc-plugin', {}]],
60
61
  },
61
62
  i18n: {
@@ -177,7 +178,7 @@ export function withGraphCommerce(nextConfig: NextConfig, cwd: string): NextConf
177
178
  }
178
179
  }
179
180
 
180
- config.plugins.push(new InterceptorPlugin(graphcommerceConfig))
181
+ config.plugins.push(new InterceptorPlugin(graphcommerceConfig, !options.isServer))
181
182
 
182
183
  return typeof nextConfig.webpack === 'function' ? nextConfig.webpack(config, options) : config
183
184
  },