npm run dev 是“在本地运行我的网站”的标准,但它是如何工作的呢?我们如何扩展它的功能?在这篇文章中,我们将看看:
作为一个激励示例,以下是凸帮助程序示例应用程序中定义的一些脚本。我们将介绍每个部分的作用
"scripts": { "dev": "npm-run-all --parallel dev:backend dev:frontend", "build": "tsc && vite build", "dev:backend": "convex dev", "dev:frontend": "vite", "predev": "convex dev --until-success", "test": "vitest" },它们的定义方式和位置
// in package.json { // ... "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, //...在这里你可以运行 npm run dev 或 npm run lint 等
您可以在文档中了解有关 npm run 的更多信息。
为什么使用脚本?
yarn # similar to `npm install` npm run dev # still works!您不必记住 npm run dev 映射到yarn dev(或yarn run dev)。 npx 也是如此:无论您使用什么包管理器来安装东西,npx 凸开发都可以工作。
并行运行命令
4
"scripts": { "dev": "npm-run-all --parallel dev:backend dev:frontend", "dev:backend": "convex dev", "dev:frontend": "vite", },这定义了三个脚本。
预开发?后期构建?
"scripts": { "dev": "npm-run-all --parallel dev:backend dev:frontend", "dev:backend": "convex dev", "dev:frontend": "vite", "predev": "convex dev --until-success", },这将在 npm-run-all --parallel dev:backend dev:frontend 的“dev”命令之前运行凸开发 --until-success。
For those used to shell scripting, you can run two commands in sequence if the previous one succeeds with commandA && commandB. This works on both Windows and Unix (mac / linux).
However, there's a couple advantages to just using pre-scripts:
For Convex, when you first run npx convex dev (or npm run dev with the above scripts), it will ask you to log in if you aren't already, and ask you to set up your project if one isn't already set up. This is great, but interactive commands that update the output text don't work well when the output is being streamed by multiple commands at once. This is the motivation for running npx convex dev --until-success before npx convex dev.
If you change your "predev" command for Convex to include --run it will run a server-side function before your frontend has started.
"scripts": { //... "predev": "convex dev --until-success --run init", //... },
The --run init command will run a function that is the default export in convex/init.ts. You could also have run --run myFolder/myModule:myFunction. See docs on naming here. See this post on seeding data but the gist is that you can define an internalMutation that checks if the database is empty, and if so inserts a collection of records for testing / setup purposes.
If you use TypeScript, you can run a type check / compile your typescript files with a bare tsc. If your tsconfig.json is configured to emit types, it will write out the types. If not, it will just validate the types. This is great to do as part of the build, so you don't build anything that has type errors. This is why the above example did:
"build": "tsc && vite build",
If you want to pass arguments to a command, for instance passing arguments to your testing command to specify what test to run, you can pass them after a -- to separate the command from the argument. Technically you don't need -- if your arguments are positional instead of --prefixed, but it doesn't hurt to always do it in case you forget which to do it for.
npm run test -- --grep="pattern"
We looked at some ways of using package.json scripts to simplify our workflows. Who knew how much power could rest behind a simple npm run dev? Looking at our original example:
"scripts": { "dev": "npm-run-all --parallel dev:backend dev:frontend", "build": "tsc && vite build", "dev:backend": "convex dev", "dev:frontend": "vite", "predev": "convex dev --until-success", "test": "vitest" },
The way your shell finds which command to run when you type npm is to check the shell's PATH environment variable (on unix machines anyways). You can see your own with echo "$PATH". It checks all the places specified in $PATH and uses the first one. ↩
Technically you can override & specify where npm installs binaries. ↩
如果你确实想要,你可以直接运行 npm exec vitest,简称 npx vitest,./npm_modules/.bin/vitest,或者将 .npm_modules/.bin 添加到你的 PATH 中。 ↩
有些人使用裸 & 在后台运行一项任务,但这在 Windows 上不受支持,并且中断一个命令不一定会杀死另一个命令。 ↩
以上是使用 package.json 脚本增强'npm run dev”的详细内容。更多信息请关注PHP中文网其他相关文章!