forked from vercel/next.js
/
functions-manifest-plugin.ts
138 lines (127 loc) · 4.31 KB
/
functions-manifest-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
import { sources, webpack5 } from 'next/dist/compiled/webpack/webpack'
import { normalizePagePath } from '../../../server/normalize-page-path'
import { FUNCTIONS_MANIFEST } from '../../../shared/lib/constants'
import { getPageFromPath } from '../../entries'
import { collectAssets, getEntrypointInfo } from './middleware-plugin'
const PLUGIN_NAME = 'FunctionsManifestPlugin'
export interface FunctionsManifest {
version: 1
pages: {
[page: string]: {
runtime?: string
env: string[]
files: string[]
name: string
page: string
regexp: string
}
}
}
export default class FunctionsManifestPlugin {
dev: boolean
pagesDir: string
pageExtensions: string[]
webServerRuntime: boolean
pagesRuntime: Map<string, string>
constructor({
dev,
pagesDir,
pageExtensions,
webServerRuntime,
}: {
dev: boolean
pagesDir: string
pageExtensions: string[]
webServerRuntime: boolean
}) {
this.dev = dev
this.pagesDir = pagesDir
this.webServerRuntime = webServerRuntime
this.pageExtensions = pageExtensions
this.pagesRuntime = new Map()
}
createAssets(
compilation: webpack5.Compilation,
assets: any,
envPerRoute: Map<string, string[]>,
webServerRuntime: boolean
) {
const functionsManifest: FunctionsManifest = {
version: 1,
pages: {},
}
const infos = getEntrypointInfo(compilation, envPerRoute, webServerRuntime)
infos.forEach((info) => {
const { page } = info
// TODO: use global default runtime instead of 'web'
const pageRuntime = this.pagesRuntime.get(page)
const isWebRuntime =
pageRuntime === 'edge' || (this.webServerRuntime && !pageRuntime)
functionsManifest.pages[page] = {
// Not assign if it's nodejs runtime, project configured node version is used instead
...(isWebRuntime && { runtime: 'web' }),
...info,
}
})
const assetPath =
(this.webServerRuntime ? '' : 'server/') + FUNCTIONS_MANIFEST
assets[assetPath] = new sources.RawSource(
JSON.stringify(functionsManifest, null, 2)
)
}
apply(compiler: webpack5.Compiler) {
const handler = (parser: webpack5.javascript.JavascriptParser) => {
parser.hooks.exportSpecifier.tap(
PLUGIN_NAME,
(statement: any, _identifierName: string, exportName: string) => {
const { declaration } = statement
if (exportName === 'config') {
const varDecl = declaration.declarations[0]
const init = varDecl.init.properties[0]
const runtime = init.value.value
// @ts-ignore buildInfo exists on Module
parser.state.module.buildInfo.NEXT_runtime = runtime
}
}
)
}
compiler.hooks.compilation.tap(
PLUGIN_NAME,
(
compilation: webpack5.Compilation,
{ normalModuleFactory: factory }: any
) => {
factory.hooks.parser.for('javascript/auto').tap(PLUGIN_NAME, handler)
factory.hooks.parser.for('javascript/esm').tap(PLUGIN_NAME, handler)
compilation.hooks.seal.tap(PLUGIN_NAME, () => {
for (const entryData of compilation.entries.values()) {
for (const dependency of entryData.dependencies) {
// @ts-ignore TODO: webpack 5 types
const module = compilation.moduleGraph.getModule(dependency)
const outgoingConnections =
compilation.moduleGraph.getOutgoingConnectionsByModule(module)
if (!outgoingConnections) return
const entryModules = outgoingConnections.keys()
for (const mod of entryModules) {
const runtime = mod?.buildInfo?.NEXT_runtime
if (runtime) {
// @ts-ignore: TODO: webpack 5 types
const normalizedPagePath = normalizePagePath(mod.userRequest)
const pagePath = normalizedPagePath.replace(this.pagesDir, '')
const page = getPageFromPath(pagePath, this.pageExtensions)
this.pagesRuntime.set(page, runtime)
break
}
}
}
}
})
}
)
collectAssets(compiler, this.createAssets.bind(this), {
dev: this.dev,
pluginName: PLUGIN_NAME,
webServerRuntime: this.webServerRuntime,
})
}
}