Skip to content

Commit bdf8ba4

Browse files
committed
feature #693 Add a way to configure devServer options (Kocal)
This PR was squashed before being merged into the master branch (closes #693). Discussion ---------- Add a way to configure devServer options Hi :) This PR is a proposal for #692. It add a new method `Encore.configureDevServerOptions()` for [configuring the webpack dev-server](https://webpack.js.org/configuration/dev-server/). Example: ```js Encore // ... .configureDevServerOptions(options => { options.https = { key: '...', cert: '...', }; }) ``` I've installed [`chai-subet`](https://www.chaijs.com/plugins/chai-subset/) for asserting objects (it's more readable and easy to do), but I can remove it if it's not wanted. 👍 Commits ------- 6d2730e Add a way to configure devServer options
2 parents 829d84a + 6d2730e commit bdf8ba4

File tree

7 files changed

+100
-1
lines changed

7 files changed

+100
-1
lines changed

index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,30 @@ class Encore {
652652
return this;
653653
}
654654

655+
/**
656+
* Configure the devServer configuration.
657+
*
658+
* https://webpack.js.org/configuration/dev-server
659+
*
660+
* ```
661+
* Encore.configureDevServerOptions(function(options) {
662+
* // change the configuration
663+
* options.https = {
664+
* key: '<your SSL cert key content>',
665+
* cert: '<your SSL cert content>',
666+
* };
667+
* });
668+
* ```
669+
*
670+
* @param {function} callback
671+
* @returns {Encore}
672+
*/
673+
configureDevServerOptions(callback) {
674+
webpackConfig.configureDevServerOptions(callback);
675+
676+
return this;
677+
}
678+
655679
/**
656680
* Automatically make some variables available everywhere!
657681
*

lib/WebpackConfig.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class WebpackConfig {
144144
this.cssLoaderConfigurationCallback = () => {};
145145
this.splitChunksConfigurationCallback = () => {};
146146
this.watchOptionsConfigurationCallback = () => {};
147+
this.devServerOptionsConfigurationCallback = () => {};
147148
this.vueLoaderOptionsCallback = () => {};
148149
this.eslintLoaderOptionsCallback = () => {};
149150
this.tsConfigurationCallback = () => {};
@@ -499,6 +500,14 @@ class WebpackConfig {
499500
this.watchOptionsConfigurationCallback = callback;
500501
}
501502

503+
configureDevServerOptions(callback) {
504+
if (typeof callback !== 'function') {
505+
throw new Error('Argument 1 to configureDevServerOptions() must be a callback function.');
506+
}
507+
508+
this.devServerOptionsConfigurationCallback = callback;
509+
}
510+
502511
createSharedEntry(name, file) {
503512
if (this.shouldSplitEntryChunks) {
504513
throw new Error('Using splitEntryChunks() and createSharedEntry() together is not supported. Use one of these strategies only to optimize your build.');

lib/config-generator.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ class ConfigGenerator {
614614
buildDevServerConfig() {
615615
const contentBase = pathUtil.getContentBase(this.webpackConfig);
616616

617-
return {
617+
const devServerOptions = {
618618
contentBase: contentBase,
619619
// this doesn't appear to be necessary, but here in case
620620
publicPath: this.webpackConfig.getRealPublicPath(),
@@ -628,6 +628,11 @@ class ConfigGenerator {
628628
watchOptions: this.buildWatchOptionsConfig(),
629629
https: this.webpackConfig.useDevServerInHttps()
630630
};
631+
632+
return applyOptionsCallback(
633+
this.webpackConfig.devServerOptionsConfigurationCallback,
634+
devServerOptions
635+
);
631636
}
632637
}
633638

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"babel-eslint": "^10.0.1",
6363
"chai": "^3.5.0",
6464
"chai-fs": "^1.0.0",
65+
"chai-subset": "^1.6.0",
6566
"core-js": "^3.0.0",
6667
"eslint": "^5.15.2",
6768
"eslint-loader": "^2.1.2",

test/config-generator.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,31 @@ describe('The config-generator function', () => {
593593
expect(actualConfig.devServer.hot).to.be.true;
594594
});
595595

596+
it('devServer with custom options', () => {
597+
const config = createConfig();
598+
config.runtimeConfig.useDevServer = true;
599+
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
600+
config.outputPath = isWindows ? 'C:\\tmp\\public' : '/tmp/public';
601+
config.setPublicPath('/');
602+
config.addEntry('main', './main');
603+
604+
config.configureDevServerOptions(options => {
605+
options.https = {
606+
key: 'https.key',
607+
cert: 'https.cert',
608+
};
609+
});
610+
611+
const actualConfig = configGenerator(config);
612+
613+
expect(actualConfig.devServer).to.containSubset({
614+
https: {
615+
key: 'https.key',
616+
cert: 'https.cert',
617+
},
618+
});
619+
});
620+
596621
it('devServer with custom watch options', () => {
597622
const config = createConfig();
598623
config.runtimeConfig.useDevServer = true;
@@ -607,6 +632,7 @@ describe('The config-generator function', () => {
607632
});
608633

609634
const actualConfig = configGenerator(config);
635+
610636
expect(actualConfig.watchOptions).to.deep.equals({
611637
'ignored': /node_modules/,
612638
'poll': 250,
@@ -616,6 +642,34 @@ describe('The config-generator function', () => {
616642
'poll': 250,
617643
});
618644
});
645+
646+
it('devServer with custom options and watch options', () => {
647+
const config = createConfig();
648+
config.runtimeConfig.useDevServer = true;
649+
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
650+
config.runtimeConfig.useHotModuleReplacement = true;
651+
config.outputPath = isWindows ? 'C:\\tmp\\public' : '/tmp/public';
652+
config.setPublicPath('/');
653+
config.addEntry('main', './main');
654+
655+
config.configureWatchOptions(watchOptions => {
656+
watchOptions.poll = 250;
657+
});
658+
config.configureDevServerOptions(options => {
659+
// should take precedence over `configureWatchOptions()`
660+
options.watchOptions.poll = 500;
661+
});
662+
663+
const actualConfig = configGenerator(config);
664+
expect(actualConfig.watchOptions).to.deep.equals({
665+
'ignored': /node_modules/,
666+
'poll': 250,
667+
});
668+
expect(actualConfig.devServer.watchOptions).to.deep.equals({
669+
'ignored': /node_modules/,
670+
'poll': 500,
671+
});
672+
});
619673
});
620674

621675
describe('test for addPlugin config', () => {

test/functional.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
const os = require('os');
1313
const chai = require('chai');
1414
chai.use(require('chai-fs'));
15+
chai.use(require('chai-subset'));
1516
const expect = chai.expect;
1617
const path = require('path');
1718
const testSetup = require('./helpers/setup');

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,11 @@ chai-fs@^1.0.0:
17571757
bit-mask "0.0.2-alpha"
17581758
readdir-enhanced "^1.4.0"
17591759

1760+
chai-subset@^1.6.0:
1761+
version "1.6.0"
1762+
resolved "https://registry.yarnpkg.com/chai-subset/-/chai-subset-1.6.0.tgz#a5d0ca14e329a79596ed70058b6646bd6988cfe9"
1763+
integrity sha1-pdDKFOMpp5WW7XAFi2ZGvWmIz+k=
1764+
17601765
chai@^3.5.0:
17611766
version "3.5.0"
17621767
resolved "https://registry.yarnpkg.com/chai/-/chai-3.5.0.tgz#4d02637b067fe958bdbfdd3a40ec56fef7373247"

0 commit comments

Comments
 (0)