Skip to content

Commit

Permalink
bug #627 Allow to overwrite pre-defined aliases using addAliases() (L…
Browse files Browse the repository at this point in the history
…yrkan)

This PR was merged into the master branch.

Discussion
----------

Allow to overwrite pre-defined aliases using addAliases()

Currently pre-defined aliases (used by `vue` and `preact-compat`) are added after the ones from `Encore.addAliases(...)`, which prevents overwriting them using this method.

It makes more sense to change that order so aliases from `Encore.addAliases(...)` are always added last (fixes #625).

Commits
-------

29d1cb4 Allow to overwrite pre-defined aliases using addAliases()
  • Loading branch information
weaverryan committed Oct 7, 2019
2 parents 5640271 + 29d1cb4 commit 8360f1c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/config-generator.js
Expand Up @@ -96,7 +96,7 @@ class ConfigGenerator {

config.resolve = {
extensions: ['.wasm', '.mjs', '.js', '.json', '.jsx', '.vue', '.ts', '.tsx'],
alias: Object.assign({}, this.webpackConfig.aliases)
alias: {}
};

if (this.webpackConfig.useVueLoader) {
Expand All @@ -108,6 +108,8 @@ class ConfigGenerator {
config.resolve.alias['react-dom'] = 'preact-compat';
}

Object.assign(config.resolve.alias, this.webpackConfig.aliases);

config.externals = [...this.webpackConfig.externals];

return config;
Expand Down
22 changes: 22 additions & 0 deletions test/config-generator.js
Expand Up @@ -453,6 +453,28 @@ describe('The config-generator function', () => {
'testB': 'src/testB'
});
});

it('with addAliases() that overwrites pre-defined aliases', () => {
const config = createConfig();
config.outputPath = '/tmp/output/public-path';
config.publicPath = '/public-path';
config.enableVueLoader(); // Adds the 'vue$' alias
config.enablePreactPreset({ preactCompat: true }); // Adds the 'react' and 'react-dom' aliases
config.addAliases({
'foo': 'bar',
'vue$': 'new-vue$',
'react-dom': 'new-react-dom',
});

const actualConfig = configGenerator(config);

expect(actualConfig.resolve.alias).to.deep.equals({
'foo': 'bar',
'vue$': 'new-vue$',
'react-dom': 'new-react-dom',
'react': 'preact-compat' // Keeps predefined aliases that are not overwritten
});
});
});

describe('addExternals() adds new externals', () => {
Expand Down

0 comments on commit 8360f1c

Please sign in to comment.