File size: 914 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
import type { API, FileInfo } from 'jscodeshift'
import { createParserFromPath } from '../lib/parser'

export default function transformer(file: FileInfo, _api: API) {
  if (
    process.env.NODE_ENV !== 'test' &&
    !/[/\\]app[/\\].*?(page|layout|route)\.[^/\\]+$/.test(file.path)
  ) {
    return file.source
  }

  const j = createParserFromPath(file.path)
  const root = j(file.source)

  const runtimeExport = root.find(j.ExportNamedDeclaration, {
    declaration: {
      type: 'VariableDeclaration',
      declarations: [
        {
          id: { name: 'runtime' },
        },
      ],
    },
  })

  if (runtimeExport.size() !== 1) {
    return file.source
  }

  const runtimeValue = runtimeExport.find(j.StringLiteral, {
    value: 'experimental-edge',
  })

  if (runtimeValue.size() !== 1) {
    return file.source
  }

  runtimeValue.replaceWith(j.stringLiteral('edge'))

  return root.toSource()
}