File size: 4,632 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import type { Compiler } from '@rspack/core'
import {
  getInvalidator,
  getEntries,
  EntryTypes,
  getEntryKey,
} from '../../../server/dev/on-demand-entry-handler'
import { COMPILER_NAMES } from '../../../shared/lib/constants'

import { getProxiedPluginState } from '../../build-context'
import { PAGE_TYPES } from '../../../lib/page-types'
import { getRspackCore } from '../../../shared/lib/get-rspack'

type Actions = {
  [actionId: string]: {
    workers: {
      [name: string]: { moduleId: string | number; async: boolean }
    }
    // Record which layer the action is in (rsc or sc_action), in the specific entry.
    layer: {
      [name: string]: string
    }
  }
}

export type ActionManifest = {
  // Assign a unique encryption key during production build.
  encryptionKey: string
  node: Actions
  edge: Actions
}

export interface ModuleInfo {
  moduleId: string | number
  async: boolean
}

const pluginState = getProxiedPluginState({
  // A map to track "action" -> "list of bundles".
  serverActions: {} as ActionManifest['node'],
  edgeServerActions: {} as ActionManifest['edge'],

  serverActionModules: {} as {
    [workerName: string]: { server?: ModuleInfo; client?: ModuleInfo }
  },

  edgeServerActionModules: {} as {
    [workerName: string]: { server?: ModuleInfo; client?: ModuleInfo }
  },

  ssrModules: {} as { [ssrModuleId: string]: ModuleInfo },
  edgeSsrModules: {} as { [ssrModuleId: string]: ModuleInfo },

  rscModules: {} as { [rscModuleId: string]: ModuleInfo },
  edgeRscModules: {} as { [rscModuleId: string]: ModuleInfo },

  injectedClientEntries: {} as Record<string, string>,
})

interface Options {
  dev: boolean
  appDir: string
  isEdgeServer: boolean
  encryptionKey: string
}

export class RspackFlightClientEntryPlugin {
  plugin: any
  compiler?: Compiler

  constructor(options: Options) {
    const { FlightClientEntryPlugin } = getRspackCore()

    this.plugin = new FlightClientEntryPlugin({
      ...options,
      builtinAppLoader: !!process.env.BUILTIN_SWC_LOADER,
      shouldInvalidateCb: ({
        bundlePath,
        entryName,
        absolutePagePath,
        clientBrowserLoader,
      }: any) => {
        let shouldInvalidate = false
        const compiler = this.compiler!

        const entries = getEntries(compiler.outputPath)
        const pageKey = getEntryKey(
          COMPILER_NAMES.client,
          PAGE_TYPES.APP,
          bundlePath
        )

        if (!entries[pageKey]) {
          entries[pageKey] = {
            type: EntryTypes.CHILD_ENTRY,
            parentEntries: new Set([entryName]),
            absoluteEntryFilePath: absolutePagePath,
            bundlePath,
            request: clientBrowserLoader,
            dispose: false,
            lastActiveTime: Date.now(),
          }
          shouldInvalidate = true
        } else {
          const entryData = entries[pageKey]
          // New version of the client loader
          if (entryData.request !== clientBrowserLoader) {
            entryData.request = clientBrowserLoader
            shouldInvalidate = true
          }
          if (entryData.type === EntryTypes.CHILD_ENTRY) {
            entryData.parentEntries.add(entryName)
          }
          entryData.dispose = false
          entryData.lastActiveTime = Date.now()
        }

        return shouldInvalidate
      },
      invalidateCb: () => {
        const compiler = this.compiler!

        // Invalidate in development to trigger recompilation
        const invalidator = getInvalidator(compiler.outputPath)
        // Check if any of the entry injections need an invalidation
        if (invalidator) {
          invalidator.invalidate([COMPILER_NAMES.client])
        }
      },
      stateCb: (state: any) => {
        Object.assign(pluginState.serverActions, state.serverActions)
        Object.assign(pluginState.edgeServerActions, state.edgeServerActions)
        Object.assign(
          pluginState.serverActionModules,
          state.serverActionModules
        )
        Object.assign(
          pluginState.edgeServerActionModules,
          state.edgeServerActionModules
        )
        Object.assign(pluginState.ssrModules, state.ssrModules)
        Object.assign(pluginState.edgeSsrModules, state.edgeSsrModules)
        Object.assign(pluginState.rscModules, state.rscModules)
        Object.assign(pluginState.edgeRscModules, state.edgeRscModules)
        Object.assign(
          pluginState.injectedClientEntries,
          state.injectedClientEntries
        )
      },
    })
  }

  apply(compiler: Compiler) {
    this.compiler = compiler
    this.plugin.apply(compiler)
  }
}