8

Is there any boiler plate code to use pm2 with webpack watch option for ts files auto hot reload?

pm2 start index.js is helpful to run directly, but how to add multiple tasks before doing it like watch files and auto reload using webpack and pm2 from dist folders?

Mithun Shreevatsa
  • 3,588
  • 9
  • 50
  • 95

2 Answers2

7

After much research considering performance, I might add live reload, which is a to-do task – but not a priority as of now.

"scripts": {
    "build": "webpack --config webpack.config.js --watch",
    "pm2": "pm2 start ./dist/server.js --watch=true",
    "postinstall": "npm run build",
    "test": "jest --forceExit",
    "test-ci": "npm test && cat ./coverage/lcov.info | coveralls",
    "start": "supervisor ./dist/server.js",
    "server:dev": "concurrently \"npm run build \" \"npm run start\""
}
GROVER.
  • 4,071
  • 2
  • 19
  • 66
Mithun Shreevatsa
  • 3,588
  • 9
  • 50
  • 95
0

Create a process.json for pm2 config In the script key you can give a webpack compiler to run. I am not sure if it will run for it watch reload.

DrEarnest
  • 853
  • 2
  • 13
  • 27
  • I am not clear with this, can you please explain a bit more. Do we really need webpack as well? Is there any watch mode which compiles and reload typescript using pm2? – Mithun Shreevatsa Apr 02 '18 at 12:07
  • [express-ts](https://github.com/skant09/express-ts) I am able to run this project using ```pm2 start src/server.ts --watch``` Note that in server.js, I am importing `ts-node` for transpiling. – DrEarnest Apr 02 '18 at 15:02
  • Thanks for your response @DrEarnest, i am looking for webpack integration along with pm2. So that, it can compile typescripts, uglify or minify and compile sass files as well is my requirement – Mithun Shreevatsa Apr 02 '18 at 17:40
  • In the server.js you can import your config and run it. It will recompile on each watch. `require('webpack'); var webpackConfig = require('webpack'.config'); we pack(webpackConfig);` – DrEarnest Apr 02 '18 at 17:46
  • A simple working example with hot reload of pm2 plz, ill accept your answer and reward it straight away. It will be great if you can push to the same repository which u have created – Mithun Shreevatsa Apr 02 '18 at 17:56