Skip to content

Commit 6d2730e

Browse files
Kocalweaverryan
authored andcommitted
Add a way to configure devServer options
1 parent a4ae7ce commit 6d2730e

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
@@ -613,7 +613,7 @@ class ConfigGenerator {
613613
buildDevServerConfig() {
614614
const contentBase = pathUtil.getContentBase(this.webpackConfig);
615615

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

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
@@ -588,6 +588,31 @@ describe('The config-generator function', () => {
588588
expect(actualConfig.devServer.hot).to.be.true;
589589
});
590590

591+
it('devServer with custom options', () => {
592+
const config = createConfig();
593+
config.runtimeConfig.useDevServer = true;
594+
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
595+
config.outputPath = isWindows ? 'C:\\tmp\\public' : '/tmp/public';
596+
config.setPublicPath('/');
597+
config.addEntry('main', './main');
598+
599+
config.configureDevServerOptions(options => {
600+
options.https = {
601+
key: 'https.key',
602+
cert: 'https.cert',
603+
};
604+
});
605+
606+
const actualConfig = configGenerator(config);
607+
608+
expect(actualConfig.devServer).to.containSubset({
609+
https: {
610+
key: 'https.key',
611+
cert: 'https.cert',
612+
},
613+
});
614+
});
615+
591616
it('devServer with custom watch options', () => {
592617
const config = createConfig();
593618
config.runtimeConfig.useDevServer = true;
@@ -602,6 +627,7 @@ describe('The config-generator function', () => {
602627
});
603628

604629
const actualConfig = configGenerator(config);
630+
605631
expect(actualConfig.watchOptions).to.deep.equals({
606632
'ignored': /node_modules/,
607633
'poll': 250,
@@ -611,6 +637,34 @@ describe('The config-generator function', () => {
611637
'poll': 250,
612638
});
613639
});
640+
641+
it('devServer with custom options and watch options', () => {
642+
const config = createConfig();
643+
config.runtimeConfig.useDevServer = true;
644+
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
645+
config.runtimeConfig.useHotModuleReplacement = true;
646+
config.outputPath = isWindows ? 'C:\\tmp\\public' : '/tmp/public';
647+
config.setPublicPath('/');
648+
config.addEntry('main', './main');
649+
650+
config.configureWatchOptions(watchOptions => {
651+
watchOptions.poll = 250;
652+
});
653+
config.configureDevServerOptions(options => {
654+
// should take precedence over `configureWatchOptions()`
655+
options.watchOptions.poll = 500;
656+
});
657+
658+
const actualConfig = configGenerator(config);
659+
expect(actualConfig.watchOptions).to.deep.equals({
660+
'ignored': /node_modules/,
661+
'poll': 250,
662+
});
663+
expect(actualConfig.devServer.watchOptions).to.deep.equals({
664+
'ignored': /node_modules/,
665+
'poll': 500,
666+
});
667+
});
614668
});
615669

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

test/functional.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
const chai = require('chai');
1313
chai.use(require('chai-fs'));
14+
chai.use(require('chai-subset'));
1415
const expect = chai.expect;
1516
const path = require('path');
1617
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)