Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(serve): merge CLI and devServer options correctly #1649

Merged
merged 28 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7b63a1a
fix(dev-server): start adding merge and options helpers for dev serve…
knagaitsev Jun 24, 2020
18a1944
fix(serve): pass the correct options out of helper
knagaitsev Jun 24, 2020
af55584
fix(serve): add merge handling for client object
knagaitsev Jun 24, 2020
3a1cb15
chore(serve): remove socket option
knagaitsev Jul 6, 2020
9379947
chore(serve): add handling for multiple devServer configs
knagaitsev Jul 6, 2020
4f70c61
chore(serve): remove index of compiler as name option
knagaitsev Jul 7, 2020
224a39f
chore(serve): improve dev server types
knagaitsev Jul 7, 2020
56ad1bf
chore(serve): remove any use of the name flag in favor of config name…
knagaitsev Aug 22, 2020
bb8d9ab
chore(serve): add createConfig test, remove old util, improve args pa…
knagaitsev Aug 22, 2020
ae12b67
tests(cli): add arg parser stuff
knagaitsev Aug 23, 2020
b5aed3b
tests(cli): added more arg parser cli tests
knagaitsev Aug 24, 2020
b5ee5c3
tests(serve): start adding mergeOptions test
knagaitsev Aug 24, 2020
ff587c9
chore(serve): fill out dev server type mostly
knagaitsev Aug 24, 2020
db12a34
tests(serve): finish mergeOptions tests
knagaitsev Aug 24, 2020
b15cfb2
tests(serve): add getDevServerOptions tests
knagaitsev Aug 24, 2020
0a8f953
tests(serve): change how webpack is required in test
knagaitsev Aug 24, 2020
036d6e1
tests(serve): start making tests for startDevServer
knagaitsev Aug 25, 2020
ff84bcb
chore(serve): add new parseArgs helper
knagaitsev Aug 25, 2020
95066f1
tests(serve): finish parsedArgs tests
knagaitsev Aug 25, 2020
8a3bf07
tests(serve): completed startDevServer tests
knagaitsev Aug 25, 2020
17f77bd
tests(serve): update parseArgs snapshots for webpack-dev-server v3
knagaitsev Aug 25, 2020
f9bbefc
tests(serve): remove webpack as dev dependency for serve
knagaitsev Aug 25, 2020
6824fb6
chore(serve): add comment about how merge options should work
knagaitsev Aug 26, 2020
68a9802
chore(serve): add webpack as dep
knagaitsev Aug 26, 2020
a183450
chore(serve): remove webpack as serve dep
knagaitsev Aug 26, 2020
c6ebda9
tests(serve): update parseArgs snapshot
knagaitsev Aug 26, 2020
9661231
chore(serve): add comment about use of option in test
knagaitsev Aug 26, 2020
6f52de9
Merge branch 'next' into fix/dev-server-options-merging
anshumanv Aug 26, 2020
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
49 changes: 49 additions & 0 deletions packages/serve/__tests__/__snapshots__/parseArgs.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`parseArgs handles hot arg 1`] = `
Object {
"devServerArgs": Object {
"clientLogLevel": "info",
"hot": true,
"inline": true,
"liveReload": true,
"serveIndex": true,
},
"webpackArgs": Object {
"color": true,
"config": Array [],
"hot": true,
"mode": "production",
},
}
`;

exports[`parseArgs handles unknown args 1`] = `
Array [
Array [
"Unknown argument: --unknown-arg",
],
Array [
"Unknown argument: --unknown-arg-2",
],
]
`;

exports[`parseArgs parses webpack and dev server args 1`] = `
Object {
"devServerArgs": Object {
"bonjour": true,
"clientLogLevel": "info",
"inline": true,
"liveReload": true,
"port": 8080,
"serveIndex": true,
},
"webpackArgs": Object {
"color": true,
"config": Array [],
"mode": "production",
"target": "node",
},
}
`;
84 changes: 84 additions & 0 deletions packages/serve/__tests__/__snapshots__/startDevServer.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`startDevServer should set default port and host if not provided 1`] = `
Object {
"host": "localhost",
"port": 8080,
}
`;

exports[`startDevServer should set default port and host if not provided 2`] = `
Array [
8080,
"localhost",
[Function],
]
`;

exports[`startDevServer should start dev server correctly for multi compiler with 1 devServer config 1`] = `
Object {
"bonjour": true,
"host": "my.host",
"hot": true,
"port": 9000,
"progress": true,
}
`;

exports[`startDevServer should start dev server correctly for multi compiler with 1 devServer config 2`] = `
Array [
9000,
"my.host",
[Function],
]
`;

exports[`startDevServer should start dev server correctly for single compiler 1`] = `
Object {
"bonjour": true,
"host": "my.host",
"hot": true,
"port": 9000,
"progress": true,
}
`;

exports[`startDevServer should start dev server correctly for single compiler 2`] = `
Array [
9000,
"my.host",
[Function],
]
`;

exports[`startDevServer should start dev servers correctly for multi compiler with 2 devServer configs 1`] = `
Object {
"host": "localhost",
"port": 9000,
"progress": true,
}
`;

exports[`startDevServer should start dev servers correctly for multi compiler with 2 devServer configs 2`] = `
Object {
"host": "localhost",
"port": 9001,
"progress": true,
}
`;

exports[`startDevServer should start dev servers correctly for multi compiler with 2 devServer configs 3`] = `
Array [
9000,
"localhost",
[Function],
]
`;

exports[`startDevServer should start dev servers correctly for multi compiler with 2 devServer configs 4`] = `
Array [
9001,
"localhost",
[Function],
]
`;
19 changes: 0 additions & 19 deletions packages/serve/__tests__/args-to-camel-case.test.ts

This file was deleted.

43 changes: 43 additions & 0 deletions packages/serve/__tests__/createConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

import createConfig from '../src/createConfig';

describe('createConfig', () => {
it('creates config with arguments', () => {
const args = {
hot: true,
openPage: 'main',
};
expect(createConfig(args)).toEqual(args);
});

it('sets client object using clientLogging argument', () => {
const args = {
clientLogging: 'verbose',
};
expect(createConfig(args)).toEqual({
client: {
logging: 'verbose',
},
});
});

it('sets hot using hotOnly argument', () => {
const args = {
hotOnly: true,
};
expect(createConfig(args)).toEqual({
hot: 'only',
});
});

it('overrides hot with hotOnly', () => {
const args = {
hot: true,
hotOnly: true,
};
expect(createConfig(args)).toEqual({
hot: 'only',
});
});
});
51 changes: 51 additions & 0 deletions packages/serve/__tests__/getDevServerOptions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

import getDevServerOptions from '../src/getDevServerOptions';

describe('getDevServerOptions', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires, node/no-extraneous-require
const webpack = require('webpack');

it('gets dev server options from single compiler', () => {
const compiler = webpack({
devServer: {
hot: true,
host: 'my.host',
},
});
expect(getDevServerOptions(compiler)).toEqual([
{
hot: true,
host: 'my.host',
},
]);
});

it('gets dev server options from multi compiler', () => {
const compiler = webpack([
{
devServer: {
hot: true,
host: 'my.host',
},
},
{
devServer: {
hot: false,
host: 'other.host',
},
},
]);

expect(getDevServerOptions(compiler)).toEqual([
{
hot: true,
host: 'my.host',
},
{
hot: false,
host: 'other.host',
},
]);
});
});
33 changes: 33 additions & 0 deletions packages/serve/__tests__/mergeOptions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

import mergeOptions from '../src/mergeOptions';

describe('mergeOptions', () => {
it('merges CLI and devServer options correctly', () => {
const cliOptions = {
client: {
logging: 'verbose',
},
hot: true,
bonjour: true,
};
const devServerOptions = {
client: {
host: 'localhost',
logging: 'none',
},
hot: false,
liveReload: false,
};
// CLI should take priority
expect(mergeOptions(cliOptions, devServerOptions)).toEqual({
client: {
host: 'localhost',
logging: 'verbose',
},
hot: true,
bonjour: true,
liveReload: false,
});
});
});
49 changes: 49 additions & 0 deletions packages/serve/__tests__/parseArgs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

// TODO: update snapshots once we update to webpack-dev-server@4

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const errorMock: any = jest.fn();
jest.mock('webpack-cli/lib/utils/logger', () => {
return {
error: errorMock,
};
});

import WebpackCLI from 'webpack-cli';
import parseArgs from '../src/parseArgs';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const processExitSpy: any = jest.spyOn(process, 'exit');
// eslint-disable-next-line @typescript-eslint/no-empty-function
processExitSpy.mockImplementation(() => {});

describe('parseArgs', () => {
const cli = new WebpackCLI();

beforeEach(() => {
errorMock.mockClear();
processExitSpy.mockClear();
});

it('parses webpack and dev server args', () => {
const args = parseArgs(cli, ['--bonjour', '--target=node', '--port', '8080']);
expect(args).toMatchSnapshot();
expect(errorMock.mock.calls.length).toEqual(0);
expect(processExitSpy.mock.calls.length).toEqual(0);
});

it('handles hot arg', () => {
const args = parseArgs(cli, ['--hot']);
expect(args).toMatchSnapshot();
expect(errorMock.mock.calls.length).toEqual(0);
expect(processExitSpy.mock.calls.length).toEqual(0);
});

it('handles unknown args', () => {
parseArgs(cli, ['--unknown-arg', '--unknown-arg-2']);
expect(errorMock.mock.calls).toMatchSnapshot();
expect(processExitSpy.mock.calls.length).toEqual(1);
expect(processExitSpy.mock.calls[0]).toEqual([2]);
});
});
Loading