【问题标题】:Node/WebpackDevServer/react-scripts, remove "To create a production build, use npm run build" message on startNode/WebpackDevServer/react-scripts,在启动时删除“To create a production build, use npm run build”消息
【发布时间】:2021-06-14 04:06:36
【问题描述】:

当我在使用 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 不是必须用于生成生产版本的命令,所以这实际上给出了错误的指令,有没有办法删除或自定义此消息?

【问题讨论】:

    标签: node.js reactjs webpack webpack-4


    【解决方案1】:

    这是打印出来的,因为我的启动脚本在 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/WebpackDevServerUtilscreateCompileruseYarn 相同。在捕获中,console.log 再次被替换为原来的。

    【讨论】:

      【解决方案2】:

      这是一个建议,不是生产构建的要求。

      看起来你已经成功编译了。

      【讨论】:

      • 这不是建议,它明确告诉客户使用 npm run build 进行生产构建,如果他们这样做会失败。这也没有回答我的问题
      • 我知道它没有回答这个问题。我以为我在帖子中提到了这一点。对不起。
      猜你喜欢
      • 2020-09-20
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 2019-03-19
      • 2017-10-05
      • 1970-01-01
      相关资源
      最近更新 更多