From 8f7a7ec06ed3037d68162731225aa1db67a6f245 Mon Sep 17 00:00:00 2001 From: Christian Preston Date: Sun, 16 Oct 2022 05:40:03 -0400 Subject: [PATCH] feat(nuxi): `nuxi build-module` command (#7610) Co-authored-by: Pooya Parsa --- docs/content/3.api/5.commands/build-module.md | 16 +++++++++ packages/nuxi/src/commands/build-module.ts | 33 +++++++++++++++++++ packages/nuxi/src/commands/index.ts | 1 + 3 files changed, 50 insertions(+) create mode 100644 docs/content/3.api/5.commands/build-module.md create mode 100644 packages/nuxi/src/commands/build-module.ts diff --git a/docs/content/3.api/5.commands/build-module.md b/docs/content/3.api/5.commands/build-module.md new file mode 100644 index 0000000000..1445a52c49 --- /dev/null +++ b/docs/content/3.api/5.commands/build-module.md @@ -0,0 +1,16 @@ +# `nuxi build-module` + +```{bash} +npx nuxi build-module [--stub] [rootDir] +``` + +The `build-module` command runs `@nuxt/module-builder` to generate `dist` directory within your `rootDir` that contains the full build for your **nuxt-module**. + +Option | Default | Description +-------------------------|-----------------|------------------ +`rootDir` | `.` | The root directory of the module to bundle. +`--stub` | `false` | Stub out your module for development using [jiti](https://github.com/unjs/jiti#jiti). (**note:** This is mainly for development purposes.) + +::alert +This command is only available when you are using `@nuxt/module-builder` to build your module. Please see [this readme](https://github.com/nuxt/module-builder#-nuxt-module-builder) for more information. +:: diff --git a/packages/nuxi/src/commands/build-module.ts b/packages/nuxi/src/commands/build-module.ts new file mode 100644 index 0000000000..43932c8801 --- /dev/null +++ b/packages/nuxi/src/commands/build-module.ts @@ -0,0 +1,33 @@ +import { execa } from 'execa' +import consola from 'consola' +import { resolve } from 'pathe' +import { tryResolveModule } from '../utils/cjs' +import { defineNuxtCommand } from './index' + +const MODULE_BUILDER_PKG = '@nuxt/module-builder' + +export default defineNuxtCommand({ + meta: { + name: 'build-module', + usage: 'npx nuxi build-module [--stub] [rootDir]', + description: `Helper command for using ${MODULE_BUILDER_PKG}` + }, + async invoke (args) { + // Find local installed version + const rootDir = resolve(args._[0] || '.') + const hasLocal = tryResolveModule(`${MODULE_BUILDER_PKG}/package.json`, rootDir) + + const execArgs = Object.entries({ + '--stub': args.stub + }).filter(([, value]) => value).map(([key]) => key) + + let cmd = 'nuxt-module-build' + if (!hasLocal) { + consola.warn(`Cannot find locally installed version of \`${MODULE_BUILDER_PKG}\` (>=0.2.0). Falling back to \`npx ${MODULE_BUILDER_PKG}\``) + cmd = 'npx' + execArgs.unshift(MODULE_BUILDER_PKG) + } + + await execa(cmd, execArgs, { preferLocal: true, stdio: 'inherit', cwd: rootDir }) + } +}) diff --git a/packages/nuxi/src/commands/index.ts b/packages/nuxi/src/commands/index.ts index 4771e7c4da..1efc57bced 100644 --- a/packages/nuxi/src/commands/index.ts +++ b/packages/nuxi/src/commands/index.ts @@ -5,6 +5,7 @@ const _rDefault = (r: any) => r.default || r export const commands = { dev: () => import('./dev').then(_rDefault), build: () => import('./build').then(_rDefault), + 'build-module': () => import('./build-module').then(_rDefault), cleanup: () => import('./cleanup').then(_rDefault), clean: () => import('./cleanup').then(_rDefault), preview: () => import('./preview').then(_rDefault),