Skip to content

Commit d3e2936

Browse files
authored
feat: serve integration (#1712)
1 parent e06392a commit d3e2936

File tree

9 files changed

+139
-99
lines changed

9 files changed

+139
-99
lines changed

packages/serve/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
"webpack-dev-server": "3.10.3"
1414
},
1515
"peerDependencies": {
16-
"webpack-cli": "4.x.x",
17-
"webpack-dev-server": "3.x.x || 4.x.x"
16+
"webpack-cli": "4.x.x"
17+
},
18+
"peerDependenciesMeta": {
19+
"webpack-dev-server": {
20+
"optional": true
21+
}
1822
},
1923
"gitHead": "fb50f766851f500ca12867a2aa9de81fa6e368f9"
2024
}

packages/serve/src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { devServer } from 'webpack-dev-server/bin/cli-flags';
21
import WebpackCLI from 'webpack-cli';
32
import logger from 'webpack-cli/lib/utils/logger';
43
import startDevServer from './startDevServer';
@@ -11,10 +10,16 @@ import startDevServer from './startDevServer';
1110
* @returns {Function} invokes the devServer API
1211
*/
1312
export default function serve(...args: string[]): void {
13+
let devServerFlags: object[];
14+
try {
15+
devServerFlags = require('webpack-dev-server/bin/cli-flags').devServer;
16+
} catch (err) {
17+
throw new Error(`You need to install 'webpack-dev-server' for running 'webpack serve'.\n${err}`);
18+
}
1419
const cli = new WebpackCLI();
1520
const core = cli.getCoreFlags();
1621

17-
const parsedDevServerArgs = cli.argParser(devServer, args, true);
22+
const parsedDevServerArgs = cli.argParser(devServerFlags, args, true);
1823
const devServerArgs = parsedDevServerArgs.opts;
1924
const parsedWebpackArgs = cli.argParser(core, parsedDevServerArgs.unknownArgs, true, process.title);
2025
const webpackArgs = parsedWebpackArgs.opts;

packages/serve/src/startDevServer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Server from 'webpack-dev-server/lib/Server';
2-
31
/**
42
*
53
* Starts the devServer
@@ -10,6 +8,8 @@ import Server from 'webpack-dev-server/lib/Server';
108
* @returns {Void}
119
*/
1210
export default function startDevServer(compiler, options): void {
11+
// eslint-disable-next-line @typescript-eslint/no-var-requires
12+
const Server = require('webpack-dev-server/lib/Server');
1313
const firstWpOpt = compiler.compilers ? compiler.compilers[0].options : compiler.options;
1414
const devServerOptions = firstWpOpt.devServer || {};
1515

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('hello world');
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { runServe } = require('../../../../test/utils/test-utils');
2+
3+
describe('Serve', () => {
4+
const isWindows = process.platform === 'win32';
5+
6+
// TODO fix me on windows
7+
if (isWindows) {
8+
it('TODO: Fix on windows', () => {
9+
expect(true).toBe(true);
10+
});
11+
return;
12+
}
13+
14+
it('should run with cli', async () => {
15+
const { stdout, stderr } = await runServe([], __dirname);
16+
expect(stdout).toContain('main.js');
17+
expect(stdout).not.toContain('hot/dev-server.js');
18+
expect(stderr).toHaveLength(0);
19+
});
20+
21+
it('should work with flags', async () => {
22+
const { stdout, stderr } = await runServe(['--hot'], __dirname);
23+
expect(stdout).toContain('main.js');
24+
expect(stdout).toContain('hot/dev-server.js');
25+
expect(stderr).toHaveLength(0);
26+
});
27+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
mode: 'development',
3+
devtool: false,
4+
};

packages/webpack-cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@webpack-cli/package-utils": "^1.0.1-alpha.4",
2727
"@webpack-cli/info": "^1.0.1-alpha.4",
2828
"@webpack-cli/init": "^1.0.1-alpha.5",
29+
"@webpack-cli/serve": "^1.0.1-alpha.5",
2930
"ansi-escapes": "^4.3.1",
3031
"colorette": "^1.2.1",
3132
"command-line-usage": "^6.1.0",

test/serve/basic/serve-basic.test.js

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,62 +19,61 @@ describe('basic serve usage', () => {
1919

2020
const isWindows = process.platform === 'win32';
2121

22+
// TODO fix me on windows
2223
if (isWindows) {
23-
// TODO fix me on windows
24-
it('compiles without flags', () => {
24+
it('TODO: Fix on windows', () => {
2525
expect(true).toBe(true);
26-
27-
console.warn('TODO: fix `serve` test on windows');
28-
});
29-
} else {
30-
it('should respect the --no-color flag', async () => {
31-
const { stdout, stderr } = await runServe(['--help', '--no-color'], __dirname);
32-
options.enabled = true;
33-
expect(stdout).not.toContain(yellow(usageText));
34-
expect(stdout).toContain(descriptionText);
35-
expect(stderr).toHaveLength(0);
3626
});
27+
return;
28+
}
3729

38-
it('should not invoke info subcommand', async () => {
39-
const { stdout, stderr } = await runServe(['--client-log-level', 'info'], testPath);
40-
expect(stdout).toContain('main.js');
41-
expect(stdout).not.toContain('hot/dev-server.js');
42-
expect(stderr).toHaveLength(0);
43-
});
30+
it('should respect the --no-color flag', async () => {
31+
const { stdout, stderr } = await runServe(['--help', '--no-color'], __dirname);
32+
options.enabled = true;
33+
expect(stdout).not.toContain(yellow(usageText));
34+
expect(stdout).toContain(descriptionText);
35+
expect(stderr).toHaveLength(0);
36+
});
4437

45-
it('compiles without flags', async () => {
46-
const { stdout, stderr } = await runServe(['--port', port], testPath);
47-
expect(stdout).toContain('main.js');
48-
expect(stdout).not.toContain('hot/dev-server.js');
49-
expect(stderr).toHaveLength(0);
50-
});
38+
it('should not invoke info subcommand', async () => {
39+
const { stdout, stderr } = await runServe(['--client-log-level', 'info'], testPath);
40+
expect(stdout).toContain('main.js');
41+
expect(stdout).not.toContain('hot/dev-server.js');
42+
expect(stderr).toHaveLength(0);
43+
});
5144

52-
it('uses hot flag to alter bundle', async () => {
53-
const { stdout, stderr } = await runServe(['--port', port, '--hot'], testPath);
54-
expect(stdout).toContain('main.js');
55-
expect(stdout).toContain('hot/dev-server.js');
56-
expect(stderr).toHaveLength(0);
57-
});
45+
it('compiles without flags', async () => {
46+
const { stdout, stderr } = await runServe(['--port', port], testPath);
47+
expect(stdout).toContain('main.js');
48+
expect(stdout).not.toContain('hot/dev-server.js');
49+
expect(stderr).toHaveLength(0);
50+
});
5851

59-
it('uses no-hot flag', async () => {
60-
const { stdout, stderr } = await runServe(['--port', port, '--no-hot'], testPath);
61-
expect(stdout).toContain('main.js');
62-
expect(stdout).not.toContain('hot/dev-server.js');
63-
expect(stderr).toHaveLength(0);
64-
});
52+
it('uses hot flag to alter bundle', async () => {
53+
const { stdout, stderr } = await runServe(['--port', port, '--hot'], testPath);
54+
expect(stdout).toContain('main.js');
55+
expect(stdout).toContain('hot/dev-server.js');
56+
expect(stderr).toHaveLength(0);
57+
});
6558

66-
it('uses hot flag and progress flag', async () => {
67-
const { stdout, stderr } = await runServe(['--port', port, '--hot', '--progress'], testPath);
68-
expect(stdout).toContain('main.js');
69-
expect(stdout).toContain('hot/dev-server.js');
70-
// progress flag makes use of stderr
71-
expect(stderr).not.toHaveLength(0);
72-
});
59+
it('uses no-hot flag', async () => {
60+
const { stdout, stderr } = await runServe(['--port', port, '--no-hot'], testPath);
61+
expect(stdout).toContain('main.js');
62+
expect(stdout).not.toContain('hot/dev-server.js');
63+
expect(stderr).toHaveLength(0);
64+
});
7365

74-
it('throws error on unknown flag', async () => {
75-
const { stdout, stderr } = await runServe(['--port', port, '--unknown-flag'], testPath);
76-
expect(stdout).toHaveLength(0);
77-
expect(stderr).toContain('Unknown argument: --unknown-flag');
78-
});
79-
}
66+
it('uses hot flag and progress flag', async () => {
67+
const { stdout, stderr } = await runServe(['--port', port, '--hot', '--progress'], testPath);
68+
expect(stdout).toContain('main.js');
69+
expect(stdout).toContain('hot/dev-server.js');
70+
// progress flag makes use of stderr
71+
expect(stderr).not.toHaveLength(0);
72+
});
73+
74+
it('throws error on unknown flag', async () => {
75+
const { stdout, stderr } = await runServe(['--port', port, '--unknown-flag'], testPath);
76+
expect(stdout).toHaveLength(0);
77+
expect(stderr).toContain('Unknown argument: --unknown-flag');
78+
});
8079
});

test/serve/with-custom-port/serve-custom-config.test.js

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,53 @@ describe('serve with devServer in config', () => {
1515

1616
const isWindows = process.platform === 'win32';
1717

18+
// TODO fix me on windows
1819
if (isWindows) {
19-
// TODO fix me on windows
20-
it('compiles without flags', () => {
20+
it('TODO: Fix on windows', () => {
2121
expect(true).toBe(true);
22-
23-
console.warn('TODO: fix `serve` test on windows');
24-
});
25-
} else {
26-
it('Should pick up the host and port from config', async () => {
27-
const { stdout, stderr } = await runServe([], testPath);
28-
// Should output the correct bundle file
29-
expect(stdout).toContain('main.js');
30-
expect(stdout).not.toContain('hot/dev-server.js');
31-
// Runs at correct host and port
32-
expect(stdout).toContain('http://0.0.0.0:1234');
33-
expect(stderr).toBeFalsy();
3422
});
23+
return;
24+
}
3525

36-
it('Port flag should override the config port', async () => {
37-
const { stdout, stderr } = await runServe(['--port', port], testPath);
38-
// Should output the correct bundle file
39-
expect(stdout).toContain('main.js');
40-
expect(stdout).not.toContain('hot/dev-server.js');
41-
// Runs at correct host and port
42-
expect(stdout).toContain(`http://0.0.0.0:${port}`);
43-
expect(stderr).toBeFalsy();
44-
});
26+
it('Should pick up the host and port from config', async () => {
27+
const { stdout, stderr } = await runServe([], testPath);
28+
// Should output the correct bundle file
29+
expect(stdout).toContain('main.js');
30+
expect(stdout).not.toContain('hot/dev-server.js');
31+
// Runs at correct host and port
32+
expect(stdout).toContain('http://0.0.0.0:1234');
33+
expect(stderr).toBeFalsy();
34+
});
4535

46-
it('Passing hot flag works alongside other server config', async () => {
47-
const { stdout, stderr } = await runServe(['--port', port, '--hot'], testPath);
48-
// Should output the correct bundle file
49-
expect(stdout).toContain('main.js');
50-
// HMR is being used
51-
expect(stdout).toContain('hot/dev-server.js');
52-
// Runs at correct host and port
53-
expect(stdout).toContain(`http://0.0.0.0:${port}`);
54-
expect(stderr).toBeFalsy();
55-
});
36+
it('Port flag should override the config port', async () => {
37+
const { stdout, stderr } = await runServe(['--port', port], testPath);
38+
// Should output the correct bundle file
39+
expect(stdout).toContain('main.js');
40+
expect(stdout).not.toContain('hot/dev-server.js');
41+
// Runs at correct host and port
42+
expect(stdout).toContain(`http://0.0.0.0:${port}`);
43+
expect(stderr).toBeFalsy();
44+
});
5645

57-
it('works fine when no-hot flag is passed alongside other server config', async () => {
58-
const { stdout, stderr } = await runServe(['--port', port, '--no-hot'], testPath);
59-
// Should output the correct bundle file
60-
expect(stdout).toContain('main.js');
61-
// HMR is not being used
62-
expect(stdout).not.toContain('hot/dev-server.js');
63-
// Runs at correct host and port
64-
expect(stdout).toContain(`http://0.0.0.0:${port}`);
65-
expect(stderr).toBeFalsy();
66-
});
67-
}
46+
it('Passing hot flag works alongside other server config', async () => {
47+
const { stdout, stderr } = await runServe(['--port', port, '--hot'], testPath);
48+
// Should output the correct bundle file
49+
expect(stdout).toContain('main.js');
50+
// HMR is being used
51+
expect(stdout).toContain('hot/dev-server.js');
52+
// Runs at correct host and port
53+
expect(stdout).toContain(`http://0.0.0.0:${port}`);
54+
expect(stderr).toBeFalsy();
55+
});
56+
57+
it('works fine when no-hot flag is passed alongside other server config', async () => {
58+
const { stdout, stderr } = await runServe(['--port', port, '--no-hot'], testPath);
59+
// Should output the correct bundle file
60+
expect(stdout).toContain('main.js');
61+
// HMR is not being used
62+
expect(stdout).not.toContain('hot/dev-server.js');
63+
// Runs at correct host and port
64+
expect(stdout).toContain(`http://0.0.0.0:${port}`);
65+
expect(stderr).toBeFalsy();
66+
});
6867
});

0 commit comments

Comments
 (0)