Skip to content

feat: configure watching options #486

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

Merged
merged 5 commits into from
Jan 29, 2019
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
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,27 @@ class Encore {
return this;
}

/**
* Configure the watchOptions and devServer.watchOptions configuration.
*
* https://webpack.js.org/configuration/watch/
* https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
*
* Encore.configureWatchOptions(function(watchOptions) {
* // change the configuration
*
* watchOptions.poll = 250; // useful when running inside a Virtual Machine
* });
*
* @param {function} callback
* @returns {Encore}
*/
configureWatchOptions(callback) {
webpackConfig.configureWatchOptions(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 @@ -94,6 +94,7 @@ class WebpackConfig {
this.shouldUseSingleRuntimeChunk = null;
this.shouldSplitEntryChunks = false;
this.splitChunksConfigurationCallback = () => {};
this.watchOptionsConfigurationCallback = () => {};
this.vueLoaderOptionsCallback = () => {};
this.eslintLoaderOptionsCallback = () => {};
this.tsConfigurationCallback = () => {};
Expand Down Expand Up @@ -395,6 +396,14 @@ class WebpackConfig {
this.splitChunksConfigurationCallback = callback;
}

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

this.watchOptionsConfigurationCallback = 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
18 changes: 14 additions & 4 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class ConfigGenerator {
rules: this.buildRulesConfig(),
},
plugins: this.buildPluginsConfig(),
optimization: this.buildOptimizationConfig()
optimization: this.buildOptimizationConfig(),
watchOptions: this.buildWatchOptionsConfig()
};

if (this.webpackConfig.useSourceMaps) {
Expand Down Expand Up @@ -525,6 +526,17 @@ class ConfigGenerator {
return stats;
}

buildWatchOptionsConfig() {
const watchOptions = {
ignored: /node_modules/
};

return applyOptionsCallback(
this.webpackConfig.watchOptionsConfigurationCallback,
watchOptions
);
}

buildDevServerConfig() {
const contentBase = pathUtil.getContentBase(this.webpackConfig);

Expand All @@ -539,9 +551,7 @@ class ConfigGenerator {
quiet: true,
compress: true,
historyApiFallback: true,
watchOptions: {
ignored: /node_modules/
},
watchOptions: this.buildWatchOptionsConfig(),
https: this.webpackConfig.useDevServerInHttps()
};
}
Expand Down
25 changes: 25 additions & 0 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -1065,4 +1065,29 @@ describe('WebpackConfig object', () => {
}).to.throw('"foo" is not a valid key');
});
});

describe('configureWatchOptions()', () => {
it('Pass config', () => {
const config = createConfig();
const callback = (watchOptions) => {
watchOptions.poll = 250;
};

config.configureWatchOptions(callback);

expect(config.watchOptionsConfigurationCallback).to.equal(callback);
});

it('Call method without a valid callback', () => {
const config = createConfig();

expect(() => {
config.configureWatchOptions();
}).to.throw('Argument 1 to configureWatchOptions() must be a callback function.');

expect(() => {
config.configureWatchOptions({});
}).to.throw('Argument 1 to configureWatchOptions() must be a callback function.');
});
});
});
41 changes: 41 additions & 0 deletions test/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,30 @@ describe('The config-generator function', () => {
const actualConfig = configGenerator(config);
expect(actualConfig.devServer.hot).to.be.true;
});

it('devServer with custom 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;
});

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': 250,
});
});
});

describe('test for addPlugin config', () => {
Expand Down Expand Up @@ -934,4 +958,21 @@ describe('The config-generator function', () => {
expect(JSON.stringify(logger.getMessages().deprecation)).to.contain('the recommended setting is Encore.enableSingleRuntimeChunk()');
});
});

describe('Test buildWatchOptionsConfig()', () => {
it('Set webpack watch options', () => {
const config = createConfig();
config.outputPath = '/tmp/public/build';
config.setPublicPath('/build/');
config.configureWatchOptions(watchOptions => {
watchOptions.poll = 250;
});

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