0 script

There is such a script in package.json

"scripts": {
    "dev": "webpack-dev-server"
    }
Copy the code

1 run

NPM run dev: NPM run

NPM run-script NPM rum NPM urn NPM run start NPM run start NPM run start

2 System Execution

If the script command cannot be found in PATH, node_modules/. Bin will be added before the command to search for the execution file again and execute the command (CMD. Exe is used for Windows, /bin/sh is used for Linux, which can be configured using –script-shell).

You can see the node_modules/.bin/webpack-dev-server file

In Linux, the webpack-dev-server executable file is executed without suffix. In Windows, the webpack-dev-server executable file is executed without suffix and iu cannot be executed, and the default execution order is PATHEXT=.COM. .EXE; .BAT; .CMD; .VBS; .VBE; .JS; .JSE; .WSF; .WSH; .msc, so webpack-dev-server.cmd will be executed

3 Execution Content

Node_modules /.bin/webpack-dev-server
#! /bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")

case `uname` in
    *CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;;
esac

if [ -x "$basedir/node" ]; then
  exec "$basedir/node"  "$basedir/.. /webpack-dev-server/bin/webpack-dev-server.js" "$@"
else 
  exec node  "$basedir/.. /webpack-dev-server/bin/webpack-dev-server.js" "$@"
fi
Copy the code
:: Node_modules /.bin/webpack-dev-servercmd
@ECHO off
GOTO start
:find_dp0
SET dp0=%~dp0
EXIT /b
:start
SETLOCAL
CALL :find_dp0

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

endLocal & goto #_undefined_# 2>NUL || title %COMSPEC%&"%_prog%""%dp0%\.. \webpack-dev-server\bin\webpack-dev-server.js" %*Copy the code

The two files (should) only differ in language and have the same execution meaning. So read the SH script.

#! /bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
Get directory name

case `uname` in
    *CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;;
    Check system name for directory compatibility
esac

if [ -x "$basedir/node" ]; then
  exec "$basedir/node"  "$basedir/.. /webpack-dev-server/bin/webpack-dev-server.js" "$@"
else 
  exec node  "$basedir/.. /webpack-dev-server/bin/webpack-dev-server.js" "$@"
fi
The # node command executes the specified file and passes in parameters
Copy the code

4 Origin of Files

Now, where did the file come from? NPM automatically generated, refer to the official document. Whenever bin is configured in the module package.json, it is automatically generated at installation time. This file is equivalent to a Link to the executable in a module.