-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
41 lines (39 loc) · 1.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const Webpack = require('webpack');
const { gitDescribeSync } = require('git-describe');
/**
* Git describe plugin
*
* @param {Object} api - Vue CLI PluginAPI instance
* @param {Object} options - An object containing project local options specified in vue.config.js, or in the "vue" field in package.json.
* @param {Object} options.pluginOptions - Options for plugins. See: https://cli.vuejs.org/config/#pluginoptions
* @param {Object} options.pluginOptions.gitDescribe - gitDescribe plugin options. This object will be provided to the git describe module.
* @param {String} [options.pluginOptions.gitDescribe.variableName='GIT_DESCRIBE'] - The compile-time global constant name
* @return {}
*/
module.exports = (api, options) => {
// The options passed to this plugin using the Short-circuit evaluation technique
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Short-circuit_evaluation
const gitDescribeOps = (((options || {}).pluginOptions || {}).gitDescribe || {});
const variableName = gitDescribeOps.variableName || 'GIT_DESCRIBE';
api.chainWebpack(webpackConfig => {
/**
* Get the git info from the git describe module
* @return {Object} Returns the git info object from git describe
*/
const gitInfo = () => {
try {
return gitDescribeSync.apply(this, gitDescribeOps);
} catch (err) {
console.error('Failed to retrieve git info:', err);
}
return null;
};
// Webpack's DefinePlugin is used to inject the 'global variable' on compile time
// The variable used can be set via the options
const definePluginOps = {};
definePluginOps[variableName] = JSON.stringify(gitInfo());
webpackConfig
.plugin('gitDescribe')
.use(Webpack.DefinePlugin, [definePluginOps]);
});
};