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

feat: serve integration #1712

Merged
merged 25 commits into from Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6d67bec
feat: add serve to webpack-cli
rishabh3112 Jul 31, 2020
3a50303
tests: add basic integartion tests
rishabh3112 Jul 31, 2020
a4e46d2
chore: update path
rishabh3112 Jul 31, 2020
f600956
chore: check dev-server
rishabh3112 Aug 2, 2020
7746ece
chore: fix lint
rishabh3112 Aug 2, 2020
6bfbaa5
Merge branch 'next' into feat/serve-integration
rishabh3112 Aug 2, 2020
e6b2226
chore: lint package json
rishabh3112 Aug 2, 2020
19c65d7
Merge branch 'feat/serve-integration' of https://github.com/webpack/w…
rishabh3112 Aug 2, 2020
c387a85
Merge branch 'next' into feat/serve-integration
rishabh3112 Aug 6, 2020
233e7c8
chore: add check in serve itself
rishabh3112 Aug 6, 2020
4de1ab3
chore: add error handling into the package
rishabh3112 Aug 10, 2020
bb4a8cc
chore: add error handling into the package
rishabh3112 Aug 10, 2020
359796f
Merge branch 'next' into feat/serve-integration
rishabh3112 Aug 10, 2020
4f6e559
chore: shift import location
rishabh3112 Aug 10, 2020
5a7e513
Merge branch 'feat/serve-integration' of https://github.com/webpack/w…
rishabh3112 Aug 10, 2020
6bb75d3
chore: remove redundant error handling
rishabh3112 Aug 10, 2020
5ed3f0b
chore: early return on windows
rishabh3112 Aug 13, 2020
c18d6eb
chore: add dummy tests
rishabh3112 Aug 13, 2020
cdf8be2
Merge branch 'next' into feat/serve-integration
rishabh3112 Aug 13, 2020
39a0601
Merge branch 'next' into feat/serve-integration
snitin315 Aug 14, 2020
7431ec6
chore: ignore integration test
rishabh3112 Aug 16, 2020
55867d5
Merge branch 'feat/serve-integration' of https://github.com/webpack/w…
rishabh3112 Aug 16, 2020
9185904
chore: add integration test
rishabh3112 Aug 16, 2020
5534914
chore: update tests
rishabh3112 Aug 16, 2020
af9fdb7
chore: skip test on windows
rishabh3112 Aug 17, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/serve/package.json
Expand Up @@ -13,8 +13,12 @@
"webpack-dev-server": "3.10.3"
},
"peerDependencies": {
"webpack-cli": "4.x.x",
"webpack-dev-server": "3.x.x || 4.x.x"
"webpack-cli": "4.x.x"
},
"peerDependenciesMeta": {
"webpack-dev-server": {
"optional": true
}
},
"gitHead": "fb50f766851f500ca12867a2aa9de81fa6e368f9"
}
9 changes: 7 additions & 2 deletions packages/serve/src/index.ts
@@ -1,4 +1,3 @@
import { devServer } from 'webpack-dev-server/bin/cli-flags';
import WebpackCLI from 'webpack-cli';
import logger from 'webpack-cli/lib/utils/logger';
import startDevServer from './startDevServer';
Expand All @@ -11,10 +10,16 @@ import startDevServer from './startDevServer';
* @returns {Function} invokes the devServer API
*/
export default function serve(...args: string[]): void {
let devServerFlags: object[];
try {
devServerFlags = require('webpack-dev-server/bin/cli-flags').devServer;
} catch (err) {
throw new Error(`You need to install 'webpack-dev-server' for running 'webpack serve'.\n${err}`);
}
const cli = new WebpackCLI();
const core = cli.getCoreFlags();

const parsedDevServerArgs = cli.argParser(devServer, args, true);
const parsedDevServerArgs = cli.argParser(devServerFlags, args, true);
const devServerArgs = parsedDevServerArgs.opts;
const parsedWebpackArgs = cli.argParser(core, parsedDevServerArgs.unknownArgs, true, process.title);
const webpackArgs = parsedWebpackArgs.opts;
Expand Down
4 changes: 2 additions & 2 deletions packages/serve/src/startDevServer.ts
@@ -1,5 +1,3 @@
import Server from 'webpack-dev-server/lib/Server';

/**
*
* Starts the devServer
Expand All @@ -10,6 +8,8 @@ import Server from 'webpack-dev-server/lib/Server';
* @returns {Void}
*/
export default function startDevServer(compiler, options): void {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const Server = require('webpack-dev-server/lib/Server');
const firstWpOpt = compiler.compilers ? compiler.compilers[0].options : compiler.options;
const devServerOptions = firstWpOpt.devServer || {};

Expand Down
1 change: 1 addition & 0 deletions packages/webpack-cli/__tests__/serve/index.js
@@ -0,0 +1 @@
console.log('hello world');
24 changes: 24 additions & 0 deletions packages/webpack-cli/__tests__/serve/serve.test.js
@@ -0,0 +1,24 @@
const { sync: spawnSync } = require('execa');
const path = require('path');

describe('Serve', () => {
it('should run with cli', () => {
const { stdout, stderr } = spawnSync(path.resolve(__dirname, '../../bin/cli.js'), ['serve'], {
cwd: path.resolve(__dirname),
reject: false,
});
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
expect(stderr).toHaveLength(0);
});

it('should work with flags', () => {
const { stdout, stderr } = spawnSync(path.resolve(__dirname, '../../bin/cli.js'), ['serve', '--hot'], {
cwd: path.resolve(__dirname),
reject: false,
});
expect(stdout).toContain('main.js');
expect(stdout).toContain('hot/dev-server.js');
expect(stderr).toHaveLength(0);
});
});
4 changes: 4 additions & 0 deletions packages/webpack-cli/__tests__/serve/webpack.config.js
@@ -0,0 +1,4 @@
module.exports = {
mode: 'development',
devtool: false,
};
1 change: 1 addition & 0 deletions packages/webpack-cli/package.json
Expand Up @@ -26,6 +26,7 @@
"@webpack-cli/package-utils": "^1.0.1-alpha.4",
"@webpack-cli/info": "^1.0.1-alpha.4",
"@webpack-cli/init": "^1.0.1-alpha.5",
"@webpack-cli/serve": "^1.0.1-alpha.5",
"ansi-escapes": "^4.3.1",
"colorette": "^1.2.1",
"command-line-usage": "^6.1.0",
Expand Down
97 changes: 48 additions & 49 deletions test/serve/basic/serve-basic.test.js
Expand Up @@ -19,62 +19,61 @@ describe('basic serve usage', () => {

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

// TODO fix me on windows
if (isWindows) {
// TODO fix me on windows
it('compiles without flags', () => {
it('TODO: Fix on windows', () => {
expect(true).toBe(true);

console.warn('TODO: fix `serve` test on windows');
});
} else {
it('should respect the --no-color flag', async () => {
const { stdout, stderr } = await runServe(['--help', '--no-color'], __dirname);
options.enabled = true;
expect(stdout).not.toContain(yellow(usageText));
expect(stdout).toContain(descriptionText);
expect(stderr).toHaveLength(0);
});
return;
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
}

it('should not invoke info subcommand', async () => {
const { stdout, stderr } = await runServe(['--client-log-level', 'info'], testPath);
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
expect(stderr).toHaveLength(0);
});
it('should respect the --no-color flag', async () => {
const { stdout, stderr } = await runServe(['--help', '--no-color'], __dirname);
options.enabled = true;
expect(stdout).not.toContain(yellow(usageText));
expect(stdout).toContain(descriptionText);
expect(stderr).toHaveLength(0);
});

it('compiles without flags', async () => {
const { stdout, stderr } = await runServe(['--port', port], testPath);
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
expect(stderr).toHaveLength(0);
});
it('should not invoke info subcommand', async () => {
const { stdout, stderr } = await runServe(['--client-log-level', 'info'], testPath);
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
expect(stderr).toHaveLength(0);
});

it('uses hot flag to alter bundle', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--hot'], testPath);
expect(stdout).toContain('main.js');
expect(stdout).toContain('hot/dev-server.js');
expect(stderr).toHaveLength(0);
});
it('compiles without flags', async () => {
const { stdout, stderr } = await runServe(['--port', port], testPath);
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
expect(stderr).toHaveLength(0);
});

it('uses no-hot flag', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--no-hot'], testPath);
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
expect(stderr).toHaveLength(0);
});
it('uses hot flag to alter bundle', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--hot'], testPath);
expect(stdout).toContain('main.js');
expect(stdout).toContain('hot/dev-server.js');
expect(stderr).toHaveLength(0);
});

it('uses hot flag and progress flag', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--hot', '--progress'], testPath);
expect(stdout).toContain('main.js');
expect(stdout).toContain('hot/dev-server.js');
// progress flag makes use of stderr
expect(stderr).not.toHaveLength(0);
});
it('uses no-hot flag', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--no-hot'], testPath);
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
expect(stderr).toHaveLength(0);
});

it('throws error on unknown flag', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--unknown-flag'], testPath);
expect(stdout).toHaveLength(0);
expect(stderr).toContain('Unknown argument: --unknown-flag');
});
}
it('uses hot flag and progress flag', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--hot', '--progress'], testPath);
expect(stdout).toContain('main.js');
expect(stdout).toContain('hot/dev-server.js');
// progress flag makes use of stderr
expect(stderr).not.toHaveLength(0);
});

