Skip to content

Commit

Permalink
feat: 🎸 Added option to open browser window on ima dev
Browse files Browse the repository at this point in the history
  • Loading branch information
jsimck committed Jun 20, 2021
1 parent 54d4d55 commit d4f595f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/cli/lib/cliUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
};
}
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/scripts/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions packages/cli/webpack/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)
Expand Down Expand Up @@ -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({
Expand Down
27 changes: 26 additions & 1 deletion packages/cli/webpack/plugins/RunImaServerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit d4f595f

Please sign in to comment.