我如何将 package.json 版本写入我的 WebPack 包? [英] How would I write the package.json version to my WebPack bundle?

查看:22
本文介绍了我如何将 package.json 版本写入我的 WebPack 包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 package.json 文件包含我的模块的一个版本,它最终被编译成一个 app.bundle.js 文件,我包含在我的 web 项目中.我真的希望将 package.json 文件中的版本号写入 app.bundle.js 文件作为文件开头的注释.

My package.json file includes a version for my module, which ultimately get's compiled into an app.bundle.js file that I include in my web project. I would REALLY like to have the version number from the package.json file written to the app.bundle.js file as a comment right at the beginning of the file.

是否有 WebPack 插件来执行此操作或 WebPack 本身的设置?

Is there a WebPack plugin to do this or a setting with WebPack itself?

推荐答案

Webpack 带有一个 BannerPlugin 用于在每个生成的块的顶部添加一个横幅.

Webpack comes with a BannerPlugin that adds a banner to the top of each generated chunk.

您可以要求您的 package.json 并将其用作任何常规 JavaScript 对象来获取 version 字段.

You can require your package.json and use it as any regular JavaScript object to get the version field.

var PACKAGE = require('./package.json');
var version = PACKAGE.version;

然后使用它生成将在 BannerPlugin 中使用的所需横幅字符串.

Then use it to generate the desired banner string that will be used in the BannerPlugin.

var PACKAGE = require('./package.json');
var banner = PACKAGE.name + ' - ' + PACKAGE.version;

module.exports = {
  // Other stuff
  plugins: [
    new webpack.BannerPlugin(banner)
  ]
};

我使用它将 package.json 文件中的版本和其他信息添加到我自己的库的顶部.检查 webpack.config.js 这个项目的一个工作示例.

I have used it to add the version from the package.json file and other info to the top of a library of my own. Check the webpack.config.js of this project for a working example.

这篇关于我如何将 package.json 版本写入我的 WebPack 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