From d4f595f8212c22bab66b4d9f29e4a5aab0b9a1b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0ime=C4=8Dek?= Date: Sun, 20 Jun 2021 17:55:39 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Added=20option=20to=20op?= =?UTF-8?q?en=20browser=20window=20on=20ima=20dev?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cli/lib/cliUtils.js | 4 +-- packages/cli/scripts/dev.js | 7 +++-- packages/cli/webpack/config.js | 14 +++++++--- .../cli/webpack/plugins/RunImaServerPlugin.js | 27 ++++++++++++++++++- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/packages/cli/lib/cliUtils.js b/packages/cli/lib/cliUtils.js index 1db44f3ef5..729a0f5f92 100644 --- a/packages/cli/lib/cliUtils.js +++ b/packages/cli/lib/cliUtils.js @@ -40,10 +40,10 @@ function handlerFactory(handlerFn) { : path.resolve(process.cwd(), dir); return await handlerFn({ + ...yargs, rootDir: dir ? absoluteDir : process.cwd(), isProduction, - isLegacyMode: !!yargs.legacyCompatMode, - publicPath: yargs.publicPath ? yargs.publicPath : '/' + command }); }; } diff --git a/packages/cli/scripts/dev.js b/packages/cli/scripts/dev.js index a5f3d51624..3884bf1cda 100644 --- a/packages/cli/scripts/dev.js +++ b/packages/cli/scripts/dev.js @@ -22,8 +22,11 @@ const devCommand = { command: 'dev', desc: 'Run application in development watch mode', builder: builderFactory({ - 'legacy-compat-mode': { - desc: 'Runs application in ES5 compatible format' + open: { + alias: 'o', + desc: 'Opens browser window after server has been started', + type: 'boolean', + default: true } }), handler: handlerFactory(dev) diff --git a/packages/cli/webpack/config.js b/packages/cli/webpack/config.js index ad084f4499..419d855622 100644 --- a/packages/cli/webpack/config.js +++ b/packages/cli/webpack/config.js @@ -9,7 +9,7 @@ const RunImaServerPlugin = require('./plugins/RunImaServerPlugin'); const { requireConfig, resolveEnvironment } = require('./lib/configUtils'); module.exports = async args => { - const { rootDir, isProduction, isServer, isWatch, publicPath } = args; + const { rootDir, isProduction, isServer, isWatch } = args; const packageJson = require(path.resolve(rootDir, './package.json')); const imaEnvironment = resolveEnvironment(rootDir); @@ -26,7 +26,7 @@ module.exports = async args => { path.resolve(rootDir, './app/main.js') ], output: { - publicPath, + publicPath: args.publicPath, filename: isServer ? 'ima/app.server.js' : 'static/js/main.js', path: path.resolve(rootDir, './build'), ...(isServer ? { libraryTarget: 'commonjs2' } : undefined) @@ -156,7 +156,15 @@ module.exports = async args => { 'server/server.js' ] }), - ...(isWatch ? [new RunImaServerPlugin({ rootDir })] : []) + ...(isWatch + ? [ + new RunImaServerPlugin({ + rootDir, + open: args.open, + port: imaEnvironment.$Server.port + }) + ] + : []) ] : [ new MiniCssExtractPlugin({ diff --git a/packages/cli/webpack/plugins/RunImaServerPlugin.js b/packages/cli/webpack/plugins/RunImaServerPlugin.js index 49482738a7..357c71353a 100644 --- a/packages/cli/webpack/plugins/RunImaServerPlugin.js +++ b/packages/cli/webpack/plugins/RunImaServerPlugin.js @@ -16,13 +16,38 @@ class RunImaServerPlugin { path.resolve(this._options.rootDir, './build/server') ); - this._serverStart = true; + if (this._options.open) { + this._openBrowser(`http://localhost:${this._options.port || 3001}`); + this._serverStart = true; + } } callback(); } ); } + + _openBrowser(url) { + const [command, args = []] = this._browserCommand(); + + childProcess.execFile(command, [...args, encodeURI(url)]); + } + + _browserCommand() { + const { platform } = process; + + switch (platform) { + case 'android': + case 'linux': + return ['xdg-open']; + case 'darwin': + return ['open']; + case 'win32': + return ['cmd', ['/c', 'start']]; + default: + throw new Error(`Platform ${platform} isn't supported.`); + } + } } module.exports = RunImaServerPlugin;