Getting git-revision hash with webpack

≯℡__Kan透↙ 提交于 2019-12-06 19:46:16

问题


I'm trying to create archive with webpack with suffix by git-revision. Could you tell me please what is good way to do it?


回答1:


You can get git revision in webpack in this way:

var childProcess = require('child_process'),
VERSION = childProcess.execSync('git rev-parse HEAD').toString();



回答2:


You can combine git-rev, arciverjs and on-build-webpack plugins for these purposes

https://www.npmjs.com/package/git-rev

http://archiverjs.com/docs/

https://www.npmjs.com/package/on-build-webpack

var childProcess = require('child_process'),
    VERSION = childProcess.execSync('git rev-parse HEAD').toString();

var WebpackOnBuildPlugin = require('on-build-webpack');

var plugins = [
  //...
  new WebpackOnBuildPlugin(function(stats) {
    var fs = require('fs');
    var archiver = require('archiver');

    var output = fs.createWriteStream(__dirname + '/' + VERSION + '-example.tar');
    var archive = archiver('tar');

    output.on('close', function() {
      console.log(archive.pointer() + ' total bytes');
      console.log('archiver has been finalized and the output file descriptor has closed.');
    });

    archive.on('error', function(err) {
      throw err;
    });

    archive.pipe(output);

    archive.bulk([
      { expand: true, cwd: 'source-dir/', src: ['*.*'] }
    ]);

    archive.finalize();
  })
];

Here is the code snippet from webpack config file which will create an archive with revision in name. For getting git revision you can use git-rev plugin or code snippet from answer of @bolelamx



来源:https://stackoverflow.com/questions/35150186/getting-git-revision-hash-with-webpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!