简体   繁体   English

自定义npm脚本在我的代码中如何工作?

[英]how does custom npm scripts works in my code?

I'm reading node js code wherein package.json I found the following script 我正在读取节点js代码,其中package.json我发现以下脚本

scripts: {
   start : 'some-dependency start' 
}

So, when I run npm run start it's actually starting my application with a web server, serving static files in my project. 因此,当我运行npm run start它实际上是通过Web服务器启动应用程序,并在项目中提供静态文件。

My question is how some-dependency start running? 我的问题是some-dependency start如何some-dependency start运行? what it can do? 它能做什么? how it can serve my static files. 它如何为我的静态文件提供服务。 I see internally some-dependency using react-scripts . 我在内部看到使用react-scripts some-dependency But I can't wrap my head around how all these things working. 但是我无法确定所有这些事情是如何工作的。

react-scripts is a package that comes in built with create-react-app when you run npm run start it executes a script/program that is wrapped in react-scripts package you can see the script for start command here , in that you can see that the script invokes the webpack-dev-server which serves the bundled javascript on a server 当您运行npm run start时, react-scripts是一个由create-react-app内置的软件包,它执行包裹在react-scripts软件包中的脚本/程序,您可以在此处查看start命令的脚本,因为您可以看到脚本调用了webpack-dev-server,该服务器在服务器上提供捆绑的javascript

Generally when you execute some script through package.json file the same happens, you need to specify a command/ invoke a script 通常,当您通过package.json文件执行某些脚本时,也会发生同样的情况,您需要指定命令/调用脚本

for example consider following script present in package.json file 例如,考虑以下package.json文件中存在的脚本

 script: {
    "development": " cd client/ && NODE_ENV=development webpack -w --config webpack.dev.config.js"
}

In the above example when you run npm run development the following things happen 在上面的示例中,当您运行npm run development ,会发生以下情况

  1. changes the directory to client 将目录更改为客户端
  2. Node environment is set to development 节点环境即将开发
  3. invokes the webpack with the config file webpack.dev.config.js present in the client directory 使用客户端目录中存在的配置文件webpack.dev.config.js调用webpack

It executes what ever that is written in the config file 它执行配置文件中写入的内容

Feel free to ask doubts if any 随时提出疑问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM