From e13082221c2da01d8b8215ebc936474bf3ca1582 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Wed, 24 Mar 2021 10:40:25 +0530 Subject: [PATCH] feat: add env var to skip local import (#2546) * feat: add env var to skip local import * feat: tests --- packages/webpack-cli/bin/cli.js | 8 +++++--- test/build/import-local/import-local.test.js | 21 ++++++++++++++++++++ test/build/import-local/src/index.js | 0 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 test/build/import-local/import-local.test.js create mode 100644 test/build/import-local/src/index.js diff --git a/packages/webpack-cli/bin/cli.js b/packages/webpack-cli/bin/cli.js index db74156aa8b..9a9f5f02a59 100755 --- a/packages/webpack-cli/bin/cli.js +++ b/packages/webpack-cli/bin/cli.js @@ -12,9 +12,11 @@ const importLocal = require('import-local'); const runCLI = require('../lib/bootstrap'); const utils = require('../lib/utils'); -// Prefer the local installation of `webpack-cli` -if (importLocal(__filename)) { - return; +if (!process.env.WEBPACK_CLI_SKIP_IMPORT_LOCAL) { + // Prefer the local installation of `webpack-cli` + if (importLocal(__filename)) { + return; + } } process.title = 'webpack'; diff --git a/test/build/import-local/import-local.test.js b/test/build/import-local/import-local.test.js new file mode 100644 index 00000000000..afbb3d90a9e --- /dev/null +++ b/test/build/import-local/import-local.test.js @@ -0,0 +1,21 @@ +'use strict'; + +const { run } = require('../../utils/test-utils'); + +const importLocalMock = jest.fn(); +jest.setMock('import-local', importLocalMock); + +describe('import local', () => { + beforeEach(() => { + importLocalMock.mockClear(); + }); + it('should skip import local when supplied', () => { + const { exitCode, stderr, stdout } = run(__dirname, [], { + env: { WEBPACK_CLI_SKIP_IMPORT_LOCAL: true }, + }); + expect(importLocalMock).toHaveBeenCalledTimes(0); + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + }); +}); diff --git a/test/build/import-local/src/index.js b/test/build/import-local/src/index.js new file mode 100644 index 00000000000..e69de29bb2d