diff --git a/index.js b/index.js index 7db82808..fa80e424 100644 --- a/index.js +++ b/index.js @@ -387,17 +387,22 @@ class Encore { * * For example: * - * Encore.addExternals({ - * jquery: 'jQuery', - * react: 'react' - * }) + * const nodeExternals = require('webpack-node-externals'); + * + * Encore.addExternals( + * { + * jquery: 'jQuery', + * react: 'react' + * }, + * nodeExternals() + * ); * - * @param {object} externals + * @param {...*} externals * * @returns {Encore} */ - addExternals(externals) { - webpackConfig.addExternals(externals); + addExternals(...externals) { + webpackConfig.addExternals(...externals); return this; } diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index fe54add2..356773d3 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -47,7 +47,7 @@ class WebpackConfig { this.providedVariables = {}; this.configuredFilenames = {}; this.aliases = {}; - this.externals = {}; + this.externals = []; // Features/Loaders flags this.useVersioning = false; @@ -298,12 +298,8 @@ class WebpackConfig { Object.assign(this.aliases, aliases); } - addExternals(externals = {}) { - if (typeof externals !== 'object') { - throw new Error('Argument 1 to addExternals() must be an object.'); - } - - Object.assign(this.externals, externals); + addExternals(...externals) { + this.externals = this.externals.concat(externals); } enableVersioning(enabled = true) { diff --git a/lib/config-generator.js b/lib/config-generator.js index 09400d9e..9004c461 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -107,7 +107,7 @@ class ConfigGenerator { config.resolve.alias['react-dom'] = 'preact-compat'; } - config.externals = Object.assign({}, this.webpackConfig.externals); + config.externals = [...this.webpackConfig.externals]; return config; } diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 2bd7e8a3..8a8984e0 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -940,24 +940,17 @@ describe('WebpackConfig object', () => { it('Adds new externals', () => { const config = createConfig(); - expect(config.externals).to.deep.equals({}); + expect(config.externals).to.deep.equals([]); config.addExternals({ 'jquery': 'jQuery', 'react': 'react' }); config.addExternals({ 'lodash': 'lodash' }); + config.addExternals(/^(jquery|\$)$/i); - expect(config.externals).to.deep.equals({ - 'jquery': 'jQuery', - 'react': 'react', - 'lodash': 'lodash' - }); - }); - - it('Calling it with an invalid argument', () => { - const config = createConfig(); - - expect(() => { - config.addExternals('foo'); - }).to.throw('must be an object'); + expect(config.externals).to.deep.equals([ + { 'jquery': 'jQuery', 'react': 'react'}, + { 'lodash': 'lodash' }, + /^(jquery|\$)$/i + ]); }); }); diff --git a/test/config-generator.js b/test/config-generator.js index 7c97875c..d6b2822e 100644 --- a/test/config-generator.js +++ b/test/config-generator.js @@ -471,7 +471,7 @@ describe('The config-generator function', () => { const actualConfig = configGenerator(config); - expect(actualConfig.externals).to.deep.equals({}); + expect(actualConfig.externals).to.deep.equals([]); }); it('with addExternals()', () => { @@ -485,10 +485,10 @@ describe('The config-generator function', () => { const actualConfig = configGenerator(config); - expect(actualConfig.externals).to.deep.equals({ + expect(actualConfig.externals).to.deep.equals([{ 'jquery': 'jQuery', 'react': 'react' - }); + }]); }); });