diff --git a/index.js b/index.js index 7db82808..696e2209 100644 --- a/index.js +++ b/index.js @@ -390,9 +390,19 @@ class Encore { * Encore.addExternals({ * jquery: 'jQuery', * react: 'react' - * }) + * }); + * + * Or: + * + * const nodeExternals = require('webpack-node-externals'); + * + * Encore.addExternals( + * // add any valid externals you have + * nodeExternals(), + * /^(jquery|\$)$/i + * ); * - * @param {object} externals + * @param {*} externals * * @returns {Encore} */ diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index fe54add2..c7ac8f81 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,12 @@ class WebpackConfig { Object.assign(this.aliases, aliases); } - addExternals(externals = {}) { - if (typeof externals !== 'object') { - throw new Error('Argument 1 to addExternals() must be an object.'); + addExternals(externals = []) { + if (!Array.isArray(externals)) { + externals = [externals]; } - Object.assign(this.externals, 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..eeba83f8 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' - }); + }]); }); });