-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(cli): use webpack-cli@4 #2845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aab3ed0
feat: add setupExitSignals option
ylemkimon 6005177
feat: find free port if port is not specified
ylemkimon d87a369
fix(cli): use webpack-cli@4
ylemkimon 1ab4177
feat(cli): add stdin option
ylemkimon 3db0cea
chore: remove webpack-cli peer dependency
ylemkimon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,158 @@ | ||
#!/usr/bin/env node | ||
/* Based on webpack/bin/webpack.js */ | ||
/* eslint-disable no-console */ | ||
|
||
'use strict'; | ||
|
||
/* eslint-disable no-shadow, no-console */ | ||
|
||
const debug = require('debug')('webpack-dev-server'); | ||
const importLocal = require('import-local'); | ||
const yargs = require('yargs'); | ||
const webpack = require('webpack'); | ||
const Server = require('../lib/Server'); | ||
const setupExitSignals = require('../lib/utils/setupExitSignals'); | ||
const colors = require('../lib/utils/colors'); | ||
const processOptions = require('../lib/utils/processOptions'); | ||
const getVersions = require('../lib/utils/getVersions'); | ||
const getColorsOption = require('../lib/utils/getColorsOption'); | ||
const options = require('./options'); | ||
|
||
let server; | ||
const serverData = { | ||
server: null, | ||
/** | ||
* @param {string} command process to run | ||
* @param {string[]} args command line arguments | ||
* @returns {Promise<void>} promise | ||
*/ | ||
const runCommand = (command, args) => { | ||
const cp = require('child_process'); | ||
return new Promise((resolve, reject) => { | ||
const executedCommand = cp.spawn(command, args, { | ||
stdio: 'inherit', | ||
shell: true, | ||
}); | ||
|
||
executedCommand.on('error', (error) => { | ||
reject(error); | ||
}); | ||
|
||
executedCommand.on('exit', (code) => { | ||
if (code === 0) { | ||
resolve(); | ||
} else { | ||
reject(); | ||
} | ||
}); | ||
}); | ||
}; | ||
// we must pass an object that contains the server object as a property so that | ||
// we can update this server property later, and setupExitSignals will be able to | ||
// recognize that the server has been instantiated, because we will set | ||
// serverData.server to the new server object. | ||
setupExitSignals(serverData); | ||
|
||
// Prefer the local installation of webpack-dev-server | ||
if (importLocal(__filename)) { | ||
debug('Using local install of webpack-dev-server'); | ||
/** | ||
* @param {string} packageName name of the package | ||
* @returns {boolean} is the package installed? | ||
*/ | ||
const isInstalled = (packageName) => { | ||
try { | ||
require.resolve(packageName); | ||
|
||
return; | ||
} | ||
return true; | ||
} catch (err) { | ||
return false; | ||
} | ||
}; | ||
|
||
try { | ||
require.resolve('webpack-cli'); | ||
} catch (err) { | ||
console.error('The CLI moved into a separate package: webpack-cli'); | ||
console.error( | ||
"Please install 'webpack-cli' in addition to webpack itself to use the CLI" | ||
); | ||
console.error('-> When using npm: npm i -D webpack-cli'); | ||
console.error('-> When using yarn: yarn add -D webpack-cli'); | ||
/** | ||
* @param {CliOption} cli options | ||
* @returns {void} | ||
*/ | ||
const runCli = (cli) => { | ||
if (cli.preprocess) { | ||
cli.preprocess(); | ||
} | ||
const path = require('path'); | ||
const pkgPath = require.resolve(`${cli.package}/package.json`); | ||
// eslint-disable-next-line import/no-dynamic-require | ||
const pkg = require(pkgPath); | ||
// eslint-disable-next-line import/no-dynamic-require | ||
require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName])); | ||
}; | ||
|
||
process.exitCode = 1; | ||
} | ||
/** | ||
* @typedef {Object} CliOption | ||
* @property {string} name display name | ||
* @property {string} package npm package name | ||
* @property {string} binName name of the executable file | ||
* @property {boolean} installed currently installed? | ||
* @property {string} url homepage | ||
* @property {function} preprocess preprocessor | ||
*/ | ||
|
||
/** @type {CliOption} */ | ||
const cli = { | ||
name: 'webpack-cli', | ||
package: 'webpack-cli', | ||
binName: 'webpack-cli', | ||
installed: isInstalled('webpack-cli'), | ||
url: 'https://github.com/webpack/webpack-cli', | ||
preprocess() { | ||
process.argv.splice(2, 0, 'serve'); | ||
}, | ||
Comment on lines
+81
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is copied from webpack/bin/webpack.js and these lines were added. |
||
}; | ||
|
||
yargs.usage( | ||
`${getVersions()}\nUsage: https://webpack.js.org/configuration/dev-server/` | ||
); | ||
|
||
// [email protected] path : 'webpack-cli/bin/config/config-yargs' | ||
let configYargsPath; | ||
try { | ||
require.resolve('webpack-cli/bin/config/config-yargs'); | ||
configYargsPath = 'webpack-cli/bin/config/config-yargs'; | ||
} catch (e) { | ||
configYargsPath = 'webpack-cli/bin/config-yargs'; | ||
} | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
// eslint-disable-next-line import/no-dynamic-require | ||
require(configYargsPath)(yargs); | ||
|
||
// It is important that this is done after the webpack yargs config, | ||
// so it overrides webpack's version info. | ||
yargs.version(getVersions()); | ||
yargs.options(options); | ||
|
||
const argv = yargs.argv; | ||
|
||
// [email protected] path : 'webpack-cli/bin/utils/convert-argv' | ||
let convertArgvPath; | ||
try { | ||
require.resolve('webpack-cli/bin/utils/convert-argv'); | ||
convertArgvPath = 'webpack-cli/bin/utils/convert-argv'; | ||
} catch (e) { | ||
convertArgvPath = 'webpack-cli/bin/convert-argv'; | ||
} | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
// eslint-disable-next-line import/no-dynamic-require | ||
const config = require(convertArgvPath)(yargs, argv, { | ||
outputFilename: '/bundle.js', | ||
}); | ||
if (!cli.installed) { | ||
const path = require('path'); | ||
const fs = require('graceful-fs'); | ||
const readLine = require('readline'); | ||
|
||
function startDevServer(config, options) { | ||
let compiler; | ||
const notify = `CLI for webpack must be installed.\n ${cli.name} (${cli.url})\n`; | ||
|
||
const configArr = config instanceof Array ? config : [config]; | ||
const statsColors = getColorsOption(configArr); | ||
console.error(notify); | ||
|
||
try { | ||
compiler = webpack(config); | ||
} catch (err) { | ||
if (err instanceof webpack.WebpackOptionsValidationError) { | ||
console.error(colors.error(statsColors, err.message)); | ||
// eslint-disable-next-line no-process-exit | ||
process.exit(1); | ||
} | ||
let packageManager; | ||
|
||
throw err; | ||
if (fs.existsSync(path.resolve(process.cwd(), 'yarn.lock'))) { | ||
packageManager = 'yarn'; | ||
} else if (fs.existsSync(path.resolve(process.cwd(), 'pnpm-lock.yaml'))) { | ||
packageManager = 'pnpm'; | ||
} else { | ||
packageManager = 'npm'; | ||
} | ||
|
||
try { | ||
server = new Server(compiler, options); | ||
serverData.server = server; | ||
} catch (err) { | ||
if (err.name === 'ValidationError') { | ||
console.error(colors.error(statsColors, err.message)); | ||
// eslint-disable-next-line no-process-exit | ||
process.exit(1); | ||
} | ||
const installOptions = [packageManager === 'yarn' ? 'add' : 'install', '-D']; | ||
|
||
throw err; | ||
} | ||
console.error( | ||
`We will use "${packageManager}" to install the CLI via "${packageManager} ${installOptions.join( | ||
' ' | ||
)}".` | ||
); | ||
|
||
server.listen(options.port, options.host, (err) => { | ||
if (err) { | ||
throw err; | ||
const question = `Do you want to install 'webpack-cli' (yes/no): `; | ||
|
||
const questionInterface = readLine.createInterface({ | ||
input: process.stdin, | ||
output: process.stderr, | ||
}); | ||
|
||
// In certain scenarios (e.g. when STDIN is not in terminal mode), the callback function will not be | ||
// executed. Setting the exit code here to ensure the script exits correctly in those cases. The callback | ||
// function is responsible for clearing the exit code if the user wishes to install webpack-cli. | ||
process.exitCode = 1; | ||
questionInterface.question(question, (answer) => { | ||
questionInterface.close(); | ||
|
||
const normalizedAnswer = answer.toLowerCase().startsWith('y'); | ||
|
||
if (!normalizedAnswer) { | ||
console.error( | ||
"You need to install 'webpack-cli' to use webpack via CLI.\n" + | ||
'You can also install the CLI manually.' | ||
); | ||
|
||
return; | ||
} | ||
process.exitCode = 0; | ||
|
||
console.log( | ||
`Installing '${ | ||
cli.package | ||
}' (running '${packageManager} ${installOptions.join(' ')} ${ | ||
cli.package | ||
}')...` | ||
); | ||
|
||
runCommand(packageManager, installOptions.concat(cli.package)) | ||
.then(() => { | ||
runCli(cli); | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
process.exitCode = 1; | ||
}); | ||
}); | ||
} else { | ||
runCli(cli); | ||
} | ||
|
||
processOptions(config, argv, (config, options) => { | ||
startDevServer(config, options); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# CLI: Stdin Option | ||
|
||
Specifying this option instructs the server to close when `stdin` ends. | ||
|
||
```console | ||
npm run webpack-dev-server -- --stdin | ||
``` | ||
|
||
## What Should Happen | ||
|
||
1. The server should begin running. | ||
2. Press `CTL+D` on your keyboard. | ||
3. The server should close. | ||
|
||
_Note: the keyboard shortcut for terminating `stdin` can vary depending on the | ||
operating systems._ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
const target = document.querySelector('#target'); | ||
|
||
target.innerHTML = | ||
'Press <code>CTL+D</code> on your keyboard to close the server.'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
context: __dirname, | ||
entry: './app.js', | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.