it('throws error on unknown flag', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--unknown-flag'], testPath);
expect(stdout).toHaveLength(0);
expect(stderr).toContain('Unknown argument: --unknown-flag');
});
});
87 changes: 43 additions & 44 deletions test/serve/with-custom-port/serve-custom-config.test.js
Expand Up @@ -15,54 +15,53 @@ describe('serve with devServer in config', () => {

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

// TODO fix me on windows
if (isWindows) {
// TODO fix me on windows
it('compiles without flags', () => {
it('TODO: Fix on windows', () => {
expect(true).toBe(true);

console.warn('TODO: fix `serve` test on windows');
});
} else {
it('Should pick up the host and port from config', async () => {
const { stdout, stderr } = await runServe([], testPath);
// Should output the correct bundle file
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
// Runs at correct host and port
expect(stdout).toContain('http://0.0.0.0:1234');
expect(stderr).toBeFalsy();
});
return;
}

it('Port flag should override the config port', async () => {
const { stdout, stderr } = await runServe(['--port', port], testPath);
// Should output the correct bundle file
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
// Runs at correct host and port
expect(stdout).toContain(`http://0.0.0.0:${port}`);
expect(stderr).toBeFalsy();
});
it('Should pick up the host and port from config', async () => {
const { stdout, stderr } = await runServe([], testPath);
// Should output the correct bundle file
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
// Runs at correct host and port
expect(stdout).toContain('http://0.0.0.0:1234');
expect(stderr).toBeFalsy();
});

it('Passing hot flag works alongside other server config', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--hot'], testPath);
// Should output the correct bundle file
expect(stdout).toContain('main.js');
// HMR is being used
expect(stdout).toContain('hot/dev-server.js');
// Runs at correct host and port
expect(stdout).toContain(`http://0.0.0.0:${port}`);
expect(stderr).toBeFalsy();
});
it('Port flag should override the config port', async () => {
const { stdout, stderr } = await runServe(['--port', port], testPath);
// Should output the correct bundle file
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
// Runs at correct host and port
expect(stdout).toContain(`http://0.0.0.0:${port}`);
expect(stderr).toBeFalsy();
});

it('works fine when no-hot flag is passed alongside other server config', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--no-hot'], testPath);
// Should output the correct bundle file
expect(stdout).toContain('main.js');
// HMR is not being used
expect(stdout).not.toContain('hot/dev-server.js');
// Runs at correct host and port
expect(stdout).toContain(`http://0.0.0.0:${port}`);
expect(stderr).toBeFalsy();
});
}
it('Passing hot flag works alongside other server config', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--hot'], testPath);
// Should output the correct bundle file
expect(stdout).toContain('main.js');
// HMR is being used
expect(stdout).toContain('hot/dev-server.js');
// Runs at correct host and port
expect(stdout).toContain(`http://0.0.0.0:${port}`);
expect(stderr).toBeFalsy();
});

it('works fine when no-hot flag is passed alongside other server config', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--no-hot'], testPath);
// Should output the correct bundle file
expect(stdout).toContain('main.js');
// HMR is not being used
expect(stdout).not.toContain('hot/dev-server.js');
// Runs at correct host and port
expect(stdout).toContain(`http://0.0.0.0:${port}`);
expect(stderr).toBeFalsy();
});
});