Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用打包工具,执行script语句之后,发生什么 #36

Open
yuandaishi opened this issue Jul 8, 2020 · 0 comments
Open

使用打包工具,执行script语句之后,发生什么 #36

yuandaishi opened this issue Jul 8, 2020 · 0 comments
Labels
react something about react

Comments

@yuandaishi
Copy link
Owner

我们习惯使用webpack构建前端项目,之后执行打包语句,例如npm run dev。然后项目就跑起来了,但是执行npm run dev之后,发生了什么事,为什么就跑起来了呢?

1.执行npm run 语句,会去寻找package.json的script属性对应的key值:例如 package.json中script属性如下

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

那么我们执行npm run start的时候,其实执行了react-scripts start语句

2.寻找modules文件夹中的bin文件夹,并寻找文件名为react-scripts的文件,我们可以看到这个文件是cmd文件。即window命令行脚本,打开该文件,看到内容如下:

@ECHO off
SETLOCAL
CALL :find_dp0

IF EXIST "%dp0%\node.exe" (
  SET "_prog=%dp0%\node.exe"
) ELSE (
  SET "_prog=node"
  SET PATHEXT=%PATHEXT:;.JS;=;%
)

"%_prog%"  "%dp0%\..\react-scripts\bin\react-scripts.js" %*
ENDLOCAL
EXIT /b %errorlevel%
:find_dp0
SET dp0=%~dp0
EXIT /b

3.从第二步我们大致猜想到此时需要执行\react-scripts\bin\react-scripts.js路径下的react-scripts.js。

至此,寻找执行文件的步骤完成,接下来便是node执行相应的js文件,开始打包。

@yuandaishi yuandaishi added the react something about react label Jul 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
react something about react
Projects
None yet
Development

No branches or pull requests

1 participant