Skip to content

fix(server): throw if multiple instances use the same compiler #3039

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

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 6 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class Server {
constructor(compiler, options = {}) {
validate(schema, options, 'webpack Dev Server');

const devServerSymbol = Symbol.for('webpack-dev-server');
if (compiler[devServerSymbol]) {
throw new Error('The compiler already has a dev server attached.');
}
compiler[devServerSymbol] = true;

this.compiler = compiler;
this.options = options;
this.logger = this.compiler.getInfrastructureLogger('webpack-dev-server');
Expand Down
2 changes: 1 addition & 1 deletion test/Validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Validation', () => {
let compiler;
let server;

beforeAll(() => {
beforeEach(() => {
compiler = webpack(config);
});

Expand Down
25 changes: 25 additions & 0 deletions test/server/Server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ describe('Server', () => {
});
});

describe('devServer property of the compiler', () => {
it('should be true', (done) => {
const compiler = webpack(config);
const server = new Server(compiler, baseDevConfig);

expect(compiler[Symbol.for('webpack-dev-server')]).toBe(true);

compiler.hooks.done.tap('webpack-dev-server', () => {
server.close(done);
});

compiler.run(() => {});
});

it('should prevent multiple Server instances from using the same compiler', () => {
const compiler = webpack(config);
let server = new Server(compiler, baseDevConfig);

expect(() => {
// eslint-disable-next-line no-unused-vars
server = new Server(compiler, baseDevConfig);
}).toThrow();
});
});

it('test server error reporting', () => {
const compiler = webpack(config);
const server = new Server(compiler, baseDevConfig);
Expand Down
2 changes: 1 addition & 1 deletion test/server/utils/createDomain.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('createDomain', () => {
let compiler;
let server;

beforeAll(() => {
beforeEach(() => {
compiler = webpack(config);
});

Expand Down