/
next-flight-client-loader.ts
177 lines (159 loc) · 4.74 KB
/
next-flight-client-loader.ts
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { promisify } from 'util'
import { parse } from '../../swc'
import { buildExports, isNextBuiltinClientComponent } from './utils'
function addExportNames(names: string[], node: any) {
if (!node) return
switch (node.type) {
case 'Identifier':
names.push(node.value)
return
case 'ObjectPattern':
for (let i = 0; i < node.properties.length; i++)
addExportNames(names, node.properties[i])
return
case 'ArrayPattern':
for (let i = 0; i < node.elements.length; i++) {
const element = node.elements[i]
if (element) addExportNames(names, element)
}
return
case 'Property':
addExportNames(names, node.value)
return
case 'AssignmentPattern':
addExportNames(names, node.left)
return
case 'RestElement':
addExportNames(names, node.argument)
return
case 'ParenthesizedExpression':
addExportNames(names, node.expression)
return
default:
return
}
}
async function collectExports(
resourcePath: string,
transformedSource: string,
{
resolve,
loadModule,
}: {
resolve: (request: string) => Promise<string>
loadModule: (request: string) => Promise<string>
}
) {
const names: string[] = []
// Next.js built-in client components
if (isNextBuiltinClientComponent(resourcePath)) {
names.push('default')
}
const { body } = await parse(transformedSource, {
filename: resourcePath,
isModule: 'unknown',
})
for (let i = 0; i < body.length; i++) {
const node = body[i]
switch (node.type) {
case 'ExportDefaultExpression':
case 'ExportDefaultDeclaration':
names.push('default')
break
case 'ExportNamedDeclaration':
if (node.declaration) {
if (node.declaration.type === 'VariableDeclaration') {
const declarations = node.declaration.declarations
for (let j = 0; j < declarations.length; j++) {
addExportNames(names, declarations[j].id)
}
} else {
addExportNames(names, node.declaration.id)
}
}
if (node.specifiers) {
const specifiers = node.specifiers
for (let j = 0; j < specifiers.length; j++) {
addExportNames(names, specifiers[j].exported)
}
}
break
case 'ExportDeclaration':
if (node.declaration?.identifier) {
addExportNames(names, node.declaration.identifier)
}
break
case 'ExpressionStatement': {
const {
expression: { left },
} = node
// exports.xxx = xxx
if (
left?.object &&
left.type === 'MemberExpression' &&
left.object.type === 'Identifier' &&
left.object.value === 'exports'
) {
addExportNames(names, left.property)
}
break
}
case 'ExportAllDeclaration':
if (node.exported) {
addExportNames(names, node.exported)
break
}
const reexportedFromResourcePath = await resolve(node.source.value)
const reexportedFromResourceSource = await loadModule(
reexportedFromResourcePath
)
names.push(
...(await collectExports(
reexportedFromResourcePath,
reexportedFromResourceSource,
{ resolve, loadModule }
))
)
continue
default:
break
}
}
return names
}
export default async function transformSource(
this: any,
source: string
): Promise<string> {
const { resourcePath, resolve, loadModule, context } = this
const transformedSource = source
if (typeof transformedSource !== 'string') {
throw new Error('Expected source to have been transformed to a string.')
}
const names = await collectExports(resourcePath, transformedSource, {
resolve: (...args) => promisify(resolve)(context, ...args),
loadModule: promisify(loadModule),
})
const moduleRefDef =
"const MODULE_REFERENCE = Symbol.for('react.module.reference');\n"
const isNextClientBuiltIn = isNextBuiltinClientComponent(resourcePath)
const clientRefsExports = names.reduce((res: any, name) => {
const moduleRef =
'{ $$typeof: MODULE_REFERENCE, filepath: ' +
JSON.stringify(resourcePath) +
', name: ' +
JSON.stringify(name === 'default' && isNextClientBuiltIn ? '' : name) +
' };\n'
res[name] = moduleRef
return res
}, {})
// still generate module references in ESM
const output = moduleRefDef + buildExports(clientRefsExports, true)
return output
}