diff --git a/bin/cli.js b/bin/cli.js index 2feaa9e7b1a..d080cd064cd 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -11,6 +11,7 @@ program .command('serve [input]') .description('starts a development server') .option('-p, --port ', 'set the port to serve on. defaults to 1234') + .option('-o, --open', 'automatically open in default browser') .option( '-d, --out-dir ', 'set the output directory. defaults to "dist"' @@ -93,7 +94,10 @@ function bundle(main, command) { const bundler = new Bundler(main, command); if (command.name() === 'serve') { - bundler.serve(command.port || 1234); + const server = bundler.serve(command.port || 1234); + if (command.open) { + require('opn')(`http://localhost:${server.address().port}`); + } } else { bundler.bundle(); } diff --git a/package.json b/package.json index b2e9afc4258..d7a19751010 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "micromatch": "^3.0.4", "mkdirp": "^0.5.1", "node-libs-browser": "^2.0.0", + "opn": "^5.1.0", "parse-json": "^4.0.0", "physical-cpu-count": "^2.0.0", "postcss": "^6.0.10", diff --git a/yarn.lock b/yarn.lock index 782d0b2ecb1..b0f5529d494 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2508,6 +2508,10 @@ is-windows@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.1.tgz#310db70f742d259a16a369202b51af84233310d9" +is-wsl@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/is-wsl/-/is-wsl-1.1.0.tgz#1f16e4aa22b04d1336b66188a66af3c600c3a66d" + isarray@1.0.0, isarray@^1.0.0, isarray@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" @@ -3413,6 +3417,12 @@ onetime@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/onetime/-/onetime-1.1.0.tgz#a1f7838f8314c516f05ecefcbc4ccfe04b4ed789" +opn@^5.1.0: + version "5.1.0" + resolved "https://registry.yarnpkg.com/opn/-/opn-5.1.0.tgz#72ce2306a17dbea58ff1041853352b4a8fc77519" + dependencies: + is-wsl "^1.1.0" + optimist@^0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/optimist/-/optimist-0.6.1.tgz#da3ea74686fa21a19a111c326e90eb15a0196686"