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', +};