(nuxt: Nuxt)
| 114 | } |
| 115 | |
| 116 | export const RemovePluginMetadataPlugin = (nuxt: Nuxt) => createUnplugin(() => { |
| 117 | return { |
| 118 | name: 'nuxt:remove-plugin-metadata', |
| 119 | transform (code, id) { |
| 120 | id = normalize(id) |
| 121 | const plugin = nuxt.apps.default?.plugins.find(p => p.src === id) |
| 122 | if (!plugin) { return } |
| 123 | |
| 124 | if (!code.trim()) { |
| 125 | logger.warn(`Plugin \`${plugin.src}\` has no content.`) |
| 126 | |
| 127 | return { |
| 128 | code: 'export default () => {}', |
| 129 | map: null, |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | const exports = findExports(code) |
| 134 | const defaultExport = exports.find(e => e.type === 'default' || e.name === 'default') |
| 135 | if (!defaultExport) { |
| 136 | logger.warn(`Plugin \`${plugin.src}\` has no default export and will be ignored at build time. Add \`export default defineNuxtPlugin(() => {})\` to your plugin.`) |
| 137 | return { |
| 138 | code: 'export default () => {}', |
| 139 | map: null, |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | const s = new MagicString(code) |
| 144 | let wrapped = false |
| 145 | const wrapperNames = new Set(['defineNuxtPlugin', 'definePayloadPlugin']) |
| 146 | |
| 147 | try { |
| 148 | parseAndWalk(code, id, (node) => { |
| 149 | if (node.type === 'ImportSpecifier' && node.imported.type === 'Identifier' && (node.imported.name === 'defineNuxtPlugin' || node.imported.name === 'definePayloadPlugin')) { |
| 150 | wrapperNames.add(node.local.name) |
| 151 | } |
| 152 | if (node.type !== 'CallExpression' || node.callee.type !== 'Identifier') { return } |
| 153 | |
| 154 | const name = 'name' in node.callee && node.callee.name |
| 155 | if (!name || !wrapperNames.has(name)) { return } |
| 156 | wrapped = true |
| 157 | |
| 158 | // Remove metadata that already has been extracted |
| 159 | if (!('order' in plugin) && !('name' in plugin)) { return } |
| 160 | for (const [argIndex, arg] of node.arguments.entries()) { |
| 161 | if (arg.type !== 'ObjectExpression') { continue } |
| 162 | |
| 163 | for (const [propertyIndex, property] of arg.properties.entries()) { |
| 164 | if (property.type === 'SpreadElement' || !('name' in property.key)) { continue } |
| 165 | |
| 166 | const propertyKey = property.key.name |
| 167 | if (propertyKey === 'order' || propertyKey === 'enforce' || propertyKey === 'name') { |
| 168 | const nextNode = arg.properties[propertyIndex + 1] || node.arguments[argIndex + 1] |
| 169 | const nextIndex = nextNode?.start || (arg.end - 1) |
| 170 | |
| 171 | s.remove(property.start, nextIndex) |
| 172 | } |
| 173 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…