This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxi): add
nuxi preview
command for local testing (#2162)
Co-authored-by: pooya parsa <pyapar@gmail.com>
- Loading branch information
Showing
11 changed files
with
128 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
import consola from 'consola' | ||
import { extendPreset, hl, prettyPath } from '../utils' | ||
import { NitroPreset, NitroContext } from '../context' | ||
import { extendPreset } from '../utils' | ||
import { NitroPreset } from '../context' | ||
import { node } from './node' | ||
|
||
export const server: NitroPreset = extendPreset(node, { | ||
entry: '{{ _internal.runtimeDir }}/entries/server', | ||
serveStatic: true, | ||
hooks: { | ||
'nitro:compiled' ({ output }: NitroContext) { | ||
consola.success('Ready to run', hl('node ' + prettyPath(output.serverDir) + '/index.mjs')) | ||
} | ||
commands: { | ||
preview: 'node {{ output.serverDir }}/index.mjs' | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { existsSync, promises as fsp } from 'fs' | ||
import { dirname, relative } from 'path' | ||
import { execa } from 'execa' | ||
import { resolve } from 'pathe' | ||
import consola from 'consola' | ||
|
||
import { defineNuxtCommand } from './index' | ||
|
||
export default defineNuxtCommand({ | ||
meta: { | ||
name: 'preview', | ||
usage: 'npx nuxi preview|start [rootDir]', | ||
description: 'Launches nitro server for local testing after `nuxi build`.' | ||
}, | ||
async invoke (args) { | ||
process.env.NODE_ENV = process.env.NODE_ENV || 'production' | ||
const rootDir = resolve(args._[0] || '.') | ||
|
||
const nitroJSONPaths = ['.output/nitro.json', 'nitro.json'].map(p => resolve(rootDir, p)) | ||
const nitroJSONPath = nitroJSONPaths.find(p => existsSync(p)) | ||
if (!nitroJSONPath) { | ||
consola.error('Cannot find `nitro.json`. Did you run `nuxi build` first? Search path:\n', nitroJSONPaths) | ||
process.exit(1) | ||
} | ||
const outputPath = dirname(nitroJSONPath) | ||
const nitroJSON = JSON.parse(await fsp.readFile(nitroJSONPath, 'utf-8')) | ||
|
||
consola.info('Node.js version:', process.versions.node) | ||
consola.info('Preset:', nitroJSON.preset) | ||
consola.info('Working dir:', relative(process.cwd(), outputPath)) | ||
|
||
if (!nitroJSON.commands.preview) { | ||
consola.error('Preview is not supported for this build.') | ||
process.exit(1) | ||
} | ||
|
||
consola.info('Starting preview command:', nitroJSON.commands.preview) | ||
const [command, ...commandArgs] = nitroJSON.commands.preview.split(' ') | ||
consola.log('') | ||
await execa(command, commandArgs, { stdio: 'inherit', cwd: outputPath }) | ||
} | ||
}) |