TL;DR
- npm-run-allを使用することで、サーバー起動時の Listening処理により発生するロックの影響を受けず、別処理を実行することができる
- scriptもスッキリと短く記入できる
経緯
モックサーバーを:3000
、ローカルサーバー(クライアント側のエントリーポイント)を:8000
で、ワンライナーで立ち上げたかったのですが、
npm run <Mock Run> && <Local-Server Run>
とすると、Mockの方でリスニング状態になってしまい、 ローカルサーバーの起動が実行されない。
デフォルトのスクリプトでは、ワンライナーではサーバーを並列実行できない ? → 導入しました。
install
npm-run-all こちらの公式サイトを参考に npm install.
コマンド 一覧
npm-run-all
公式より
The main command is npm-run-all. We can make complex plans with npm-run-all command.
Both run-s and run-p are shorthand commands. run-s is for sequential, run-p is for parallel. We can make simple plans with those commands.
- 日本語訳
- メインコマンドは
npm-run-all
だよ。 -
run-s
コマンドとrun-p
コマンドはショートハンドで、-
run-s
は直列(連続)実行 -
run-p
は並列(同時)実行
-
- メインコマンドは
実例
モックサーバーの立ち上げと、webpackのホットリロードを同時に立ち上げてみました。
npm run dev
で、npm run api
と npm run serve
を同時に実行しています。
{
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"test:unit": "vue-cli-service test:unit",
"api": "npx json-server --watch db.json",
"dev": "run-p api serve"
},
}