From 3d1e4855c55a6601d8a89dcb50d9d842009e3cda Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Fri, 26 Mar 2021 17:23:44 +0530 Subject: [PATCH] feat: add `WEBPACK_PACKAGE` env var to use custom `webpack` package (#2556) * feat: add `WEBPACK_PACKAGE` env variable * fix: typo --- packages/webpack-cli/lib/webpack-cli.js | 2 +- test/build/custom-webpack/custom-webpack.js | 1 + .../custom-webpack/custom-webpack.test.js | 26 +++++++++++++++++++ test/build/custom-webpack/src/index.js | 1 + test/build/custom-webpack/webpack.config.js | 3 +++ 5 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/build/custom-webpack/custom-webpack.js create mode 100644 test/build/custom-webpack/custom-webpack.test.js create mode 100644 test/build/custom-webpack/src/index.js create mode 100644 test/build/custom-webpack/webpack.config.js diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index dcf8e8cff22..14839a2eb67 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -9,7 +9,7 @@ const utils = require('./utils'); class WebpackCLI { constructor() { // Global - this.webpack = require('webpack'); + this.webpack = require(process.env.WEBPACK_PACKAGE || 'webpack'); this.logger = utils.logger; this.utils = utils; diff --git a/test/build/custom-webpack/custom-webpack.js b/test/build/custom-webpack/custom-webpack.js new file mode 100644 index 00000000000..dfe19cee008 --- /dev/null +++ b/test/build/custom-webpack/custom-webpack.js @@ -0,0 +1 @@ +module.exports = require('webpack'); diff --git a/test/build/custom-webpack/custom-webpack.test.js b/test/build/custom-webpack/custom-webpack.test.js new file mode 100644 index 00000000000..d0df8919ceb --- /dev/null +++ b/test/build/custom-webpack/custom-webpack.test.js @@ -0,0 +1,26 @@ +'use strict'; + +const { resolve } = require('path'); +const { run } = require('../../utils/test-utils'); + +describe('custom-webpack', () => { + it('should use custom-webpack.js', () => { + const { exitCode, stderr, stdout } = run(__dirname, [], { + env: { WEBPACK_PACKAGE: resolve(__dirname, './custom-webpack.js') }, + }); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toContain('main.js'); + }); + + it('should throw an error for invalid-webpack.js', () => { + const { exitCode, stderr, stdout } = run(__dirname, [], { + env: { WEBPACK_PACKAGE: resolve(__dirname, './invalid-webpack.js') }, + }); + + expect(exitCode).toBe(2); + expect(stderr).toContain(`Error: Cannot find module`); + expect(stdout).toBeFalsy(); + }); +}); diff --git a/test/build/custom-webpack/src/index.js b/test/build/custom-webpack/src/index.js new file mode 100644 index 00000000000..ec10f3e9de1 --- /dev/null +++ b/test/build/custom-webpack/src/index.js @@ -0,0 +1 @@ +console.log('hi'); diff --git a/test/build/custom-webpack/webpack.config.js b/test/build/custom-webpack/webpack.config.js new file mode 100644 index 00000000000..5b69c702150 --- /dev/null +++ b/test/build/custom-webpack/webpack.config.js @@ -0,0 +1,3 @@ +module.exports = { + mode: 'production', +};