/
flight-types-plugin.ts
170 lines (150 loc) · 5.21 KB
/
flight-types-plugin.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
import path from 'path'
import { promises as fs } from 'fs'
import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import { WEBPACK_LAYERS } from '../../../lib/constants'
const PLUGIN_NAME = 'FlightTypesPlugin'
interface Options {
dir: string
appDir: string
dev: boolean
isEdgeServer: boolean
}
function createTypeGuardFile(
fullPath: string,
relativePath: string,
options: {
type: 'layout' | 'page'
slots?: string[]
}
) {
return `// File: ${fullPath}
import * as entry from '${relativePath}'
type TEntry = typeof entry
check<IEntry, TEntry>(entry)
type PageParams = Record<string, string>
interface PageProps {
params: any
searchParams?: any
}
interface LayoutProps {
children: React.ReactNode
${
options.slots
? options.slots.map((slot) => ` ${slot}: React.ReactNode`).join('\n')
: ''
}
params: any
}
export type PageComponent = (props: PageProps) => React.ReactNode | Promise<React.ReactNode>
export type LayoutComponent = (props: LayoutProps) => React.ReactNode | Promise<React.ReactNode>
interface IEntry {
${
options.type === 'layout'
? `default: LayoutComponent`
: `default: PageComponent`
}
config?: {}
generateStaticParams?: (params?: PageParams) => any[] | Promise<any[]>
revalidate?: RevalidateRange<TEntry> | false
dynamic?: 'auto' | 'force-dynamic' | 'error' | 'force-static'
dynamicParams?: boolean
fetchCache?: 'auto' | 'force-no-store' | 'only-no-store' | 'default-no-store' | 'default-cache' | 'only-cache' | 'force-cache'
preferredRegion?: 'auto' | 'home' | 'edge'
${options.type === 'page' ? "runtime?: 'nodejs' | 'experimental-edge'" : ''}
}
// =============
// Utility types
type RevalidateRange<T> = T extends { revalidate: any } ? NonNegative<T['revalidate']> : never
type Impossible<K extends keyof any> = { [P in K]: never }
function check<Base, T extends Base>(_mod: T & Impossible<Exclude<keyof T, keyof Base>>): void {}
// https://github.com/sindresorhus/type-fest
type Numeric = number | bigint
type Zero = 0 | 0n
type Negative<T extends Numeric> = T extends Zero ? never : \`\${T}\` extends \`-\${string}\` ? T : never
type NonNegative<T extends Numeric> = T extends Zero ? T : Negative<T> extends never ? T : '__invalid_negative_number__'
`
}
async function collectNamedSlots(layoutPath: string) {
const layoutDir = path.dirname(layoutPath)
const items = await fs.readdir(layoutDir, { withFileTypes: true })
const slots = []
for (const item of items) {
if (item.isDirectory() && item.name.startsWith('@')) {
slots.push(item.name.slice(1))
}
}
return slots
}
export class FlightTypesPlugin {
dir: string
appDir: string
dev: boolean
isEdgeServer: boolean
constructor(options: Options) {
this.dir = options.dir
this.appDir = options.appDir
this.dev = options.dev
this.isEdgeServer = options.isEdgeServer
}
apply(compiler: webpack.Compiler) {
const assetPrefix = this.dev ? '..' : this.isEdgeServer ? '..' : '../..'
const handleModule = async (_mod: webpack.Module, assets: any) => {
if (_mod.layer !== WEBPACK_LAYERS.server) return
const mod: webpack.NormalModule = _mod as any
if (!mod.resource) return
if (!mod.resource.startsWith(this.appDir + path.sep)) return
if (!/\.(js|jsx|ts|tsx|mjs)$/.test(mod.resource)) return
const IS_LAYOUT = /[/\\]layout\.[^./\\]+$/.test(mod.resource)
const IS_PAGE = !IS_LAYOUT && /[/\\]page\.[^.]+$/.test(mod.resource)
const relativePathToApp = path.relative(this.appDir, mod.resource)
const relativePathToRoot = path.relative(this.dir, mod.resource)
const typePath = path.join(
'types',
'app',
relativePathToApp.replace(/\.(js|jsx|ts|tsx|mjs)$/, '.ts')
)
const relativeImportPath = path
.join(
path.relative(typePath, ''),
relativePathToRoot.replace(/\.(js|jsx|ts|tsx|mjs)$/, '')
)
.replace(/\\/g, '/')
const assetPath = assetPrefix + '/' + typePath.replace(/\\/g, '/')
if (IS_LAYOUT) {
const slots = await collectNamedSlots(mod.resource)
assets[assetPath] = new sources.RawSource(
createTypeGuardFile(mod.resource, relativeImportPath, {
type: 'layout',
slots,
})
) as unknown as webpack.sources.RawSource
} else if (IS_PAGE) {
assets[assetPath] = new sources.RawSource(
createTypeGuardFile(mod.resource, relativeImportPath, {
type: 'page',
})
) as unknown as webpack.sources.RawSource
}
}
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.processAssets.tapAsync(
{
name: PLUGIN_NAME,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH,
},
async (assets, callback) => {
const promises: Promise<any>[] = []
for (const entrypoint of compilation.entrypoints.values()) {
for (const chunk of entrypoint.chunks) {
compilation.chunkGraph.getChunkModules(chunk).forEach((mod) => {
promises.push(handleModule(mod, assets))
})
}
}
await Promise.all(promises)
callback()
}
)
})
}
}