惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
D
Docker
GbyAI
GbyAI
宝玉的分享
宝玉的分享
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Vercel News
Vercel News
博客园_首页
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
V
V2EX
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 我爱我家喵喵

LookImage2:用 Rust 打造的轻量级专业图像查看器 bat以隐藏模式运行jar包 完美单机155端gs修改:允许10次转生 [Docker] 部署 Nexus Repository OSS 仓库 [docker] 部署 AnythingLLM [nginx] 使用nginx代理实现Ollama支持本地html访问 [docker] 部署 Seata 分布式事务 [docker] 部署 Nacos 服务 CEF 谷歌内核下载地址 [Delphi] 自带皮肤动态切换 【算法】python版A-Star(A星)寻路 【算法】决策树算法:ID3 【算法】K-means 算法学习 【Unity】使用VSCode调试 [Python] 基于 flask 构建 Web API 实现参数注入和校验 [Redis] 解决多个 Redis 服务同步删除有关联的 key [C#] JavaScript 引擎 [MySql] 数据库死锁的排查和相关知识 [redis] 设置密码 [Java] 扩展 Jackson 的 TypeReference 支持泛型参数传递
[VUE] WebPack 打包后自动修改 dist 中 package.json 版本号
我爱我家喵喵 · 2023-12-22 · via 博客园 - 我爱我家喵喵

我们在开发 npm 包时,开发期的 package.json 通常并不一定是发布到 npm 仓库的 package.json。这种情况下每次改版本号需要改两个地方,比较麻烦。

我一般使用 webpack 进行打包,所以有了下面这个小插件。

插件源码

modify.version.plugin.js

/** 修改版本号 webpack 插件 */
function ModifyVersionPlugin(newVersion) {
  this.newVersion = newVersion;
}

ModifyVersionPlugin.prototype.apply = function(compiler) {
  compiler.hooks.emit.tap('ModifyVersionPlugin', function(compilation) {
    let assets = compilation.assets;
    let fileNames = Object.keys(assets);
    let version = this.newVersion;
    // 此项可以根据情况开启,用来更新源码中静态变量的值为当前版本号
    // 示例: const __VERSION__ = '__package_version__'
    // 打包后 __VERSION__  变量的值将变为当前版本号
    fileNames.forEach(function(fileName) {
      if (fileName === 'index.js') {
        let asset = assets[fileName];
        let assetSource = asset.source();
        // 将代码中的 __package_version__ 变量替换为版本号
        let updatedSource = assetSource.replace(/__package_version__/, version);
        asset.source = function() {
          return updatedSource;
        };
      }
    });

    // 修改 /dist/package.json 中的版本号
    try {
      const file = './dist/package.json';
      const fs = require('fs');
      let json = JSON.parse(fs.readFileSync(file, 'utf8'));
      console.log(json)
      json.version = version;
      fs.writeFileSync(file, JSON.stringify(json, null, 2));
    } catch (e) {
      console.error(e)
    }
  }.bind(this));
};

module.exports = ModifyVersionPlugin;

使用

  • 将上面的 modify.version.plugin.js 保存到项目 package.json 同级目录中。
  • 修改 webpack 配置,在 webpack.config.js 中添加如下代码:
const ModifyVersionPlugin = require('./modify.version.plugin.js');

...

const version = require('./package.json').version
console.log('Package version: ', version)

...

module.exports.plugins = (module.exports.plugins || []).concat([
  new ModifyVersionPlugin(version),
  ...
])
...