Skip to content

Commit 5689d2e

Browse files
committed
feature #217 Add Encore.addAliases and Encore.addExternals methods (Lyrkan)
This PR was merged into the master branch. Discussion ---------- Add Encore.addAliases and Encore.addExternals methods This PR adds two new methods to the API (closes #200): * **`Encore.addAliases`**: Adds the given values to the `resolve.alias` setting of the generated config * **`Encore.addExternals`**: Adds the given values to the `external` setting of the generated config Commits ------- eefe8a4 Add Encore.addAliases and Encore.addExternals methods
2 parents 36b8852 + eefe8a4 commit 5689d2e

File tree

6 files changed

+192
-1
lines changed

6 files changed

+192
-1
lines changed

index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,51 @@ const publicApi = {
328328
return this;
329329
},
330330

331+
/**
332+
* Allow you to add aliases that will be used by
333+
* Webpack when trying to resolve modules.
334+
*
335+
* See https://webpack.js.org/configuration/resolve/#resolve-alias
336+
*
337+
* For example:
338+
*
339+
* Encore.addAliases({
340+
* Utilities: path.resolve(__dirname, 'src/utilities/'),
341+
* Templates: path.resolve(__dirname, 'src/templates/')
342+
* })
343+
*
344+
* @param {object} aliases
345+
*
346+
* @returns {exports}
347+
*/
348+
addAliases(aliases) {
349+
webpackConfig.addAliases(aliases);
350+
351+
return this;
352+
},
353+
354+
/**
355+
* Allow you to exclude some dependencies from the output bundles.
356+
*
357+
* See https://webpack.js.org/configuration/externals/
358+
*
359+
* For example:
360+
*
361+
* Encore.addExternals({
362+
* jquery: 'jQuery',
363+
* react: 'react'
364+
* })
365+
*
366+
* @param {object} externals
367+
*
368+
* @returns {exports}
369+
*/
370+
addExternals(externals) {
371+
webpackConfig.addExternals(externals);
372+
373+
return this;
374+
},
375+
331376
/**
332377
* When enabled, files are rendered with a hash based
333378
* on their contents (e.g. main.a2b61cc.js)

lib/WebpackConfig.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class WebpackConfig {
4545
this.sharedCommonsEntryName = null;
4646
this.providedVariables = {};
4747
this.configuredFilenames = {};
48+
this.aliases = {};
49+
this.externals = {};
4850

4951
// Features/Loaders flags
5052
this.useVersioning = false;
@@ -271,6 +273,22 @@ class WebpackConfig {
271273
this.loaders.push(loader);
272274
}
273275

276+
addAliases(aliases = {}) {
277+
if (typeof aliases !== 'object') {
278+
throw new Error('Argument 1 to addAliases() must be an object.');
279+
}
280+
281+
Object.assign(this.aliases, aliases);
282+
}
283+
284+
addExternals(externals = {}) {
285+
if (typeof externals !== 'object') {
286+
throw new Error('Argument 1 to addExternals() must be an object.');
287+
}
288+
289+
Object.assign(this.externals, externals);
290+
}
291+
274292
enableVersioning(enabled = true) {
275293
this.useVersioning = enabled;
276294
}

lib/config-generator.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ConfigGenerator {
7777

7878
config.resolve = {
7979
extensions: ['.js', '.jsx', '.vue', '.ts', '.tsx'],
80-
alias: {}
80+
alias: this.webpackConfig.aliases
8181
};
8282

8383
if (this.webpackConfig.useVueLoader) {
@@ -89,6 +89,8 @@ class ConfigGenerator {
8989
config.resolve.alias['react-dom'] = 'preact-compat';
9090
}
9191

92+
config.externals = this.webpackConfig.externals;
93+
9294
return config;
9395
}
9496

test/WebpackConfig.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,56 @@ describe('WebpackConfig object', () => {
709709
});
710710
});
711711

712+
describe('addAliases', () => {
713+
it('Adds new aliases', () => {
714+
const config = createConfig();
715+
716+
expect(config.aliases).to.deep.equals({});
717+
718+
config.addAliases({ 'testA': 'src/testA', 'testB': 'src/testB' });
719+
config.addAliases({ 'testC': 'src/testC' });
720+
721+
expect(config.aliases).to.deep.equals({
722+
'testA': 'src/testA',
723+
'testB': 'src/testB',
724+
'testC': 'src/testC'
725+
});
726+
});
727+
728+
it('Calling it with an invalid argument', () => {
729+
const config = createConfig();
730+
731+
expect(() => {
732+
config.addAliases('foo');
733+
}).to.throw('must be an object');
734+
});
735+
});
736+
737+
describe('addExternals', () => {
738+
it('Adds new externals', () => {
739+
const config = createConfig();
740+
741+
expect(config.externals).to.deep.equals({});
742+
743+
config.addExternals({ 'jquery': 'jQuery', 'react': 'react' });
744+
config.addExternals({ 'lodash': 'lodash' });
745+
746+
expect(config.externals).to.deep.equals({
747+
'jquery': 'jQuery',
748+
'react': 'react',
749+
'lodash': 'lodash'
750+
});
751+
});
752+
753+
it('Calling it with an invalid argument', () => {
754+
const config = createConfig();
755+
756+
expect(() => {
757+
config.addExternals('foo');
758+
}).to.throw('must be an object');
759+
});
760+
});
761+
712762
describe('disableImagesLoader', () => {
713763
it('Disable default images loader', () => {
714764
const config = createConfig();

test/config-generator.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,64 @@ describe('The config-generator function', () => {
346346
});
347347
});
348348

349+
describe('addAliases() adds new aliases', () => {
350+
it('without addAliases()', () => {
351+
const config = createConfig();
352+
config.outputPath = '/tmp/output/public-path';
353+
config.publicPath = '/public-path';
354+
355+
const actualConfig = configGenerator(config);
356+
357+
expect(actualConfig.resolve.alias).to.deep.equals({});
358+
});
359+
360+
it('with addAliases()', () => {
361+
const config = createConfig();
362+
config.outputPath = '/tmp/output/public-path';
363+
config.publicPath = '/public-path';
364+
config.addAliases({
365+
'testA': 'src/testA',
366+
'testB': 'src/testB'
367+
});
368+
369+
const actualConfig = configGenerator(config);
370+
371+
expect(actualConfig.resolve.alias).to.deep.equals({
372+
'testA': 'src/testA',
373+
'testB': 'src/testB'
374+
});
375+
});
376+
});
377+
378+
describe('addExternals() adds new externals', () => {
379+
it('without addExternals()', () => {
380+
const config = createConfig();
381+
config.outputPath = '/tmp/output/public-path';
382+
config.publicPath = '/public-path';
383+
384+
const actualConfig = configGenerator(config);
385+
386+
expect(actualConfig.externals).to.deep.equals({});
387+
});
388+
389+
it('with addExternals()', () => {
390+
const config = createConfig();
391+
config.outputPath = '/tmp/output/public-path';
392+
config.publicPath = '/public-path';
393+
config.addExternals({
394+
'jquery': 'jQuery',
395+
'react': 'react'
396+
});
397+
398+
const actualConfig = configGenerator(config);
399+
400+
expect(actualConfig.externals).to.deep.equals({
401+
'jquery': 'jQuery',
402+
'react': 'react'
403+
});
404+
});
405+
});
406+
349407
describe('.js rule receives different configuration', () => {
350408
it('Use default config', () => {
351409
const config = createConfig();

test/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ describe('Public API', () => {
8989

9090
});
9191

92+
describe('addAliases', () => {
93+
94+
it('must return the API object', () => {
95+
const returnedValue = api.addAliases({});
96+
expect(returnedValue).to.equal(api);
97+
});
98+
99+
});
100+
101+
describe('addExternals', () => {
102+
103+
it('must return the API object', () => {
104+
const returnedValue = api.addExternals({});
105+
expect(returnedValue).to.equal(api);
106+
});
107+
108+
});
109+
92110
describe('enableVersioning', () => {
93111

94112
it('must return the API object', () => {

0 commit comments

Comments
 (0)