Next.js 源码窥探 之 启动命令 - 小专栏

您所在的位置:网站首页 nextjs中文文档 Next.js 源码窥探 之 启动命令 - 小专栏

Next.js 源码窥探 之 启动命令 - 小专栏

2022-05-21 17:42| 来源: 网络整理| 查看: 265

首先来看看 Next.js 的启动命令有哪些,根据官方文档,一共是有以下 5 个命令:

next next dev next start next build next export

其中,next dev 和 next 是一样的效果。 其源码 ( 版本是 8.x.x ) 的目录如下: 可以看到项目是由 lerna 来管理的,并由 packages/next 和 packages/next-server 两个包组成。关于 lerna:

A tool for managing JavaScript projects with multiple packages

可以自行 Google。再来看看 next 的 bin 目录: 可以看到刚好存在与上面 5 个启动命令对应的 5 个文件。这里值得注意的地方是,我们运行的命令是 next build , 但文件的名字却是 next-build 。

实际上,上面 5 个命令运行的文件都是 next.ts , dev、start、build、export,都只是 next 的参数而已。

接下来直接看源码,看看 next.ts 是怎么处理这些参数的。

```javascript // ......

!/usr/bin/env node

import { join } from 'path' import spawn from 'cross-spawn' import arg from 'arg'

// 检查 react 和 react-dom 是否安装了,没有安装则提示安装并退出 ['react', 'react-dom'].forEach((dependency) => { // ...... })

// 这里定义一些 commands ,咋眼一看,就是那几个启动命令。 const defaultCommand = 'dev' const commands = [ 'build', 'start', 'export', defaultCommand, ];

// 获取命令行参数 const args = arg({ // ...... }, { permissive: true })

// ......

// 这里很关键,查找 subcommand // Check if we are running next or next const foundCommand = args._.find((cmd) => commands.includes(cmd))

// 准备 node 的运行参数 const nodeArguments: string[] = [] if (args['--inspect']) { nodeArguments.push('--inspect') }

// 去掉 subcommand 保存到 command 中,把其余的参数放在 forwardedArgs const command = foundCommand || defaultCommand const forwardedArgs = args._.filter((arg) => arg !== command)

// Make sure the next --help case is covered // 这里是后面提到的 forwardedArgs 之一 if (args['--help']) { forwardedArgs.push('--help') }

// ......

// 这里很关键,根据 subcommand 找到即将要运行的 bin 程序。 const bin = join(__dirname, 'next-' + command)

首先来看看 Next.js 的启动命令有哪些,根据官方文档,一共是有以下 5 个命令:

next next dev next start next build next export

其中,next dev 和 next 是一样的效果。 其源码 ( 版本是 8.x.x ) 的目录如下: 可以看到项目是由 lerna 来管理的,并由 packages/next 和 packages/next-server 两个包组成。关于 lerna:

A tool for managing JavaScript projects with multiple packages

可以自行 Google。再来看看 next 的 bin 目录: 可以看到刚好存在与上面 5 个启动命令对应的 5 个文件。这里值得注意的地方是,我们运行的命令是 next build , 但文件的名字却是 next-build 。

实际上,上面 5 个命令运行的文件都是 next.ts , dev、start、build、export,都只是 next 的参数而已。

接下来直接看源码,看看 next.ts 是怎么处理这些参数的。

```javascript // ......

!/usr/bin/env node

import { join } from 'path' import spawn from 'cross-spawn' import arg from 'arg'

// 检查 react 和 react-dom 是否安装了,没有安装则提示安装并退出 ['react', 'react-dom'].forEach((dependency) => { // ...... })

// 这里定义一些 commands ,咋眼一看,就是那几个启动命令。 const defaultCommand = 'dev' const commands = [ 'build', 'start', 'export', defaultCommand, ];

// 获取命令行参数 const args = arg({ // ...... }, { permissive: true })

// ......

// 这里很关键,查找 subcommand // Check if we are running next or next const foundCommand = args._.find((cmd) => commands.includes(cmd))

// 准备 node 的运行参数 const nodeArguments: string[] = [] if (args['--inspect']) { nodeArguments.push('--inspect') }

// 去掉 subcommand 保存到 command 中,把其余的参数放在 forwardedArgs const command = foundCommand || defaultCommand const forwardedArgs = args._.filter((arg) => arg !== command)

// Make sure the next --help case is covered // 这里是后面提到的 forwardedArgs 之一 if (args['--help']) { forwardedArgs.push('--help') }

// ......

// 这里很关键,根据 subcommand 找到即将要运行的 bin 程序。 const bin = join(__dirname, 'next-' + command)

首先来看看 Next.js 的启动命令有哪些,根据官方文档,一共是有以下 5 个命令:

next next dev next start next build next export

其中,next dev 和 next 是一样的效果。 其源码 ( 版本是 8.x.x ) 的目录如下: 可以看到项目是由 lerna 来管理的,并由 packages/next 和 packages/next-server 两个包组成。关于 lerna:

A tool for managing JavaScript projects with multiple packages

可以自行 Google。再来看看 next 的 bin 目录: 可以看到刚好存在与上面 5 个启动命令对应的 5 个文件。这里值得注意的地方是,我们运行的命令是 next build , 但文件的名字却是 next-build 。

实际上,上面 5 个命令运行的文件都是 next.ts , dev、start、build、export,都只是 next 的参数而已。

接下来直接看源码,看看 next.ts 是怎么处理这些参数的。

```javascript // ......

!/usr/bin/env node

import { join } from 'path' import spawn from 'cross-spawn' import arg from 'arg'

// 检查 react 和 react-dom 是否安装了,没有安装则提示安装并退出 ['react', 'react-dom'].forEach((dependency) => { // ...... })

// 这里定义一些 commands ,咋眼一看,就是那几个启动命令。 const defaultCommand = 'dev' const commands = [ 'build', 'start', 'export', defaultCommand, ];

// 获取命令行参数 const args = arg({ // ...... }, { permissive: true })

// ......

// 这里很关键,查找 subcommand // Check if we are running next or next const foundCommand = args._.find((cmd) => commands.includes(cmd))

// 准备 node 的运行参数 const nodeArguments: string[] = [] if (args['--inspect']) { nodeArguments.push('--inspect') }

// 去掉 subcommand 保存到 command 中,把其余的参数放在 forwardedArgs const command = foundCommand || defaultCommand const forwardedArgs = args._.filter((arg) => arg !== command)

// Make sure the next --help case is covered // 这里是后面提到的 forwardedArgs 之一 if (args['--help']) { forwardedArgs.push('--help') }

// ......

// 这里很关键,根据 subcommand 找到即将要运行的 bin 程序。 const bin = join(__dirname, 'next-' + command)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3