LoginSignup
0
0

More than 3 years have passed since last update.

browser-syncのport番号をランダムにする

Last updated at Posted at 2020-07-07

複数の案件を同時に進めている都合上、複数個のbrowser-syncを同時に走らせたいときportが衝突してイライラしちゃうので、そもそもportをランダムにしとけばいいんじゃないか説。

他にいい方法があれば教えてください。。。

// browser-sync.js
module.exports = port => {
    const path = require('path');
    const browser = require('browser-sync').create();

    browser.init({
        files: [
          // 監視したいファイルのパターンリスト
        ],
        // 動的・プライベート ポート番号からランダムで
        port: port || Math.floor(Math.random() * (65535 - 49152)) + 49152,
        startPath: '/',
        server: {
            baseDir: path.join(__dirname, 'public')
        }
    });
};

// 呼び出し側のjs
require('./browser-sync')(); // 引数にポート番号を入れれば固定でも実行できる

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0