From 34abd1baf6ff682bd410b3eca0c33b1a45b60182 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Thu, 21 May 2020 00:18:14 +0530 Subject: [PATCH 01/12] =?UTF-8?q?fix:=20=F0=9F=90=9B=20do=20not=20apply=20?= =?UTF-8?q?own=20defaults=20while=20setting=20mode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webpack-cli/lib/groups/ZeroConfigGroup.js | 24 ++----------------- .../lib/utils/development-config.js | 13 ---------- packages/webpack-cli/lib/utils/none-config.js | 13 ---------- .../lib/utils/production-config.js | 13 ---------- 4 files changed, 2 insertions(+), 61 deletions(-) delete mode 100644 packages/webpack-cli/lib/utils/development-config.js delete mode 100644 packages/webpack-cli/lib/utils/none-config.js delete mode 100644 packages/webpack-cli/lib/utils/production-config.js diff --git a/packages/webpack-cli/lib/groups/ZeroConfigGroup.js b/packages/webpack-cli/lib/groups/ZeroConfigGroup.js index 94fff84b6b1..e98803d7d93 100644 --- a/packages/webpack-cli/lib/groups/ZeroConfigGroup.js +++ b/packages/webpack-cli/lib/groups/ZeroConfigGroup.js @@ -16,7 +16,7 @@ class ZeroConfigGroup extends GroupHelper { * It determines the mode to pass to webpack compiler * @returns {string} The mode */ - getEnvFromOptionsAndMode() { + resolveMode() { if (process.env.NODE_ENV && (process.env.NODE_ENV === PRODUCTION || process.env.NODE_ENV === DEVELOPMENT)) { return process.env.NODE_ENV; } else { @@ -44,28 +44,8 @@ class ZeroConfigGroup extends GroupHelper { } } - resolveZeroConfig() { - const defaultConfigType = this.getEnvFromOptionsAndMode(); - let defaultConfig; - if (defaultConfigType === PRODUCTION) { - defaultConfig = require('../utils/production-config')(); - } else if (defaultConfigType === DEVELOPMENT) { - defaultConfig = require('../utils/development-config')(); - } else { - defaultConfig = require('../utils/none-config')(); - } - - const isEntryObject = defaultConfig.entry && defaultConfig.entry instanceof Object; - const isOutputDefined = defaultConfig.output && defaultConfig.output.filename; - const isConflictingOutput = isEntryObject && isOutputDefined && defaultConfig.output.filename === 'main.js'; - if (isConflictingOutput) { - defaultConfig.output.filename = '[name].main.js'; - } - this.opts.options = defaultConfig; - } - run() { - this.resolveZeroConfig(); + this.opts.options.mode = this.resolveMode(); return this.opts; } } diff --git a/packages/webpack-cli/lib/utils/development-config.js b/packages/webpack-cli/lib/utils/development-config.js deleted file mode 100644 index c776a931e90..00000000000 --- a/packages/webpack-cli/lib/utils/development-config.js +++ /dev/null @@ -1,13 +0,0 @@ -const { join, resolve } = require('path'); - -const defaultOutputPath = resolve(join(process.cwd(), 'dist')); - -module.exports = () => ({ - mode: 'development', - entry: './index.js', - devtool: 'eval', - output: { - path: defaultOutputPath, - filename: 'main.js', - }, -}); diff --git a/packages/webpack-cli/lib/utils/none-config.js b/packages/webpack-cli/lib/utils/none-config.js deleted file mode 100644 index 7d6ffe1cbf6..00000000000 --- a/packages/webpack-cli/lib/utils/none-config.js +++ /dev/null @@ -1,13 +0,0 @@ -const { join, resolve } = require('path'); - -const defaultOutputPath = resolve(join(process.cwd(), 'dist')); - -module.exports = () => ({ - mode: 'none', - entry: './index.js', - devtool: 'eval', - output: { - path: defaultOutputPath, - filename: 'main.js', - }, -}); diff --git a/packages/webpack-cli/lib/utils/production-config.js b/packages/webpack-cli/lib/utils/production-config.js deleted file mode 100644 index f7525d63936..00000000000 --- a/packages/webpack-cli/lib/utils/production-config.js +++ /dev/null @@ -1,13 +0,0 @@ -const { join, resolve } = require('path'); - -const defaultOutputPath = resolve(join(process.cwd(), 'dist')); - -module.exports = () => ({ - mode: 'production', - entry: './index.js', - devtool: 'source-map', - output: { - path: defaultOutputPath, - filename: 'main.js', - }, -}); From adc7472a33577d8fdb72134d91c3281ea2c9a7a2 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Thu, 21 May 2020 20:15:22 +0530 Subject: [PATCH 02/12] tests: add tests to ensure no additional properties in mode --- .../webpack-cli/__tests__/ZeroConfigGroup.test.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/webpack-cli/__tests__/ZeroConfigGroup.test.js b/packages/webpack-cli/__tests__/ZeroConfigGroup.test.js index ae9497d6671..83c3fbc8011 100644 --- a/packages/webpack-cli/__tests__/ZeroConfigGroup.test.js +++ b/packages/webpack-cli/__tests__/ZeroConfigGroup.test.js @@ -1,7 +1,7 @@ const ZeroConfigGroup = require('../lib/groups/ZeroConfigGroup'); -describe('GroupHelper', function () { - it('should load the dev zero config', () => { +describe('ZeroConfigGroup', function () { + it('should load the dev zero config', () => { const group = new ZeroConfigGroup([ { dev: true, @@ -27,8 +27,9 @@ describe('GroupHelper', function () { mode: 'production', }, ]); - const result = group.run(); + // ensure no other properties are added + expect(result.options).toMatchObject({ mode: 'production' }); expect(result.options.mode).toEqual('production'); }); @@ -40,6 +41,8 @@ describe('GroupHelper', function () { ]); const result = group.run(); + // ensure no other properties are added + expect(result.options).toMatchObject({ mode: 'development' }); expect(result.options.mode).toEqual('development'); }); @@ -51,6 +54,8 @@ describe('GroupHelper', function () { ]); const result = group.run(); + // ensure no other properties are added + expect(result.options).toMatchObject({ mode: 'none' }); expect(result.options.mode).toEqual('none'); }); }); From 9530e994d61007a19dae5234d42ee403779fde76 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Fri, 22 May 2020 01:37:45 +0530 Subject: [PATCH 03/12] tests: add tests to ensure no additional properties in mode --- test/mode/mode-with-config/.gitignore | 3 + test/mode/mode-with-config/index.js | 10 ++ .../mode-with-config/mode-with-config.test.js | 94 +++++++++++++++++++ test/mode/mode-with-config/package.json | 6 ++ test/mode/mode-with-config/webpack.config.js | 27 ++++++ 5 files changed, 140 insertions(+) create mode 100644 test/mode/mode-with-config/.gitignore create mode 100644 test/mode/mode-with-config/index.js create mode 100644 test/mode/mode-with-config/mode-with-config.test.js create mode 100644 test/mode/mode-with-config/package.json create mode 100644 test/mode/mode-with-config/webpack.config.js diff --git a/test/mode/mode-with-config/.gitignore b/test/mode/mode-with-config/.gitignore new file mode 100644 index 00000000000..9716e5f1673 --- /dev/null +++ b/test/mode/mode-with-config/.gitignore @@ -0,0 +1,3 @@ +yarn.lock +package-lock.json +node_modules/ diff --git a/test/mode/mode-with-config/index.js b/test/mode/mode-with-config/index.js new file mode 100644 index 00000000000..6d7382df589 --- /dev/null +++ b/test/mode/mode-with-config/index.js @@ -0,0 +1,10 @@ +require("react") +console.log("Ichigo") +if (process.env.NODE_ENV === "production") { + console.log("production mode") +} else if (process.env.NODE_ENV === "development") { + console.log(console.log("development mode")) +} else { + console.log(console.log("none mode")) +} + diff --git a/test/mode/mode-with-config/mode-with-config.test.js b/test/mode/mode-with-config/mode-with-config.test.js new file mode 100644 index 00000000000..c23b65ce469 --- /dev/null +++ b/test/mode/mode-with-config/mode-with-config.test.js @@ -0,0 +1,94 @@ +'use strict'; +const { run } = require('../../utils/test-utils'); +const { stat, readFile } = require('fs'); +const { resolve } = require('path'); + +describe('mode flags with config', () => { + it('should run in production mode when --mode=production is passed', (done) => { + const { stderr, stdout } = run(__dirname, ['--mode', 'production', '--config', './webpack.config.js']); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + + // Should generate the appropriate files + stat(resolve(__dirname, './bin/main.js.OTHER.LICENSE.txt'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + }); + + stat(resolve(__dirname, './bin/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + }); + + // Should not generate source maps when not specified + stat(resolve(__dirname, './bin/main.js.map'), (err, stats) => { + expect(err).toBeTruthy(); + }); + + // Correct mode should be propagated to the compiler + readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => { + expect(err).toBe(null); + expect(data).toContain('"production mode"'); + done(); + }); + }); + + it('should run in development mode when --mode=development is passed', (done) => { + const { stderr, stdout } = run(__dirname, ['--mode', 'development', '--config', './webpack.config.js']); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + + // Should generate the appropriate files + stat(resolve(__dirname, './bin/main.js.OTHER.LICENSE.txt'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + }); + + stat(resolve(__dirname, './bin/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + }); + + // Should not generate source maps when not specified + stat(resolve(__dirname, './bin/main.js.map'), (err, stats) => { + expect(err).toBeTruthy(); + }); + + // Correct mode should be propagated to the compiler + readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => { + expect(err).toBe(null); + expect(data).toContain('The "eval" devtool has been used'); + expect(data).toContain('development mode'); + done(); + }); + }); + + it('should run in none mode when --mode=none is passed', (done) => { + const { stderr, stdout } = run(__dirname, ['--mode', 'none', '--config', './webpack.config.js']); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + + // Should generate the appropriate files + stat(resolve(__dirname, './bin/main.js.OTHER.LICENSE.txt'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + }); + + stat(resolve(__dirname, './bin/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + }); + + // Should not generate source maps when not specified + stat(resolve(__dirname, './bin/main.js.map'), (err, stats) => { + expect(err).toBeTruthy(); + }); + + // Correct mode should be propagated to the compiler + readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => { + expect(err).toBe(null); + expect(data).toContain('none mode'); + done(); + }); + }); +}); diff --git a/test/mode/mode-with-config/package.json b/test/mode/mode-with-config/package.json new file mode 100644 index 00000000000..e0f712a0ac4 --- /dev/null +++ b/test/mode/mode-with-config/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "terser-webpack-plugin": "^3.0.1", + "react": "^16.13.1" + } +} diff --git a/test/mode/mode-with-config/webpack.config.js b/test/mode/mode-with-config/webpack.config.js new file mode 100644 index 00000000000..a2af940a34c --- /dev/null +++ b/test/mode/mode-with-config/webpack.config.js @@ -0,0 +1,27 @@ +const path = require('path'); +const dirname = __dirname; +const TerserPlugin = require('terser-webpack-plugin'); + +module.exports = () => { + const config = { + entry: './index.js', + output: { + path: path.join(dirname, 'dist'), + filename: '[name].js', + ecmaVersion: 5, + }, + optimization: { + minimizer: [ + new TerserPlugin({ + sourceMap: false, + extractComments: { + filename: (fileData) => { + return `${fileData.filename}.OTHER.LICENSE.txt${fileData.query}`; + }, + }, + }), + ], + }, + }; + return config; +}; From 86989c255b9f16c7da8be325f3845e86babf8532 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Fri, 22 May 2020 01:42:54 +0530 Subject: [PATCH 04/12] tests: add tests to ensure no additional properties in mode --- test/mode/mode-with-config/mode-with-config.test.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/mode/mode-with-config/mode-with-config.test.js b/test/mode/mode-with-config/mode-with-config.test.js index c23b65ce469..49ad85bdf81 100644 --- a/test/mode/mode-with-config/mode-with-config.test.js +++ b/test/mode/mode-with-config/mode-with-config.test.js @@ -1,7 +1,8 @@ 'use strict'; -const { run } = require('../../utils/test-utils'); const { stat, readFile } = require('fs'); const { resolve } = require('path'); +// eslint-disable-next-line node/no-unpublished-require +const { run } = require('../../utils/test-utils'); describe('mode flags with config', () => { it('should run in production mode when --mode=production is passed', (done) => { @@ -21,7 +22,7 @@ describe('mode flags with config', () => { }); // Should not generate source maps when not specified - stat(resolve(__dirname, './bin/main.js.map'), (err, stats) => { + stat(resolve(__dirname, './bin/main.js.map'), (err) => { expect(err).toBeTruthy(); }); @@ -50,7 +51,7 @@ describe('mode flags with config', () => { }); // Should not generate source maps when not specified - stat(resolve(__dirname, './bin/main.js.map'), (err, stats) => { + stat(resolve(__dirname, './bin/main.js.map'), (err) => { expect(err).toBeTruthy(); }); @@ -80,7 +81,7 @@ describe('mode flags with config', () => { }); // Should not generate source maps when not specified - stat(resolve(__dirname, './bin/main.js.map'), (err, stats) => { + stat(resolve(__dirname, './bin/main.js.map'), (err) => { expect(err).toBeTruthy(); }); From 630dea5fe04e8d16f25dfa55b68c6c5987558589 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Fri, 22 May 2020 01:47:55 +0530 Subject: [PATCH 05/12] tests: add tests to ensure no additional properties in mode --- test/mode/mode-with-config/webpack.config.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/mode/mode-with-config/webpack.config.js b/test/mode/mode-with-config/webpack.config.js index a2af940a34c..7c659c4efd2 100644 --- a/test/mode/mode-with-config/webpack.config.js +++ b/test/mode/mode-with-config/webpack.config.js @@ -8,7 +8,6 @@ module.exports = () => { output: { path: path.join(dirname, 'dist'), filename: '[name].js', - ecmaVersion: 5, }, optimization: { minimizer: [ From 6a6614f8867747bcf233f7d0b5cac8efee65c5d9 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Fri, 22 May 2020 10:46:30 +0530 Subject: [PATCH 06/12] tests: add tests to ensure no additional properties in mode --- test/mode/mode-with-config/mode-with-config.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mode/mode-with-config/mode-with-config.test.js b/test/mode/mode-with-config/mode-with-config.test.js index 49ad85bdf81..c91872c3cd9 100644 --- a/test/mode/mode-with-config/mode-with-config.test.js +++ b/test/mode/mode-with-config/mode-with-config.test.js @@ -4,7 +4,7 @@ const { resolve } = require('path'); // eslint-disable-next-line node/no-unpublished-require const { run } = require('../../utils/test-utils'); -describe('mode flags with config', () => { +describe.skip('mode flags with config', () => { it('should run in production mode when --mode=production is passed', (done) => { const { stderr, stdout } = run(__dirname, ['--mode', 'production', '--config', './webpack.config.js']); expect(stderr).toBeFalsy(); From 4980c43434c189ebb342f160289ad7eb99d07a0d Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Fri, 22 May 2020 11:15:53 +0530 Subject: [PATCH 07/12] tests: add tests to ensure no additional properties in mode --- test/mode/mode-with-config/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mode/mode-with-config/package.json b/test/mode/mode-with-config/package.json index e0f712a0ac4..22ba3e0ed63 100644 --- a/test/mode/mode-with-config/package.json +++ b/test/mode/mode-with-config/package.json @@ -1,6 +1,6 @@ { "dependencies": { "terser-webpack-plugin": "^3.0.1", - "react": "^16.13.1" + "react": "^16.13.0" } } From 2e1aaa76766b9ddcdf3dc52d2de9025a1c848723 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Fri, 22 May 2020 18:06:02 +0530 Subject: [PATCH 08/12] tests: add tests to ensure no additional properties in mode --- test/mode/mode-with-config/mode-with-config.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mode/mode-with-config/mode-with-config.test.js b/test/mode/mode-with-config/mode-with-config.test.js index c91872c3cd9..49ad85bdf81 100644 --- a/test/mode/mode-with-config/mode-with-config.test.js +++ b/test/mode/mode-with-config/mode-with-config.test.js @@ -4,7 +4,7 @@ const { resolve } = require('path'); // eslint-disable-next-line node/no-unpublished-require const { run } = require('../../utils/test-utils'); -describe.skip('mode flags with config', () => { +describe('mode flags with config', () => { it('should run in production mode when --mode=production is passed', (done) => { const { stderr, stdout } = run(__dirname, ['--mode', 'production', '--config', './webpack.config.js']); expect(stderr).toBeFalsy(); From edff561d2400c11dffa706a21b1030346876fa0c Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 23 May 2020 10:25:53 +0530 Subject: [PATCH 09/12] tests: add tests to ensure no additional properties in mode --- test/mode/mode-with-config/mode-with-config.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mode/mode-with-config/mode-with-config.test.js b/test/mode/mode-with-config/mode-with-config.test.js index 49ad85bdf81..c91872c3cd9 100644 --- a/test/mode/mode-with-config/mode-with-config.test.js +++ b/test/mode/mode-with-config/mode-with-config.test.js @@ -4,7 +4,7 @@ const { resolve } = require('path'); // eslint-disable-next-line node/no-unpublished-require const { run } = require('../../utils/test-utils'); -describe('mode flags with config', () => { +describe.skip('mode flags with config', () => { it('should run in production mode when --mode=production is passed', (done) => { const { stderr, stdout } = run(__dirname, ['--mode', 'production', '--config', './webpack.config.js']); expect(stderr).toBeFalsy(); From 290ad25a9dd3fd74c19a7bd933ea68694f7b221d Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 23 May 2020 10:37:32 +0530 Subject: [PATCH 10/12] tests: update prepSuite script --- scripts/prepareSuite.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/scripts/prepareSuite.js b/scripts/prepareSuite.js index 31552e2dcbe..f25a5065ea8 100644 --- a/scripts/prepareSuite.js +++ b/scripts/prepareSuite.js @@ -33,21 +33,19 @@ function extractFolder(folderToRead, folders = []) { return folders; } -{ - Promise.all( - collectTestingFoldersWithPackage().map(async (folder) => { - return execa('yarn', { +(async () => { + try { + const folders = collectTestingFoldersWithPackage(); + for (const folder of folders) { + await execa('yarn', { cwd: folder, stdio: 'inherit', }); - }), - ) - .then(() => { - console.log(chalk.inverse.green(' Successfully prepared the test suite ')); - }) - .catch((e) => { - console.error(chalk.inverse.red(' Unable to prepare the test suite ')); - console.error(e.stack); - process.exitCode = 1; - }); -} + } + console.log(chalk.inverse.green(' Successfully prepared the test suite ')); + } catch (e) { + console.error(chalk.inverse.red(' Unable to prepare the test suite ')); + console.error(e.stack); + process.exitCode = 1; + } +})(); From 7dce34b2775654eff619de7bf7aefe461c409902 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 23 May 2020 10:48:06 +0530 Subject: [PATCH 11/12] tests: add tests to ensure no additional properties in mode --- test/mode/mode-with-config/mode-with-config.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mode/mode-with-config/mode-with-config.test.js b/test/mode/mode-with-config/mode-with-config.test.js index c91872c3cd9..49ad85bdf81 100644 --- a/test/mode/mode-with-config/mode-with-config.test.js +++ b/test/mode/mode-with-config/mode-with-config.test.js @@ -4,7 +4,7 @@ const { resolve } = require('path'); // eslint-disable-next-line node/no-unpublished-require const { run } = require('../../utils/test-utils'); -describe.skip('mode flags with config', () => { +describe('mode flags with config', () => { it('should run in production mode when --mode=production is passed', (done) => { const { stderr, stdout } = run(__dirname, ['--mode', 'production', '--config', './webpack.config.js']); expect(stderr).toBeFalsy(); From 93201352f76dee1cc4da7304c20b1b7837dd8a29 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 23 May 2020 10:52:56 +0530 Subject: [PATCH 12/12] tests: add tests to ensure no additional properties in mode --- test/mode/mode-with-config/mode-with-config.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/mode/mode-with-config/mode-with-config.test.js b/test/mode/mode-with-config/mode-with-config.test.js index 49ad85bdf81..5a20f6f2b91 100644 --- a/test/mode/mode-with-config/mode-with-config.test.js +++ b/test/mode/mode-with-config/mode-with-config.test.js @@ -58,7 +58,6 @@ describe('mode flags with config', () => { // Correct mode should be propagated to the compiler readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => { expect(err).toBe(null); - expect(data).toContain('The "eval" devtool has been used'); expect(data).toContain('development mode'); done(); });