forked from webpack/webpack-dev-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(server): add updateOptions API helper (webpack#2117)
* refactor(server): add update options api helper * test(server): removed global require eslint comments * refactor(server): switch update options to normalize options
- Loading branch information
1 parent
4ce5dcf
commit 4735879
Showing
4 changed files
with
146 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable | ||
no-undefined | ||
*/ | ||
|
||
function normalizeOptions(compiler, options) { | ||
// Setup default value | ||
options.contentBase = | ||
options.contentBase !== undefined ? options.contentBase : process.cwd(); | ||
|
||
// set serverMode default | ||
if (options.serverMode === undefined) { | ||
options.serverMode = 'sockjs'; | ||
} | ||
|
||
// set clientMode default | ||
if (options.clientMode === undefined) { | ||
options.clientMode = 'sockjs'; | ||
} | ||
|
||
if (!options.watchOptions) { | ||
options.watchOptions = {}; | ||
} | ||
} | ||
|
||
module.exports = normalizeOptions; |
40 changes: 40 additions & 0 deletions
40
test/server/utils/__snapshots__/normalizeOptions.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`normalizeOptions contentBase array should set correct options 1`] = ` | ||
Object { | ||
"clientMode": "sockjs", | ||
"contentBase": Array [ | ||
"/path/to/dist1", | ||
"/path/to/dist2", | ||
], | ||
"serverMode": "sockjs", | ||
"watchOptions": Object {}, | ||
} | ||
`; | ||
|
||
exports[`normalizeOptions contentBase string should set correct options 1`] = ` | ||
Object { | ||
"clientMode": "sockjs", | ||
"contentBase": "/path/to/dist", | ||
"serverMode": "sockjs", | ||
"watchOptions": Object {}, | ||
} | ||
`; | ||
|
||
exports[`normalizeOptions no options should set correct options 1`] = ` | ||
Object { | ||
"clientMode": "sockjs", | ||
"serverMode": "sockjs", | ||
"watchOptions": Object {}, | ||
} | ||
`; | ||
|
||
exports[`normalizeOptions watchOptions should set correct options 1`] = ` | ||
Object { | ||
"clientMode": "sockjs", | ||
"serverMode": "sockjs", | ||
"watchOptions": Object { | ||
"poll": true, | ||
}, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
|
||
const webpack = require('webpack'); | ||
const normalizeOptions = require('../../../lib/utils/normalizeOptions'); | ||
|
||
describe('normalizeOptions', () => { | ||
const cases = [ | ||
{ | ||
title: 'no options', | ||
multiCompiler: false, | ||
options: {}, | ||
optionsResults: null, | ||
}, | ||
{ | ||
title: 'contentBase string', | ||
multiCompiler: false, | ||
options: { | ||
contentBase: '/path/to/dist', | ||
}, | ||
optionsResults: null, | ||
}, | ||
{ | ||
title: 'contentBase array', | ||
multiCompiler: false, | ||
options: { | ||
contentBase: ['/path/to/dist1', '/path/to/dist2'], | ||
}, | ||
optionsResults: null, | ||
}, | ||
{ | ||
title: 'watchOptions', | ||
multiCompiler: false, | ||
options: { | ||
watchOptions: { | ||
poll: true, | ||
}, | ||
}, | ||
optionsResults: null, | ||
}, | ||
]; | ||
|
||
cases.forEach((data) => { | ||
describe(data.title, () => { | ||
let compiler; | ||
beforeAll(() => { | ||
let webpackConfig; | ||
if (data.multiCompiler) { | ||
webpackConfig = require('../../fixtures/multi-compiler-config/webpack.config'); | ||
} else { | ||
webpackConfig = require('../../fixtures/simple-config/webpack.config'); | ||
} | ||
|
||
compiler = webpack(webpackConfig); | ||
}); | ||
|
||
it('should set correct options', () => { | ||
const originalContentBase = data.options.contentBase; | ||
normalizeOptions(compiler, data.options); | ||
if (data.optionsResults) { | ||
expect(data.options).toEqual(data.optionsResults); | ||
} else { | ||
if (data.options.contentBase !== originalContentBase) { | ||
// we don't want this in the snapshot, because it is | ||
// the current working directory | ||
expect(data.options.contentBase).toEqual(process.cwd()); | ||
delete data.options.contentBase; | ||
} | ||
expect(data.options).toMatchSnapshot(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |