Node/WebpackDevServer/react-scripts,在开始时删除 "To create a production build, use npm run build" 消息
Node/WebpackDevServer/react-scripts, remove "To create a production build, use npm run build" message on start
当我在使用 webpack 和 WebpackDevServer 的 javascript/react 应用程序上 运行 yarn start
时遇到问题。成功编译后会显示这条熟悉的消息;
Compiled successfully!
You can now view {project name} in the browser.
http://127.0.0.1:8000/
Note that the development build is not optimized.
To create a production build, use npm run build.
问题是 npm run build
不是必须用于生成生产版本的命令,所以这实际上给出了错误的指令,有什么方法可以删除或自定义此消息吗?
这是建议,不是生产构建的要求。
看来你编译成功了。
这是打印出来的,因为我的启动脚本在 react-dev-utils/WebpackDevServerUtils.js 中使用了 createCompiler
,在这里 printInstructions
函数在成功编译时打印了它,请参阅 here。唯一可能的配置是将 useYarn
设置为 true,这只是用 yarn build
替换命令。所以我可以使用不同的包或分叉它,两者都不理想。
这个问题的解决方法是覆盖 console.log 以捕获此消息并将其替换为所需的消息,如下所示。
const originalConsoleLog = console.log
console.log = (args) => {
if (typeof args === 'string' && args.indexOf("To create a production build, use ") !== -1) {
let stringToReplace = `use ${chalk.cyan(`${useYarn ? 'yarn' : 'npm run'} build`)}`
args = args.replace(stringToReplace, {your instructions string here})
console.log = originalConsoleLog
}
args ? originalConsoleLog(args) : originalConsoleLog()
}
其中 useYarn
与传递给 react-dev-utils/WebpackDevServerUtils
的 createCompiler
相同的 useYarn
。最后,console.log
再次被原来的替换。
当我在使用 webpack 和 WebpackDevServer 的 javascript/react 应用程序上 运行 yarn start
时遇到问题。成功编译后会显示这条熟悉的消息;
Compiled successfully!
You can now view {project name} in the browser.
http://127.0.0.1:8000/
Note that the development build is not optimized.
To create a production build, use npm run build.
问题是 npm run build
不是必须用于生成生产版本的命令,所以这实际上给出了错误的指令,有什么方法可以删除或自定义此消息吗?
这是建议,不是生产构建的要求。
看来你编译成功了。
这是打印出来的,因为我的启动脚本在 react-dev-utils/WebpackDevServerUtils.js 中使用了 createCompiler
,在这里 printInstructions
函数在成功编译时打印了它,请参阅 here。唯一可能的配置是将 useYarn
设置为 true,这只是用 yarn build
替换命令。所以我可以使用不同的包或分叉它,两者都不理想。
这个问题的解决方法是覆盖 console.log 以捕获此消息并将其替换为所需的消息,如下所示。
const originalConsoleLog = console.log
console.log = (args) => {
if (typeof args === 'string' && args.indexOf("To create a production build, use ") !== -1) {
let stringToReplace = `use ${chalk.cyan(`${useYarn ? 'yarn' : 'npm run'} build`)}`
args = args.replace(stringToReplace, {your instructions string here})
console.log = originalConsoleLog
}
args ? originalConsoleLog(args) : originalConsoleLog()
}
其中 useYarn
与传递给 react-dev-utils/WebpackDevServerUtils
的 createCompiler
相同的 useYarn
。最后,console.log
再次被原来的替换。