4

一 npm脚本配置

npm脚本在我们日常工作中用到的还是非常多的,如果大家用webpack作为打包工具的话,相信npm run dev/bulid 之类的命令大家一定都使用的非常多了。那么为什么只需要输入npm run dev就能开启一个虚拟服务器?为什么run build就能构建项目呢?相信有很多同学会和我一样存在着些许疑问。下面我们就来看一看npm run dev/bulid等命令到底是个什么东西?

1.1 package.json

大家如果在项目中使用了npm去下载包的话,项目中一定会有一个npm的管理文件package.json。
图片描述

打开package.json,你会看到类似如下所示的内容

{
  "name": "todolist",
  "version": "1.0.0",
  "description": "todolist",
  "author": "chirs xiong",
  "private": true,
  "scripts": {
    "predev":"echo $npm_package_name",
    "dev": "node build/dev-server.js",
    "start": "node build/dev-server.js",
    "build": "node build/build.js",
    "unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run",
    "e2e": "node test/e2e/runner.js",
    "test": "npm run unit && npm run e2e",
    "lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs"
  },
  "dependencies": {
    "animate.css": "^3.5.2",
    "element-ui": "^1.3.6",
    "font-awesome": "^4.7.0",
    "font-awesome-webpack": "0.0.5-beta.2",
    "iconate": "^0.2.7",
    "less": "^2.7.2",
    "less-loader": "^4.0.4",
    "particles.js": "^2.0.0",
    "style-loader": "^0.18.2",
    "vue": "^2.3.3",
    "vue-disqus": "^2.0.3",
    "vue-router": "^2.3.1"
  },
  "devDependencies": {
    "autoprefixer": "^6.7.2",
    "babel-core": "^6.22.1",
    "babel-eslint": "^7.1.1",
    "babel-loader": "^6.4.1",
    "babel-plugin-component": "^0.9.1",
    "babel-plugin-istanbul": "^4.1.1",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-preset-env": "^1.3.2",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-2": "^6.22.0",
    "babel-register": "^6.22.0",
    "chai": "^3.5.0",
    "chalk": "^1.1.3",
    "chromedriver": "^2.27.2",
    "connect-history-api-fallback": "^1.3.0",
    "copy-webpack-plugin": "^4.0.1",
    "cross-env": "^4.0.0",
    "cross-spawn": "^5.0.1",
    "css-loader": "^0.28.4",
    "eslint": "^3.19.0",
    "eslint-config-standard": "^6.2.1",
    "eslint-friendly-formatter": "^2.0.7",
    "eslint-loader": "^1.7.1",
    "eslint-plugin-html": "^2.0.0",
    "eslint-plugin-promise": "^3.4.0",
    "eslint-plugin-standard": "^2.0.1",
    "eventsource-polyfill": "^0.9.6",
    "express": "^4.14.1",
    "extract-text-webpack-plugin": "^2.0.0",
    "file-loader": "^0.11.2",
    "friendly-errors-webpack-plugin": "^1.1.3",
    "html-webpack-plugin": "^2.28.0",
    "http-proxy-middleware": "^0.17.3",
    "inject-loader": "^3.0.0",
    "karma": "^1.4.1",
    "karma-coverage": "^1.1.1",
    "karma-mocha": "^1.3.0",
    "karma-phantomjs-launcher": "^1.0.2",
    "karma-phantomjs-shim": "^1.4.0",
    "karma-sinon-chai": "^1.3.1",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-spec-reporter": "0.0.30",
    "karma-webpack": "^2.0.2",
    "lolex": "^1.5.2",
    "mocha": "^3.2.0",
    "nightwatch": "^0.9.12",
    "node-sass": "^4.5.3",
    "opn": "^4.0.2",
    "optimize-css-assets-webpack-plugin": "^1.3.0",
    "ora": "^1.2.0",
    "phantomjs-prebuilt": "^2.1.14",
    "rimraf": "^2.6.0",
    "sass-loader": "^6.0.6",
    "selenium-server": "^3.0.1",
    "semver": "^5.3.0",
    "shelljs": "^0.7.6",
    "sinon": "^2.1.0",
    "sinon-chai": "^2.8.0",
    "transform-loader": "^0.2.4",
    "url-loader": "^0.5.8",
    "vue-loader": "^12.1.0",
    "vue-style-loader": "^3.0.1",
    "vue-template-compiler": "^2.3.3",
    "webpack": "^2.6.1",
    "webpack-bundle-analyzer": "^2.2.1",
    "webpack-dev-middleware": "^1.10.0",
    "webpack-hot-middleware": "^2.18.0",
    "webpack-merge": "^4.1.0"
  },
  "engines": {
    "node": ">= 4.0.0",
    "npm": ">= 3.0.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}
1.2 scripts脚本

你可以在package.json中看到你项目的基本信息,如名称,项目版本,依赖组件等等。现在大家来看看scripts属性,有没有觉得非常熟悉,我们平时用的dev,start,bulid等都出现在了scripts属性中,而script属性就是npm的脚本。你可以在这里自定义脚本。如下

scripts: {
 "mock": "node mock/mock-server.js"
}

像这样定义脚本之后,只要你输入在shell中输入npm run mock 就相当于执行了mock-server.js这个命令

二 总结

其实npm脚本的运行机理就是当你运行npm run时,实际上是默认打开了一个shell窗口,然后输入script中定义的命令。如 npm run mock。其实际上是默认打开了一个shell窗口(当然你看不到),然后在该窗口中输入node mock/mock-server.js,需要注意的一点是npm run默认打开的shell,其路径默认为node_modules的路径,所以如果你想运行node_modules中的内容,路径中不需要加node_modules


淼淼真人
1.1k 声望202 粉丝

一个前端菜鸟!