Skip to content

Commit 93f2a6d

Browse files
authored
feature #486 feat: configure watching options (Kocal)
2 parents 1219c89 + 23f3654 commit 93f2a6d

File tree

5 files changed

+110
-4
lines changed

5 files changed

+110
-4
lines changed

index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,27 @@ class Encore {
588588
return this;
589589
}
590590

591+
/**
592+
* Configure the watchOptions and devServer.watchOptions configuration.
593+
*
594+
* https://webpack.js.org/configuration/watch/
595+
* https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
596+
*
597+
* Encore.configureWatchOptions(function(watchOptions) {
598+
* // change the configuration
599+
*
600+
* watchOptions.poll = 250; // useful when running inside a Virtual Machine
601+
* });
602+
*
603+
* @param {function} callback
604+
* @returns {Encore}
605+
*/
606+
configureWatchOptions(callback) {
607+
webpackConfig.configureWatchOptions(callback);
608+
609+
return this;
610+
}
611+
591612
/**
592613
* Automatically make some variables available everywhere!
593614
*

lib/WebpackConfig.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class WebpackConfig {
9494
this.shouldUseSingleRuntimeChunk = null;
9595
this.shouldSplitEntryChunks = false;
9696
this.splitChunksConfigurationCallback = () => {};
97+
this.watchOptionsConfigurationCallback = () => {};
9798
this.vueLoaderOptionsCallback = () => {};
9899
this.eslintLoaderOptionsCallback = () => {};
99100
this.tsConfigurationCallback = () => {};
@@ -395,6 +396,14 @@ class WebpackConfig {
395396
this.splitChunksConfigurationCallback = callback;
396397
}
397398

399+
configureWatchOptions(callback) {
400+
if (typeof callback !== 'function') {
401+
throw new Error('Argument 1 to configureWatchOptions() must be a callback function.');
402+
}
403+
404+
this.watchOptionsConfigurationCallback = callback;
405+
}
406+
398407
createSharedEntry(name, file) {
399408
if (this.shouldSplitEntryChunks) {
400409
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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class ConfigGenerator {
6969
rules: this.buildRulesConfig(),
7070
},
7171
plugins: this.buildPluginsConfig(),
72-
optimization: this.buildOptimizationConfig()
72+
optimization: this.buildOptimizationConfig(),
73+
watchOptions: this.buildWatchOptionsConfig()
7374
};
7475

7576
if (this.webpackConfig.useSourceMaps) {
@@ -525,6 +526,17 @@ class ConfigGenerator {
525526
return stats;
526527
}
527528

529+
buildWatchOptionsConfig() {
530+
const watchOptions = {
531+
ignored: /node_modules/
532+
};
533+
534+
return applyOptionsCallback(
535+
this.webpackConfig.watchOptionsConfigurationCallback,
536+
watchOptions
537+
);
538+
}
539+
528540
buildDevServerConfig() {
529541
const contentBase = pathUtil.getContentBase(this.webpackConfig);
530542

@@ -539,9 +551,7 @@ class ConfigGenerator {
539551
quiet: true,
540552
compress: true,
541553
historyApiFallback: true,
542-
watchOptions: {
543-
ignored: /node_modules/
544-
},
554+
watchOptions: this.buildWatchOptionsConfig(),
545555
https: this.webpackConfig.useDevServerInHttps()
546556
};
547557
}

test/WebpackConfig.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,4 +1065,29 @@ describe('WebpackConfig object', () => {
10651065
}).to.throw('"foo" is not a valid key');
10661066
});
10671067
});
1068+
1069+
describe('configureWatchOptions()', () => {
1070+
it('Pass config', () => {
1071+
const config = createConfig();
1072+
const callback = (watchOptions) => {
1073+
watchOptions.poll = 250;
1074+
};
1075+
1076+
config.configureWatchOptions(callback);
1077+
1078+
expect(config.watchOptionsConfigurationCallback).to.equal(callback);
1079+
});
1080+
1081+
it('Call method without a valid callback', () => {
1082+
const config = createConfig();
1083+
1084+
expect(() => {
1085+
config.configureWatchOptions();
1086+
}).to.throw('Argument 1 to configureWatchOptions() must be a callback function.');
1087+
1088+
expect(() => {
1089+
config.configureWatchOptions({});
1090+
}).to.throw('Argument 1 to configureWatchOptions() must be a callback function.');
1091+
});
1092+
});
10681093
});

test/config-generator.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,30 @@ describe('The config-generator function', () => {
573573
const actualConfig = configGenerator(config);
574574
expect(actualConfig.devServer.hot).to.be.true;
575575
});
576+
577+
it('devServer with custom watch options', () => {
578+
const config = createConfig();
579+
config.runtimeConfig.useDevServer = true;
580+
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
581+
config.runtimeConfig.useHotModuleReplacement = true;
582+
config.outputPath = isWindows ? 'C:\\tmp\\public' : '/tmp/public';
583+
config.setPublicPath('/');
584+
config.addEntry('main', './main');
585+
586+
config.configureWatchOptions(watchOptions => {
587+
watchOptions.poll = 250;
588+
});
589+
590+
const actualConfig = configGenerator(config);
591+
expect(actualConfig.watchOptions).to.deep.equals({
592+
'ignored': /node_modules/,
593+
'poll': 250,
594+
});
595+
expect(actualConfig.devServer.watchOptions).to.deep.equals({
596+
'ignored': /node_modules/,
597+
'poll': 250,
598+
});
599+
});
576600
});
577601

578602
describe('test for addPlugin config', () => {
@@ -934,4 +958,21 @@ describe('The config-generator function', () => {
934958
expect(JSON.stringify(logger.getMessages().deprecation)).to.contain('the recommended setting is Encore.enableSingleRuntimeChunk()');
935959
});
936960
});
961+
962+
describe('Test buildWatchOptionsConfig()', () => {
963+
it('Set webpack watch options', () => {
964+
const config = createConfig();
965+
config.outputPath = '/tmp/public/build';
966+
config.setPublicPath('/build/');
967+
config.configureWatchOptions(watchOptions => {
968+
watchOptions.poll = 250;
969+
});
970+
971+
const actualConfig = configGenerator(config);
972+
expect(actualConfig.watchOptions).to.deep.equals({
973+
ignored: /node_modules/,
974+
poll: 250,
975+
});
976+
});
977+
});
937978
});

0 commit comments

Comments
 (0)