From ab9b4471e7a91d78043b4d10f4bf323272bc5a31 Mon Sep 17 00:00:00 2001 From: Sergey Melyukov Date: Wed, 18 Mar 2020 11:21:28 +0300 Subject: [PATCH 1/2] feat: support entry descriptor (closes #2453) --- lib/utils/addEntries.js | 10 +++++++- test/fixtures/entry-as-descriptor/foo.js | 3 +++ .../entry-as-descriptor/webpack.config.js | 24 +++++++++++++++++++ test/server/utils/addEntries.test.js | 12 ++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/entry-as-descriptor/foo.js create mode 100644 test/fixtures/entry-as-descriptor/webpack.config.js diff --git a/lib/utils/addEntries.js b/lib/utils/addEntries.js index 501b12e080..792fae03e6 100644 --- a/lib/utils/addEntries.js +++ b/lib/utils/addEntries.js @@ -68,7 +68,15 @@ function addEntries(config, options, server) { Object.keys(originalEntry).forEach((key) => { // entry[key] should be a string here - clone[key] = prependEntry(originalEntry[key], additionalEntries); + const entryDescription = originalEntry[key]; + if (typeof entryDescription === 'object' && entryDescription.import) { + clone[key] = { + ...entryDescription, + import: prependEntry(entryDescription.import, additionalEntries), + }; + } else { + clone[key] = prependEntry(entryDescription, additionalEntries); + } }); return clone; diff --git a/test/fixtures/entry-as-descriptor/foo.js b/test/fixtures/entry-as-descriptor/foo.js new file mode 100644 index 0000000000..f88d8b5040 --- /dev/null +++ b/test/fixtures/entry-as-descriptor/foo.js @@ -0,0 +1,3 @@ +'use strict'; + +console.log('i am foo!'); diff --git a/test/fixtures/entry-as-descriptor/webpack.config.js b/test/fixtures/entry-as-descriptor/webpack.config.js new file mode 100644 index 0000000000..32ae8c6cae --- /dev/null +++ b/test/fixtures/entry-as-descriptor/webpack.config.js @@ -0,0 +1,24 @@ +'use strict'; + +module.exports = { + mode: 'development', + context: __dirname, + entry: { + main: { + import: './foo.js', + }, + }, + plugins: [ + { + apply(compiler) { + compiler.hooks.done.tap('webpack-dev-server', (stats) => { + let exitCode = 0; + if (stats.hasErrors()) { + exitCode = 1; + } + setTimeout(() => process.exit(exitCode)); + }); + }, + }, + ], +}; diff --git a/test/server/utils/addEntries.test.js b/test/server/utils/addEntries.test.js index 1bc6a6cef6..a5a375da2a 100644 --- a/test/server/utils/addEntries.test.js +++ b/test/server/utils/addEntries.test.js @@ -5,6 +5,7 @@ const webpack = require('webpack'); const addEntries = require('../../../lib/utils/addEntries'); const config = require('./../../fixtures/simple-config/webpack.config'); const configEntryAsFunction = require('./../../fixtures/entry-as-function/webpack.config'); +const configEntryAsDescriptor = require('./../../fixtures/entry-as-descriptor/webpack.config'); const normalize = (entry) => entry.split(path.sep).join('/'); @@ -290,6 +291,17 @@ describe('addEntries util', () => { expect(typeof webpackOptions.entry === 'function').toBe(true); }); + it('should supports entry as descriptor', () => { + const webpackOptions = Object.assign({}, configEntryAsDescriptor); + const devServerOptions = {}; + + addEntries(webpackOptions, devServerOptions); + + expect(typeof webpackOptions.entry === 'object').toBe(true); + expect(typeof webpackOptions.entry.main === 'object').toBe(true); + expect(Array.isArray(webpackOptions.entry.main.import)).toBe(true); + }); + it('should only prepends devServer entry points to web targets by default', () => { const webpackOptions = [ Object.assign({}, config), From 9f28cd8fd941721fbd1e1eb976d848e954aa6eb5 Mon Sep 17 00:00:00 2001 From: Sergey Melyukov Date: Wed, 18 Mar 2020 14:59:57 +0300 Subject: [PATCH 2/2] feat: spread to object.assign --- lib/utils/addEntries.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/utils/addEntries.js b/lib/utils/addEntries.js index 792fae03e6..7eaac33003 100644 --- a/lib/utils/addEntries.js +++ b/lib/utils/addEntries.js @@ -70,10 +70,9 @@ function addEntries(config, options, server) { // entry[key] should be a string here const entryDescription = originalEntry[key]; if (typeof entryDescription === 'object' && entryDescription.import) { - clone[key] = { - ...entryDescription, + clone[key] = Object.assign({}, entryDescription, { import: prependEntry(entryDescription.import, additionalEntries), - }; + }); } else { clone[key] = prependEntry(entryDescription, additionalEntries); }