File size: 2,716 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import type { API, FileInfo, JSXElement, Options } from 'jscodeshift'
import { createParserFromPath } from '../parser'

export const indexContext = {
  multipleRenderRoots: false,
  nestedRender: false,
}

export default function transformer(
  file: FileInfo,
  _api: API,
  options: Options
) {
  const j = createParserFromPath(file.path)
  const root = j(file.source)
  let hasModifications = false
  let foundReactRender = 0
  let hasRenderImport = false
  let defaultReactDomImport: string | undefined

  root.find(j.ImportDeclaration).forEach((path) => {
    if (path.node.source.value === 'react-dom') {
      return path.node.specifiers.forEach((specifier) => {
        if (specifier.local.name === 'render') {
          hasRenderImport = true
        }
        if (specifier.type === 'ImportDefaultSpecifier') {
          defaultReactDomImport = specifier.local.name
        }
      })
    }
    return false
  })

  root
    .find(j.CallExpression)
    .filter((path) => {
      const { node } = path
      let found = false

      if (
        defaultReactDomImport &&
        node.callee.type === 'MemberExpression' &&
        (node.callee.object as any).name === defaultReactDomImport &&
        (node.callee.property as any).name === 'render'
      ) {
        found = true
      }

      if (hasRenderImport && (node.callee as any).name === 'render') {
        found = true
      }

      if (found) {
        foundReactRender++
        hasModifications = true

        if (!Array.isArray(path.parentPath?.parentPath?.value)) {
          indexContext.nestedRender = true
          return false
        }

        const newNode = j.exportDefaultDeclaration(
          j.functionDeclaration(
            j.identifier('NextIndexWrapper'),
            [],
            j.blockStatement([
              j.returnStatement(
                // TODO: remove React.StrictMode wrapper and use
                // next.config.js option instead?
                path.node.arguments.find(
                  (a) => a.type === 'JSXElement'
                ) as JSXElement
              ),
            ])
          )
        )

        path.parentPath.insertBefore(newNode)
        return true
      }
      return false
    })
    .remove()

  indexContext.multipleRenderRoots = foundReactRender > 1
  hasModifications =
    hasModifications &&
    !indexContext.nestedRender &&
    !indexContext.multipleRenderRoots

  // TODO: move function passed to reportWebVitals if present to
  // _app reportWebVitals and massage values to expected shape

  // root.find(j.CallExpression, {
  //   callee: {
  //     name: 'reportWebVitals'
  //   }
  // }).remove()

  return hasModifications ? root.toSource(options) : null
}