Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Allowing browser flag in serve #167

Merged
merged 3 commits into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions config/webpack/serve.webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ function getWebpackConfig(argv, skyPagesConfig) {
* @name WebpackPluginDone
*/
function WebpackPluginDone() {
const shorthand = {
l: 'launch',
b: 'browser'
};

let launched = false;
this.plugin('done', (stats) => {
if (!launched) {
Expand All @@ -62,7 +67,7 @@ function getWebpackConfig(argv, skyPagesConfig) {
this.options.devServer.publicPath
);

const hostUrl = hostUtils.resolve(
let hostUrl = hostUtils.resolve(
queryStringBase,
localUrl,
stats.toJson().chunks,
Expand All @@ -73,8 +78,18 @@ function getWebpackConfig(argv, skyPagesConfig) {
launched = true;

// Process shorthand flags
if (argv.l) {
argv.launch = argv.l;
Object.keys(shorthand).forEach(key => {
if (argv[key]) {
argv[shorthand[key]] = argv[key];
}
});

// Edge uses a different technique (protocol vs executable)
if (argv.browser === 'edge') {
const edge = 'microsoft-edge:';
argv.browser = undefined;
hostUrl = edge + hostUrl;
localUrl = edge + localUrl;
}

switch (argv.launch) {
Expand All @@ -87,11 +102,11 @@ function getWebpackConfig(argv, skyPagesConfig) {
localUrl += queryStringBase;

logger.info(`Launching Local URL: ${localUrl}`);
open(localUrl);
open(localUrl, argv.browser);
break;
default:
logger.info(`Launching Host URL: ${hostUrl}`);
open(hostUrl);
open(hostUrl, argv.browser);
break;
}
}
Expand Down
55 changes: 38 additions & 17 deletions test/config-webpack-serve.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ describe('config webpack serve', () => {

let lib;
let called;
let openCalledWith;
let openParamUrl;
let openParamBrowser;
let config;
let argv = {};

Expand All @@ -41,13 +42,16 @@ describe('config webpack serve', () => {

beforeEach(() => {
called = false;
openCalledWith = '';
openParamUrl = '';
openParamBrowser = undefined;

argv = {};

spyOn(logger, 'info');
mock('open', (url) => {
mock('open', (url, browser) => {
called = true;
openCalledWith = url;
openParamUrl = url;
openParamBrowser = browser;
});

lib = require('../config/webpack/serve.webpack.config');
Expand Down Expand Up @@ -128,10 +132,9 @@ describe('config webpack serve', () => {
}
});

const url = 'https://my-host-server.url/@blackbaud/skyux-builder/?local=true&_cfg=';
expect(logger.info).toHaveBeenCalledTimes(2);
expect(openCalledWith).toContain(
'https://my-host-server.url/@blackbaud/skyux-builder/?local=true&_cfg='
);
expect(openParamUrl.indexOf(url)).toBe(0);
});

it('should log the host url and launch it when --launch host', () => {
Expand All @@ -153,8 +156,9 @@ describe('config webpack serve', () => {
}
});

const url = 'https://my-host-server.url';
expect(logger.info).toHaveBeenCalledTimes(2);
expect(openCalledWith).toContain('https://my-host-server.url');
expect(openParamUrl.indexOf(url)).toBe(0);
});

it('should log the local url and launch it when --launch local', () => {
Expand All @@ -176,8 +180,9 @@ describe('config webpack serve', () => {
}
});

const url = 'https://localhost:1234';
expect(logger.info).toHaveBeenCalledTimes(2);
expect(openCalledWith).toContain('https://localhost:1234');
expect(openParamUrl.indexOf(url)).toBe(0);
});

it('should log a done message and not launch it when --launch none', () => {
Expand Down Expand Up @@ -226,6 +231,8 @@ describe('config webpack serve', () => {
expect(called).toEqual(false);
});



it('host querystring should not contain externals if they do not exist', () => {
const localConfig = lib.getWebpackConfig(argv, {
runtime: runtimeUtils.getDefaultRuntime(),
Expand All @@ -250,7 +257,7 @@ describe('config webpack serve', () => {
chunks: []
})
});
const urlParsed = urlLibrary.parse(openCalledWith, true);
const urlParsed = urlLibrary.parse(openParamUrl, true);
const configString = new Buffer.from(urlParsed.query._cfg, 'base64').toString();
const configObject = JSON.parse(configString);

Expand Down Expand Up @@ -286,13 +293,14 @@ describe('config webpack serve', () => {
]
})
});
const urlParsed = urlLibrary.parse(openCalledWith, true);
const urlParsed = urlLibrary.parse(openParamUrl, true);
const configString = new Buffer.from(urlParsed.query._cfg, 'base64').toString();
const configObject = JSON.parse(configString);
const url = 'https://localhost:1234';

expect(urlParsed.query._cfg).toBeDefined();
expect(configObject.externals).toEqual(skyuxConfig.skyux.app.externals);
expect(configObject.localUrl).toContain('https://localhost:1234');
expect(configObject.localUrl.indexOf(url)).toBe(0);
expect(configObject.scripts).toEqual([
{ name: 'a.js' },
{ name: 'b.js' }
Expand All @@ -308,22 +316,22 @@ describe('config webpack serve', () => {
argv.envid = 'asdf';

bindToDone();
expect(openCalledWith).toContain(`?envid=asdf`);
expect(openParamUrl).toContain(`?envid=asdf`);
});

it('should pass through svcid from the command line', () => {
argv.svcid = 'asdf';

bindToDone();
expect(openCalledWith).toContain(`?svcid=asdf`);
expect(openParamUrl).toContain(`?svcid=asdf`);
});

it('should run envid and svcid through encodeURIComponent', () => {
argv.envid = '&=$';
argv.svcid = '^%';

bindToDone();
expect(openCalledWith).toContain(
expect(openParamUrl).toContain(
`?envid=${encodeURIComponent(argv.envid)}&svcid=${encodeURIComponent(argv.svcid)}`
);
});
Expand All @@ -334,8 +342,21 @@ describe('config webpack serve', () => {
argv.myid = 'asdf3';

bindToDone();
expect(openCalledWith).toContain(`?envid=asdf1&svcid=asdf2`);
expect(openCalledWith).not.toContain(`myid=asdf3`);
expect(openParamUrl).toContain(`?envid=asdf1&svcid=asdf2`);
expect(openParamUrl).not.toContain(`myid=asdf3`);
});

it('should pass --browser flag to open', () => {
argv.browser = 'custom-browser';
bindToDone();
expect(openParamBrowser).toEqual(argv.browser);
});

it('should handle --browser edge different syntax', () => {
argv.browser = 'edge';
bindToDone();
expect(openParamBrowser).not.toBeDefined();
expect(openParamUrl.indexOf('microsoft-edge')).toBe(0);
});

});