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

Add a way to configure devServer options #693

Merged
merged 1 commit into from
Mar 20, 2020
Merged
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
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,30 @@ class Encore {
return this;
}

/**
* Configure the devServer configuration.
*
* https://webpack.js.org/configuration/dev-server
*
* ```
* Encore.configureDevServerOptions(function(options) {
* // change the configuration
* options.https = {
* key: '<your SSL cert key content>',
* cert: '<your SSL cert content>',
* };
* });
* ```
*
* @param {function} callback
* @returns {Encore}
*/
configureDevServerOptions(callback) {
webpackConfig.configureDevServerOptions(callback);

return this;
}

/**
* Automatically make some variables available everywhere!
*
Expand Down
9 changes: 9 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class WebpackConfig {
this.cssLoaderConfigurationCallback = () => {};
this.splitChunksConfigurationCallback = () => {};
this.watchOptionsConfigurationCallback = () => {};
this.devServerOptionsConfigurationCallback = () => {};
this.vueLoaderOptionsCallback = () => {};
this.eslintLoaderOptionsCallback = () => {};
this.tsConfigurationCallback = () => {};
Expand Down Expand Up @@ -499,6 +500,14 @@ class WebpackConfig {
this.watchOptionsConfigurationCallback = callback;
}

configureDevServerOptions(callback) {
if (typeof callback !== 'function') {
throw new Error('Argument 1 to configureDevServerOptions() must be a callback function.');
}

this.devServerOptionsConfigurationCallback = callback;
}

createSharedEntry(name, file) {
if (this.shouldSplitEntryChunks) {
throw new Error('Using splitEntryChunks() and createSharedEntry() together is not supported. Use one of these strategies only to optimize your build.');
Expand Down
7 changes: 6 additions & 1 deletion lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ class ConfigGenerator {
buildDevServerConfig() {
const contentBase = pathUtil.getContentBase(this.webpackConfig);

return {
const devServerOptions = {
contentBase: contentBase,
// this doesn't appear to be necessary, but here in case
publicPath: this.webpackConfig.getRealPublicPath(),
Expand All @@ -627,6 +627,11 @@ class ConfigGenerator {
watchOptions: this.buildWatchOptionsConfig(),
https: this.webpackConfig.useDevServerInHttps()
};

return applyOptionsCallback(
this.webpackConfig.devServerOptionsConfigurationCallback,
devServerOptions
);
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"babel-eslint": "^10.0.1",
"chai": "^3.5.0",
"chai-fs": "^1.0.0",
"chai-subset": "^1.6.0",
"core-js": "^3.0.0",
"eslint": "^5.15.2",
"eslint-loader": "^2.1.2",
Expand Down
54 changes: 54 additions & 0 deletions test/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,31 @@ describe('The config-generator function', () => {
expect(actualConfig.devServer.hot).to.be.true;
});

it('devServer with custom options', () => {
const config = createConfig();
config.runtimeConfig.useDevServer = true;
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
config.outputPath = isWindows ? 'C:\\tmp\\public' : '/tmp/public';
config.setPublicPath('/');
config.addEntry('main', './main');

config.configureDevServerOptions(options => {
options.https = {
key: 'https.key',
cert: 'https.cert',
};
});

const actualConfig = configGenerator(config);

expect(actualConfig.devServer).to.containSubset({
https: {
key: 'https.key',
cert: 'https.cert',
},
});
});

it('devServer with custom watch options', () => {
const config = createConfig();
config.runtimeConfig.useDevServer = true;
Expand All @@ -602,6 +627,7 @@ describe('The config-generator function', () => {
});

const actualConfig = configGenerator(config);

expect(actualConfig.watchOptions).to.deep.equals({
'ignored': /node_modules/,
'poll': 250,
Expand All @@ -611,6 +637,34 @@ describe('The config-generator function', () => {
'poll': 250,
});
});

it('devServer with custom options and watch options', () => {
const config = createConfig();
config.runtimeConfig.useDevServer = true;
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
config.runtimeConfig.useHotModuleReplacement = true;
config.outputPath = isWindows ? 'C:\\tmp\\public' : '/tmp/public';
config.setPublicPath('/');
config.addEntry('main', './main');

config.configureWatchOptions(watchOptions => {
watchOptions.poll = 250;
});
config.configureDevServerOptions(options => {
// should take precedence over `configureWatchOptions()`
options.watchOptions.poll = 500;
});

const actualConfig = configGenerator(config);
expect(actualConfig.watchOptions).to.deep.equals({
'ignored': /node_modules/,
'poll': 250,
});
expect(actualConfig.devServer.watchOptions).to.deep.equals({
'ignored': /node_modules/,
'poll': 500,
});
});
});

describe('test for addPlugin config', () => {
Expand Down
1 change: 1 addition & 0 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const chai = require('chai');
chai.use(require('chai-fs'));
chai.use(require('chai-subset'));
const expect = chai.expect;
const path = require('path');
const testSetup = require('./helpers/setup');
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,11 @@ chai-fs@^1.0.0:
bit-mask "0.0.2-alpha"
readdir-enhanced "^1.4.0"

chai-subset@^1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/chai-subset/-/chai-subset-1.6.0.tgz#a5d0ca14e329a79596ed70058b6646bd6988cfe9"
integrity sha1-pdDKFOMpp5WW7XAFi2ZGvWmIz+k=

chai@^3.5.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/chai/-/chai-3.5.0.tgz#4d02637b067fe958bdbfdd3a40ec56fef7373247"
Expand Down