fix(kit,nuxt,schema)!: remove other support for nuxt2/bridge (#28936)

This commit is contained in:
Daniel Roe 2024-09-11 11:02:22 +01:00 committed by GitHub
parent 2f0a28d47e
commit a0ea790b80
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 4 additions and 49 deletions

View File

@ -6,8 +6,6 @@ import { logger } from './logger'
/** /**
* Register a directory to be scanned for components and imported only when used. * Register a directory to be scanned for components and imported only when used.
*
* Requires Nuxt 2.13+
*/ */
export async function addComponentsDir (dir: ComponentsDir, opts: { prepend?: boolean } = {}) { export async function addComponentsDir (dir: ComponentsDir, opts: { prepend?: boolean } = {}) {
const nuxt = useNuxt() const nuxt = useNuxt()
@ -23,8 +21,6 @@ export type AddComponentOptions = { name: string, filePath: string } & Partial<E
/** /**
* Register a component by its name and filePath. * Register a component by its name and filePath.
*
* Requires Nuxt 2.13+
*/ */
export async function addComponent (opts: AddComponentOptions) { export async function addComponent (opts: AddComponentOptions) {
const nuxt = useNuxt() const nuxt = useNuxt()

View File

@ -1,20 +1,15 @@
import type { Import } from 'unimport' import type { Import } from 'unimport'
import type { ImportPresetWithDeprecation } from '@nuxt/schema' import type { ImportPresetWithDeprecation } from '@nuxt/schema'
import { useNuxt } from './context' import { useNuxt } from './context'
import { assertNuxtCompatibility } from './compatibility'
import { toArray } from './utils' import { toArray } from './utils'
export function addImports (imports: Import | Import[]) { export function addImports (imports: Import | Import[]) {
assertNuxtCompatibility({ bridge: true })
useNuxt().hook('imports:extend', (_imports) => { useNuxt().hook('imports:extend', (_imports) => {
_imports.push(...toArray(imports)) _imports.push(...toArray(imports))
}) })
} }
export function addImportsDir (dirs: string | string[], opts: { prepend?: boolean } = {}) { export function addImportsDir (dirs: string | string[], opts: { prepend?: boolean } = {}) {
assertNuxtCompatibility({ bridge: true })
useNuxt().hook('imports:dirs', (_dirs: string[]) => { useNuxt().hook('imports:dirs', (_dirs: string[]) => {
for (const dir of toArray(dirs)) { for (const dir of toArray(dirs)) {
_dirs[opts.prepend ? 'unshift' : 'push'](dir) _dirs[opts.prepend ? 'unshift' : 'push'](dir)
@ -22,8 +17,6 @@ export function addImportsDir (dirs: string | string[], opts: { prepend?: boolea
}) })
} }
export function addImportsSources (presets: ImportPresetWithDeprecation | ImportPresetWithDeprecation[]) { export function addImportsSources (presets: ImportPresetWithDeprecation | ImportPresetWithDeprecation[]) {
assertNuxtCompatibility({ bridge: true })
useNuxt().hook('imports:sources', (_presets: ImportPresetWithDeprecation[]) => { useNuxt().hook('imports:sources', (_presets: ImportPresetWithDeprecation[]) => {
for (const preset of toArray(presets)) { for (const preset of toArray(presets)) {
_presets.push(preset) _presets.push(preset)

View File

@ -5,7 +5,7 @@ import { useNuxt } from './context'
import { logger } from './logger' import { logger } from './logger'
import { addTemplate } from './template' import { addTemplate } from './template'
export function addLayout (this: any, template: NuxtTemplate | string, name?: string) { export function addLayout (template: NuxtTemplate | string, name?: string) {
const nuxt = useNuxt() const nuxt = useNuxt()
const { filename, src } = addTemplate(template) const { filename, src } = addTemplate(template)
const layoutName = kebabCase(name || parse(filename).name).replace(/["']/g, '') const layoutName = kebabCase(name || parse(filename).name).replace(/["']/g, '')

View File

@ -79,9 +79,9 @@ function _defineNuxtModule<
} }
// Module format is always a simple function // Module format is always a simple function
async function normalizedModule (this: any, inlineOptions: Partial<TOptions>, nuxt: Nuxt): Promise<ModuleSetupReturn> { async function normalizedModule (inlineOptions: Partial<TOptions>, nuxt = tryUseNuxt()!): Promise<ModuleSetupReturn> {
if (!nuxt) { if (!nuxt) {
nuxt = tryUseNuxt() || this.nuxt /* invoked by nuxt 2 */ throw new TypeError('Cannot use module outside of Nuxt context')
} }
// Avoid duplicate installs // Avoid duplicate installs

View File

@ -320,11 +320,6 @@ export async function writeTypes (nuxt: Nuxt) {
await fsp.writeFile(declarationPath, GeneratedBy + '\n' + declaration) await fsp.writeFile(declarationPath, GeneratedBy + '\n' + declaration)
} }
// This is needed for Nuxt 2 which clears the build directory again before building
// https://github.com/nuxt/nuxt/blob/2.x/packages/builder/src/builder.js#L144
// @ts-expect-error TODO: Nuxt 2 hook
nuxt.hook('builder:prepared', writeFile)
await writeFile() await writeFile()
} }

View File

@ -149,29 +149,12 @@ export const RemovePluginMetadataPlugin = (nuxt: Nuxt) => createUnplugin(() => {
if (_node.type === 'ImportSpecifier' && (_node.imported.name === 'defineNuxtPlugin' || _node.imported.name === 'definePayloadPlugin')) { if (_node.type === 'ImportSpecifier' && (_node.imported.name === 'defineNuxtPlugin' || _node.imported.name === 'definePayloadPlugin')) {
wrapperNames.add(_node.local.name) wrapperNames.add(_node.local.name)
} }
if (_node.type === 'ExportDefaultDeclaration' && (_node.declaration.type === 'FunctionDeclaration' || _node.declaration.type === 'ArrowFunctionExpression')) {
if ('params' in _node.declaration && _node.declaration.params.length > 1) {
logger.warn(`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 } if (_node.type !== 'CallExpression' || (_node as CallExpression).callee.type !== 'Identifier') { return }
const node = _node as CallExpression & { start: number, end: number } const node = _node as CallExpression & { start: number, end: number }
const name = 'name' in node.callee && node.callee.name const name = 'name' in node.callee && node.callee.name
if (!name || !wrapperNames.has(name)) { return } if (!name || !wrapperNames.has(name)) { return }
wrapped = true 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) {
logger.warn(`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 // Remove metadata that already has been extracted
if (!('order' in plugin) && !('name' in plugin)) { return } if (!('order' in plugin) && !('name' in plugin)) { return }
for (const [argIndex, _arg] of node.arguments.entries()) { for (const [argIndex, _arg] of node.arguments.entries()) {

View File

@ -40,7 +40,6 @@ describe('plugin-metadata', () => {
it('should overwrite invalid plugins', () => { it('should overwrite invalid plugins', () => {
const invalidPlugins = [ const invalidPlugins = [
'export const plugin = {}', 'export const plugin = {}',
'export default function (ctx, inject) {}',
] ]
for (const plugin of invalidPlugins) { for (const plugin of invalidPlugins) {
expect(transformPlugin.transform.call({ parse }, plugin, 'my-plugin.mjs').code).toBe('export default () => {}') expect(transformPlugin.transform.call({ parse }, plugin, 'my-plugin.mjs').code).toBe('export default () => {}')

View File

@ -1,18 +1,10 @@
export interface NuxtCompatibility { export interface NuxtCompatibility {
/** /**
* Required nuxt version in semver format. * Required nuxt version in semver format.
* @example `^2.14.0` or `>=3.0.0-27219851.6e49637`. * @example `^3.2.0` or `>=3.13.0`.
*/ */
nuxt?: string nuxt?: string
/**
* Bridge constraint for Nuxt 2 support.
*
* - `true`: When using Nuxt 2, using bridge module is required.
* - `false`: When using Nuxt 2, using bridge module is not supported.
*/
bridge?: boolean
/** /**
* Mark a builder as incompatible, or require a particular version. * Mark a builder as incompatible, or require a particular version.
* *

View File

@ -1,3 +0,0 @@
export default function previousPlugin (one, inject) {
inject()
}