Skip to content

Commit 02ff319

Browse files
Ryan WeaverRyan Weaver
Ryan Weaver
authored and
Ryan Weaver
committed
feature #495 Fix support for Webpack externals. This fixes #471 (David Ellingsworth, Lyrkan, weaverryan)
This PR was merged into the master branch. Discussion ---------- Fix support for Webpack externals. This fixes #471 This is an alternative to #472 which does not modify the public Encore API and requires the user to pass an array if they want to add multiple externals at a time. Commits ------- 58dc86d tweaking example c1fbbf3 Split the addExternals example into two separate examples. c0a8d8d Use {*} instead of {object} for externals parameter of addExternals method. 0e012d3 Fix style issues identified by lint. 5ab6822 Include a regex in the tests of addExternals. a51b35f Update documentation for Encore.addExternals 3d6fb55 Ensure a copy of the externals is returned when generating a Webpack config. 84a0dc0 Store externals in an array. This fixes #471
2 parents 16022ba + 58dc86d commit 02ff319

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,19 @@ class Encore {
390390
* Encore.addExternals({
391391
* jquery: 'jQuery',
392392
* react: 'react'
393-
* })
393+
* });
394+
*
395+
* Or:
396+
*
397+
* const nodeExternals = require('webpack-node-externals');
398+
*
399+
* Encore.addExternals(
400+
* // add any valid externals you have
401+
* nodeExternals(),
402+
* /^(jquery|\$)$/i
403+
* );
394404
*
395-
* @param {object} externals
405+
* @param {*} externals
396406
*
397407
* @returns {Encore}
398408
*/

lib/WebpackConfig.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class WebpackConfig {
4747
this.providedVariables = {};
4848
this.configuredFilenames = {};
4949
this.aliases = {};
50-
this.externals = {};
50+
this.externals = [];
5151

5252
// Features/Loaders flags
5353
this.useVersioning = false;
@@ -299,12 +299,12 @@ class WebpackConfig {
299299
Object.assign(this.aliases, aliases);
300300
}
301301

302-
addExternals(externals = {}) {
303-
if (typeof externals !== 'object') {
304-
throw new Error('Argument 1 to addExternals() must be an object.');
302+
addExternals(externals = []) {
303+
if (!Array.isArray(externals)) {
304+
externals = [externals];
305305
}
306306

307-
Object.assign(this.externals, externals);
307+
this.externals = this.externals.concat(externals);
308308
}
309309

310310
enableVersioning(enabled = true) {

lib/config-generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class ConfigGenerator {
108108
config.resolve.alias['react-dom'] = 'preact-compat';
109109
}
110110

111-
config.externals = Object.assign({}, this.webpackConfig.externals);
111+
config.externals = [...this.webpackConfig.externals];
112112

113113
return config;
114114
}

test/WebpackConfig.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -940,24 +940,17 @@ describe('WebpackConfig object', () => {
940940
it('Adds new externals', () => {
941941
const config = createConfig();
942942

943-
expect(config.externals).to.deep.equals({});
943+
expect(config.externals).to.deep.equals([]);
944944

945945
config.addExternals({ 'jquery': 'jQuery', 'react': 'react' });
946946
config.addExternals({ 'lodash': 'lodash' });
947+
config.addExternals(/^(jquery|\$)$/i);
947948

948-
expect(config.externals).to.deep.equals({
949-
'jquery': 'jQuery',
950-
'react': 'react',
951-
'lodash': 'lodash'
952-
});
953-
});
954-
955-
it('Calling it with an invalid argument', () => {
956-
const config = createConfig();
957-
958-
expect(() => {
959-
config.addExternals('foo');
960-
}).to.throw('must be an object');
949+
expect(config.externals).to.deep.equals([
950+
{ 'jquery': 'jQuery', 'react': 'react' },
951+
{ 'lodash': 'lodash' },
952+
/^(jquery|\$)$/i
953+
]);
961954
});
962955
});
963956

test/config-generator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ describe('The config-generator function', () => {
471471

472472
const actualConfig = configGenerator(config);
473473

474-
expect(actualConfig.externals).to.deep.equals({});
474+
expect(actualConfig.externals).to.deep.equals([]);
475475
});
476476

477477
it('with addExternals()', () => {
@@ -485,10 +485,10 @@ describe('The config-generator function', () => {
485485

486486
const actualConfig = configGenerator(config);
487487

488-
expect(actualConfig.externals).to.deep.equals({
488+
expect(actualConfig.externals).to.deep.equals([{
489489
'jquery': 'jQuery',
490490
'react': 'react'
491-
});
491+
}]);
492492
});
493493
});
494494

0 commit comments

Comments
 (0)