Skip to content

Commit

Permalink
Make mix.webpackConfig() run *after* extensions - closes #2439
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Dec 30, 2020
1 parent ff9ff51 commit b112e13
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
13 changes: 0 additions & 13 deletions src/builder/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class WebpackConfig {
await this.buildRules();
await this.buildPlugins();
this.buildChunks();
this.mergeCustomConfig();

// We'll announce that the core config object has been
// generated by Mix. At this point, any plugins may
Expand Down Expand Up @@ -175,18 +174,6 @@ class WebpackConfig {
this.chunks.config()
);
}

/**
* Merge the user's custom Webpack configuration.
*/
mergeCustomConfig() {
if (Config.webpackConfig) {
this.webpackConfig = require('./MergeWebpackConfig')(
this.webpackConfig,
Config.webpackConfig
);
}
}
}

module.exports = WebpackConfig;
13 changes: 5 additions & 8 deletions src/components/WebpackConfig.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
let webpack = require('webpack');
let merge = require('../builder/MergeWebpackConfig');

class WebpackConfig {
register(config) {
config = typeof config == 'function' ? config(webpack) : config;
mix.override(webpackConfig => {
config = typeof config == 'function' ? config(webpackConfig) : config;

Config.webpackConfig = require('../builder/MergeWebpackConfig')(
Config.webpackConfig,
config
);

return this;
Object.assign(webpackConfig, merge(webpackConfig, config));
});
}
}

Expand Down
38 changes: 18 additions & 20 deletions test/features/webpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,13 @@ import test from 'ava';
import '../helpers/mix';
import { buildConfig } from '../helpers/webpack';

test('mix.webpackConfig()', t => {
// Test config passed as an object.
let config = { context: 'changed' };
let response = mix.webpackConfig(config);

t.deepEqual(mix, response);

t.deepEqual(config, Config.webpackConfig);

// Test config passed via a callback.
config = { context: 'changed again' };
response = mix.webpackConfig(webpack => config);

t.deepEqual(mix, response);

t.deepEqual(config, Config.webpackConfig);
});

test('Custom user config can be merged', async t => {
test('Custom webpack config can be merged', async t => {
mix.webpackConfig({ context: 'changed' });

t.is('changed', (await buildConfig()).context);
});

test('Custom user config can be merged as a callback function', async t => {
test('Custom webpack config can be merged as a callback function', async t => {
mix.webpackConfig(webpack => {
return {
context: 'changed'
Expand All @@ -36,3 +18,19 @@ test('Custom user config can be merged as a callback function', async t => {

t.is('changed', (await buildConfig()).context);
});

test('Custom webpack config is called and merged *after* all plugins and extensions', async t => {
mix.extend('extension', {
webpackConfig(config) {
config.foo = 'extension foo';
}
});

mix.extension().webpackConfig(() => {
return {
foo: 'webpackConfig foo'
};
});

t.is('webpackConfig foo', (await buildConfig()).foo);
});

1 comment on commit b112e13

@JeffreyWay
Copy link
Collaborator Author

@JeffreyWay JeffreyWay commented on b112e13 Dec 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note - there's a potentially breaking change here. The callback to mix.webpackConfig() will now receive the webpackConfig object instead of require('webpack'). I'm not sure if anyone was using that, but it's something to be aware of.

Please sign in to comment.