From 84a0dc0c63a7f4431a4d7f8fb7a48fcc28b2e0bb Mon Sep 17 00:00:00 2001 From: David Ellingsworth Date: Thu, 13 Dec 2018 14:19:42 -0500 Subject: [PATCH 1/6] Store externals in an array. This fixes #471 --- lib/WebpackConfig.js | 10 +++++----- lib/config-generator.js | 2 +- test/WebpackConfig.js | 19 +++++-------------- test/config-generator.js | 6 +++--- 4 files changed, 14 insertions(+), 23 deletions(-) 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..e16f6518 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..4b4a1bc1 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -940,24 +940,15 @@ 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' }); - 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' } + ]); }); }); 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' - }); + }]); }); }); From 3d6fb55fd62d97bd62bf9b6ced97045341b91ddb Mon Sep 17 00:00:00 2001 From: Vincent Le Biannic <850046+Lyrkan@users.noreply.github.com> Date: Fri, 14 Dec 2018 11:59:14 -0500 Subject: [PATCH 2/6] Ensure a copy of the externals is returned when generating a Webpack config. Co-Authored-By: deAtog --- lib/config-generator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/config-generator.js b/lib/config-generator.js index e16f6518..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 = this.webpackConfig.externals; + config.externals = [...this.webpackConfig.externals]; return config; } From 13b178010d43d4e3a0ca2ddf79cebcbc79c8f659 Mon Sep 17 00:00:00 2001 From: David Ellingsworth Date: Mon, 17 Dec 2018 17:51:01 -0500 Subject: [PATCH 3/6] Use ES6 rest parameters for the addExternals method. --- index.js | 4 ++-- lib/WebpackConfig.js | 6 +----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 7db82808..b8b8a0c6 100644 --- a/index.js +++ b/index.js @@ -396,8 +396,8 @@ class Encore { * * @returns {Encore} */ - addExternals(externals) { - webpackConfig.addExternals(externals); + addExternals(...externals) { + webpackConfig.addExternals.apply(webpackConfig, externals); return this; } diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index c7ac8f81..356773d3 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -298,11 +298,7 @@ class WebpackConfig { Object.assign(this.aliases, aliases); } - addExternals(externals = []) { - if (!Array.isArray(externals)) { - externals = [externals]; - } - + addExternals(...externals) { this.externals = this.externals.concat(externals); } From fc1e1f0eeca7baf48d978fab775ad7ea0d5016df Mon Sep 17 00:00:00 2001 From: David Ellingsworth Date: Mon, 17 Dec 2018 17:56:29 -0500 Subject: [PATCH 4/6] Update documentation for Encore.addExternals --- index.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index b8b8a0c6..686ffec0 100644 --- a/index.js +++ b/index.js @@ -387,12 +387,17 @@ 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} */ From 76e6a95a17dd6c0948679fce64d685446c2ee0ad Mon Sep 17 00:00:00 2001 From: David Ellingsworth Date: Wed, 2 Jan 2019 11:11:35 -0500 Subject: [PATCH 5/6] Use ES6 syntax for adding externals to be consistent with the new API --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 686ffec0..fa80e424 100644 --- a/index.js +++ b/index.js @@ -402,7 +402,7 @@ class Encore { * @returns {Encore} */ addExternals(...externals) { - webpackConfig.addExternals.apply(webpackConfig, externals); + webpackConfig.addExternals(...externals); return this; } From 4fc6dc31b38a6176ce02e4420a89df40b992d962 Mon Sep 17 00:00:00 2001 From: David Ellingsworth Date: Mon, 7 Jan 2019 15:38:40 -0500 Subject: [PATCH 6/6] Include a regex in the tests of addExternals. --- test/WebpackConfig.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 4b4a1bc1..8a8984e0 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -944,10 +944,12 @@ describe('WebpackConfig object', () => { 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' } + { 'lodash': 'lodash' }, + /^(jquery|\$)$/i ]); }); });