/
plugin-metadata.ts
189 lines (171 loc) · 7.23 KB
/
plugin-metadata.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
178
179
180
181
182
183
184
185
186
187
188
189
import type { CallExpression, Property, SpreadElement } from 'estree'
import type { Node } from 'estree-walker'
import { walk } from 'estree-walker'
import { parse } from 'typescript-estree'
import { defu } from 'defu'
import { findExports } from 'mlly'
import type { Nuxt } from '@nuxt/schema'
import { createUnplugin } from 'unplugin'
import MagicString from 'magic-string'
import { normalize } from 'pathe'
// eslint-disable-next-line import/no-restricted-paths
import type { ObjectPlugin, PluginMeta } from '#app'
const internalOrderMap = {
// -50: pre-all (nuxt)
'nuxt-pre-all': -50,
// -40: custom payload revivers (user)
'user-revivers': -40,
// -30: payload reviving (nuxt)
'nuxt-revivers': -30,
// -20: pre (user) <-- pre mapped to this
'user-pre': -20,
// -10: default (nuxt)
'nuxt-default': -10,
// 0: default (user) <-- default behavior
'user-default': 0,
// +10: post (nuxt)
'nuxt-post': 10,
// +20: post (user) <-- post mapped to this
'user-post': 20,
// +30: post-all (nuxt)
'nuxt-post-all': 30
}
export const orderMap: Record<NonNullable<ObjectPlugin['enforce']>, number> = {
pre: internalOrderMap['user-pre'],
default: internalOrderMap['user-default'],
post: internalOrderMap['user-post']
}
const metaCache: Record<string, Omit<PluginMeta, 'enforce'>> = {}
export function extractMetadata (code: string) {
let meta: PluginMeta = {}
if (metaCache[code]) {
return metaCache[code]
}
walk(parse(code) as Node, {
enter (_node) {
if (_node.type !== 'CallExpression' || (_node as CallExpression).callee.type !== 'Identifier') { return }
const node = _node as CallExpression & { start: number, end: number }
const name = 'name' in node.callee && node.callee.name
if (name !== 'defineNuxtPlugin' && name !== 'definePayloadPlugin') { return }
if (name === 'definePayloadPlugin') {
meta.order = internalOrderMap['user-revivers']
}
const metaArg = node.arguments[1]
if (metaArg) {
if (metaArg.type !== 'ObjectExpression') {
throw new Error('Invalid plugin metadata')
}
meta = extractMetaFromObject(metaArg.properties)
}
const plugin = node.arguments[0]
if (plugin.type === 'ObjectExpression') {
meta = defu(extractMetaFromObject(plugin.properties), meta)
}
meta.order = meta.order || orderMap[meta.enforce || 'default'] || orderMap.default
delete meta.enforce
}
})
metaCache[code] = meta
return meta as Omit<PluginMeta, 'enforce'>
}
type PluginMetaKey = keyof PluginMeta
const keys: Record<PluginMetaKey, string> = {
name: 'name',
order: 'order',
enforce: 'enforce'
}
function isMetadataKey (key: string): key is PluginMetaKey {
return key in keys
}
function extractMetaFromObject (properties: Array<Property | SpreadElement>) {
const meta: PluginMeta = {}
for (const property of properties) {
if (property.type === 'SpreadElement' || !('name' in property.key)) {
throw new Error('Invalid plugin metadata')
}
const propertyKey = property.key.name
if (!isMetadataKey(propertyKey)) { continue }
if (property.value.type === 'Literal') {
meta[propertyKey] = property.value.value as any
}
if (property.value.type === 'UnaryExpression' && property.value.argument.type === 'Literal') {
meta[propertyKey] = JSON.parse(property.value.operator + property.value.argument.raw!)
}
}
return meta
}
export const RemovePluginMetadataPlugin = (nuxt: Nuxt) => createUnplugin(() => {
return {
name: 'nuxt:remove-plugin-metadata',
enforce: 'pre',
transform (code, id) {
id = normalize(id)
const plugin = nuxt.apps.default.plugins.find(p => p.src === id)
if (!plugin) { return }
const s = new MagicString(code)
const exports = findExports(code)
const defaultExport = exports.find(e => e.type === 'default' || e.name === 'default')
if (!defaultExport) {
console.error(`[warn] [nuxt] Plugin \`${plugin.src}\` has no default export and will be ignored at build time. Add \`export default defineNuxtPlugin(() => {})\` to your plugin.`)
s.overwrite(0, code.length, 'export default () => {}')
return {
code: s.toString(),
map: nuxt.options.sourcemap.client || nuxt.options.sourcemap.server ? s.generateMap({ hires: true }) : null
}
}
let wrapped = false
try {
walk(parse(code) as Node, {
enter (_node) {
if (_node.type === 'ExportDefaultDeclaration' && (_node.declaration.type === 'FunctionDeclaration' || _node.declaration.type === 'ArrowFunctionExpression')) {
if ('params' in _node.declaration && _node.declaration.params.length > 1) {
console.warn(`[warn] [nuxt] Plugin \`${plugin.src}\` is in legacy Nuxt 2 format (context, inject) which is likely to be broken and will be ignored.`)
s.overwrite(0, code.length, 'export default () => {}')
wrapped = true // silence a duplicate error
return
}
}
if (_node.type !== 'CallExpression' || (_node as CallExpression).callee.type !== 'Identifier') { return }
const node = _node as CallExpression & { start: number, end: number }
const name = 'name' in node.callee && node.callee.name
if (name !== 'defineNuxtPlugin' && name !== 'definePayloadPlugin') { return }
wrapped = true
if (node.arguments[0].type !== 'ObjectExpression') {
// TODO: Warn if legacy plugin format is detected
if ('params' in node.arguments[0] && node.arguments[0].params.length > 1) {
console.warn(`[warn] [nuxt] Plugin \`${plugin.src}\` is in legacy Nuxt 2 format (context, inject) which is likely to be broken and will be ignored.`)
s.overwrite(0, code.length, 'export default () => {}')
return
}
}
// Remove metadata that already has been extracted
if (!('order' in plugin) && !('name' in plugin)) { return }
for (const [argIndex, arg] of node.arguments.entries()) {
if (arg.type !== 'ObjectExpression') { continue }
for (const [propertyIndex, property] of arg.properties.entries()) {
if (property.type === 'SpreadElement' || !('name' in property.key)) { continue }
const propertyKey = property.key.name
if (propertyKey === 'order' || propertyKey === 'enforce' || propertyKey === 'name') {
const nextIndex = arg.properties[propertyIndex + 1]?.range?.[0] || node.arguments[argIndex + 1]?.range?.[0] || (arg.range![1] - 1)
s.remove(property.range![0], nextIndex)
}
}
}
}
})
} catch (e) {
console.error(e)
return
}
if (!wrapped) {
console.warn(`[warn] [nuxt] Plugin \`${plugin.src}\` is not wrapped in \`defineNuxtPlugin\`. It is advised to wrap your plugins as in the future this may enable enhancements.`)
}
if (s.hasChanged()) {
return {
code: s.toString(),
map: nuxt.options.sourcemap.client || nuxt.options.sourcemap.server ? s.generateMap({ hires: true }) : null
}
}
}
}
})