想利用webpack-version-plugin 生成版本号 但是不成功

发布于 2022-09-13 01:13:12 字数 1574 浏览 12 评论 0

网上找了个插件,想用来生成版本号https://www.npmjs.com/package...
项目是使用creat-react-app生成的,没有效果,也没有报错,不知道哪里写错了。yarn build 后console.log(hashMap)也没执行。

const WebpackVersionPlugin = require('webpack-version-plugin')
const versionConfig = require(path.join(__dirname, '../version.json'));
    plugins: [
      new WebpackVersionPlugin({
        // You must set the cb option
        cb: function (hashMap) {
          console.log(hashMap);
          versionConfig.appVersion = hashMap.app.chunkHash;
          fs.writeFileSync(path.join(__dirname, '../version.json'), JSON.stringify(versionConfig, null, 2));
        }
      })
      ,
      // new HelloWorldPlugin({ options: true }),

      // Generates an `index.html` file with the <script> injected.
      new HtmlWebpackPlugin(
        Object.assign(
          {},
          {
            inject: true,
            template: paths.appHtml,
          },
          isEnvProduction
            ? {
              minify: {
                removeComments: true,
                collapseWhitespace: true,
                removeRedundantAttributes: true,
                useShortDoctype: true,
                removeEmptyAttributes: true,
                removeStyleLinkTypeAttributes: true,
                keepClosingSlash: true,
                minifyJS: true,
                minifyCSS: true,
                minifyURLs: true,
              },
            }
            : undefined
        )
      ),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南街女流氓 2022-09-20 01:13:12

问题中webpack使用方法没有问题,问题出现在webpack的启动项上。
因为引入了antd-mobile。根据文档将启动项改成了 react-app-rewired
只要将packge.json改回去即可。

/* package.json */
"scripts": {
-   "start": "react-scripts start",
+   "start": "react-app-rewired start",
-   "build": "react-scripts build",
+   "build": "react-app-rewired build",
-   "test": "react-scripts test --env=jsdom",
+   "test": "react-app-rewired test --env=jsdom",
}

但是这会导致一个问题, antd-mobile样式没有被引入。
可以这样做yarn add babel-plugin-import
在packge.json下的babel属性上增加一个plugins节点,代码如下

  "babel": {
    "presets": [
      "react-app"
    ],
    "plugins": [
      [
        "import",
        {
          "libraryName": "antd-mobile",
          "style": "css"
        }
      ]
    ]
  },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文