File size: 1,039 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
import type { webpack } from 'next/dist/compiled/webpack/webpack'

export type NextFlightActionEntryLoaderOptions = {
  actions: string
}

export type FlightActionEntryLoaderActions = [
  path: string,
  actions: { id: string; exportedName: string }[],
][]

function nextFlightActionEntryLoader(
  this: webpack.LoaderContext<NextFlightActionEntryLoaderOptions>
) {
  const { actions }: NextFlightActionEntryLoaderOptions = this.getOptions()

  const actionList = JSON.parse(actions) as FlightActionEntryLoaderActions
  const individualActions = actionList
    .map(([path, actionsFromModule]) => {
      return actionsFromModule.map(({ id, exportedName }) => {
        return [id, path, exportedName] as const
      })
    })
    .flat()

  return `
${individualActions
  .map(([id, path, exportedName]) => {
    // Re-export the same functions from the original module path as action IDs.
    return `export { ${exportedName} as "${id}" } from ${JSON.stringify(path)}`
  })
  .join('\n')}
`
}

export default nextFlightActionEntryLoader