mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
feat(nuxt): auto-install optional features on StackBlitz (#23607)
This commit is contained in:
parent
914fbd8883
commit
fc7b131bf7
@ -1,10 +1,21 @@
|
|||||||
import { addDependency } from 'nypm'
|
import { addDependency } from 'nypm'
|
||||||
import { resolvePackageJSON } from 'pkg-types'
|
import { resolvePackageJSON } from 'pkg-types'
|
||||||
import { logger } from '@nuxt/kit'
|
import { logger } from '@nuxt/kit'
|
||||||
import { isCI } from 'std-env'
|
import { isCI, provider } from 'std-env'
|
||||||
|
|
||||||
export async function ensurePackageInstalled (rootDir: string, name: string, searchPaths?: string[]) {
|
const isStackblitz = provider === 'stackblitz'
|
||||||
if (await resolvePackageJSON(name, { url: searchPaths }).catch(() => null)) {
|
|
||||||
|
export interface EnsurePackageInstalledOptions {
|
||||||
|
rootDir: string
|
||||||
|
searchPaths?: string[]
|
||||||
|
prompt?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function ensurePackageInstalled (
|
||||||
|
name: string,
|
||||||
|
options: EnsurePackageInstalledOptions
|
||||||
|
) {
|
||||||
|
if (await resolvePackageJSON(name, { url: options.searchPaths }).catch(() => null)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -13,6 +24,8 @@ export async function ensurePackageInstalled (rootDir: string, name: string, sea
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// In StackBlitz we install packages automatically by default
|
||||||
|
if (options.prompt === true || (options.prompt !== false && !isStackblitz)) {
|
||||||
const confirm = await logger.prompt(`Do you want to install ${name} package?`, {
|
const confirm = await logger.prompt(`Do you want to install ${name} package?`, {
|
||||||
type: 'confirm',
|
type: 'confirm',
|
||||||
name: 'confirm',
|
name: 'confirm',
|
||||||
@ -22,11 +35,12 @@ export async function ensurePackageInstalled (rootDir: string, name: string, sea
|
|||||||
if (!confirm) {
|
if (!confirm) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
logger.info(`Installing ${name}...`)
|
logger.info(`Installing ${name}...`)
|
||||||
try {
|
try {
|
||||||
await addDependency(name, {
|
await addDependency(name, {
|
||||||
cwd: rootDir,
|
cwd: options.rootDir,
|
||||||
dev: true
|
dev: true
|
||||||
})
|
})
|
||||||
logger.success(`Installed ${name}`)
|
logger.success(`Installed ${name}`)
|
||||||
|
@ -443,7 +443,10 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
|||||||
|
|
||||||
// Nuxt DevTools is currently opt-in
|
// Nuxt DevTools is currently opt-in
|
||||||
if (options.devtools === true || (options.devtools && options.devtools.enabled !== false)) {
|
if (options.devtools === true || (options.devtools && options.devtools.enabled !== false)) {
|
||||||
if (await import('./features').then(r => r.ensurePackageInstalled(options.rootDir, '@nuxt/devtools', options.modulesDir))) {
|
if (await import('./features').then(r => r.ensurePackageInstalled('@nuxt/devtools', {
|
||||||
|
rootDir: options.rootDir,
|
||||||
|
searchPaths: options.modulesDir
|
||||||
|
}))) {
|
||||||
options._modules.push('@nuxt/devtools')
|
options._modules.push('@nuxt/devtools')
|
||||||
} else {
|
} else {
|
||||||
logger.warn('Failed to install `@nuxt/devtools`, please install it manually, or disable `devtools` in `nuxt.config`')
|
logger.warn('Failed to install `@nuxt/devtools`, please install it manually, or disable `devtools` in `nuxt.config`')
|
||||||
@ -452,7 +455,10 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
|||||||
|
|
||||||
// Nuxt Webpack Builder is currently opt-in
|
// Nuxt Webpack Builder is currently opt-in
|
||||||
if (options.builder === '@nuxt/webpack-builder') {
|
if (options.builder === '@nuxt/webpack-builder') {
|
||||||
if (!await import('./features').then(r => r.ensurePackageInstalled(options.rootDir, '@nuxt/webpack-builder', options.modulesDir))) {
|
if (!await import('./features').then(r => r.ensurePackageInstalled('@nuxt/webpack-builder', {
|
||||||
|
rootDir: options.rootDir,
|
||||||
|
searchPaths: options.modulesDir
|
||||||
|
}))) {
|
||||||
logger.warn('Failed to install `@nuxt/webpack-builder`, please install it manually, or change the `builder` option to vite in `nuxt.config`')
|
logger.warn('Failed to install `@nuxt/webpack-builder`, please install it manually, or change the `builder` option to vite in `nuxt.config`')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user