当前位置: 首页 > 知识库问答 >
问题:

react.js - React create-react-app 构建问题?

万俟靖
2023-05-04

问题如下: 想要在React 项目执行yarn build 时, 给index.html 增加后缀
没有使用react-eject, 使用的是crao 替代, 但是craco.config.js 配置 HtmlWebpackPlugin不生效。
craco.config.js 配置如下:

const CracoLessPlugin = require('craco-less')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  plugins: [
    {
      plugin: CracoLessPlugin,
    },
  ],
  webpack: {
    plugins: {
      add: [
        [
          new HtmlWebpackPlugin({
            filename: 'index.[contenthash].html',
          }),
          'prepend',
        ],
      ],
    },
  },
}

package.json 部分组件版本如下:

 "dependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "typescript": "^4.7.2",
    "web-vitals": "^2.1.4"
  }, 
"devDependencies": {
    "@craco/craco": "^7.1.0",
    "craco-less": "^2.0.0",
    "html-webpack-plugin": "^5.5.1",
  }
 "scripts": {
    "dev": "set PORT=4488 && craco start",
    "build": "craco build",
  },

执行yarn build 打包后,配置无效, 请教各位大佬们了

共有1个答案

艾骏喆
2023-05-04

你要直接修改 webpack 配置:

const CracoLessPlugin = require('craco-less');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  plugins: [
    {
      plugin: CracoLessPlugin,
    },
  ],
  webpack: {
    configure: (webpackConfig, { env, paths }) => {
    
      const htmlWebpackPlugin = webpackConfig.plugins.find(
        (plugin) => plugin.constructor.name === 'HtmlWebpackPlugin'
      );

      if (htmlWebpackPlugin) {
        // 修改 HtmlWebpackPlugin 配置
        htmlWebpackPlugin.options.filename = 'index.[contenthash].html';
      }

      return webpackConfig;
    },
  },
};
 类似资料:
  • 我使用create react应用程序已经有一段时间了。“npm start”或“yarn start”自动重装本身工作正常,但现在我有了另一个问题。目前,我在express server上通过build文件夹运行应用程序,我使用“npm run build ”,因为express正在提供构建文件。有许多api调用要求应用程序以这种方式运行。现在,每次手动执行“npm运行构建”变得很乏味。有没有一

  • react 在 A 函数设置了state 然后调用 B函数,B函数中使用了state,但直接获取state不是最新值。 使用Effect和通过函数参数传递来解决,感觉都不优雅,大佬们有没有比较好的经验?

  • 我正在使用一个快速后端创建反应应用程序。我在端口3001上运行后端,在开发模式下,前端通过端口3000运行。我的package.json中有< code > " proxy ":" http://localhost:3001 " ,api工作得很好。 然而,当我使用yarn build,然后运行< code>serve -s build时,根本就没有进行api调用。我不知道为什么它在生产中不工作,

  • Tabris.js利用Apache Cordova构建和打包app。可以使用tabrisjs.com上的免费在线构建服务构建app,不需要任何本地配置。要在本地机器上构建app,你需要安装Xcode,Visual Studio或Android SDK等开发工具。 两种不同构建方式支持以下功能。   构建服务 本地构建 构建iOS App ✓ ✓ 构建Android App ✓ ✓ 构建Window

  • 创建响应应用时,图像中出现错误。它一直在正常工作,我所做的就是mkdir和cd到那个dir。 这就是错误的样子... NPX:在36.634s中安装了99必须使用导入来加载ES模块: C:\用户\Neeraj\AppData\漫游\npm-cache_npx\5172\node_modules\create-react-app\node_modules\is-promise\index.js不支持

  • React中的useCallback 下面的代码中Com是父组件,Button是子组件,子组件接收父组件的count2和setCount2,子组件中使用了memo(Button)导出 子组件Button 在上面的代码中,如果点击count1++的按钮(不使用useCallback),父组件会重新渲染,但是子组件也会刷新,然而子组件中的count2依赖没有变化,所以只能是handleClick2函数