Skip to content

Commit

Permalink
fix: check compatibility granularly
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed May 8, 2024
1 parent efaa81b commit 48c80c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 23 additions & 5 deletions packages/kit/src/compatibility.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import satisfies from 'semver/functions/satisfies.js' // npm/node-semver#381
import { readPackageJSON } from 'pkg-types'
import type { Nuxt, NuxtCompatibility, NuxtCompatibilityIssues } from '@nuxt/schema'
import { useNuxt } from './context'

export function normalizeSemanticVersion (version: string) {
return version.replace(/-[0-9]+\.[0-9a-f]+/, '') // Remove edge prefix
}

const builderMap = {
'@nuxt/vite-builder': 'vite',
'@nuxt/webpack-builder': 'webpack',
}

/**
* Check version constraints and return incompatibility issues as an array
*/
Expand Down Expand Up @@ -38,16 +44,28 @@ export async function checkNuxtCompatibility (constraints: NuxtCompatibility, nu
message: 'Nuxt bridge is not supported',
})
}
}

// Builder compatibility check
if (constraints.builder && constraints.builder !== 'all') {
if ((constraints.builder === 'vite' && nuxt.options.builder !== '@nuxt/vite-builder') ||
(constraints.builder === 'webpack' && nuxt.options.builder !== '@nuxt/webpack-builder')) {
// Builder compatibility check
if (constraints.builder && typeof nuxt.options.builder === 'string') {
const currentBuilder = builderMap[nuxt.options.builder] || nuxt.options.builder
if (currentBuilder in constraints.builder) {
const constraint = constraints.builder[currentBuilder]!
if (constraint === false) {
issues.push({
name: 'builder',
message: `Using compatible builder ${nuxt.options.builder}`
message: `Not compatible with \`${nuxt.options.builder}\`.`
})
}
else {
const builderVersion = await readPackageJSON(nuxt.options.builder, { url: nuxt.options.modulesDir }).then(r => r.version).catch(() => undefined)
if (builderVersion && !satisfies(normalizeSemanticVersion(builderVersion), constraint, { includePrerelease: true })) {
issues.push({
name: 'builder',
message: `Not compatible with \`${builderVersion}\` of \`${currentBuilder}\`. This module requires \`${constraint}\`.`
})
}
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions packages/schema/src/types/compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ export interface NuxtCompatibility {
* - `webpack`: compatible with `@nuxt/webpack-builder` only
* - `all`: compatible with all builders
*/
builder?: NuxtBuilders
builder?: Partial<Record<'vite' | 'webpack' | (string & {}), false | string>>
}

export type NuxtBuilders = 'vite' | 'webpack' | 'all'

export interface NuxtCompatibilityIssue {
name: string
message: string
Expand Down

0 comments on commit 48c80c1

Please sign in to comment.