-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
150 lines (129 loc) · 4 KB
/
main.js
File metadata and controls
150 lines (129 loc) · 4 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const exec = require('child_process').execSync;
const path = require('path');
const sources = require('webpack-sources');
function getFiles (
entrypoints,
chunks
) {
const ret = {};
entrypoints.forEach((entry) => {
if (chunks && !chunks.includes(entry.name)) {
return;
}
entry.getFiles().forEach((file) => {
const extension = path.extname(file).replace(/\./, '');
if (!ret[extension]) {
ret[extension] = [];
}
ret[extension].push(file);
});
});
return ret;
}
class VersionPlugin {
constructor(options = {}) {
this.options = options
}
apply (compiler) {
compiler.hooks.emit.tap('Version Plugin',
compilation => {
const currentmode = compilation.options.mode
const {
name = "VERSION_INFO",
mode = "development",
chunks,
dataOption = {},
} = this.options;
if (typeof mode === 'string') {
if (mode !== 'all' && currentmode !== mode) {
return
}
} else if (mode instanceof Array) {
if (mode.indexOf(currentmode) < 0) {
return
}
} else {
throw new Error('[VersionPlugin] mode invalid')
}
const files = getFiles(compilation.entrypoints, chunks);
const defaultDataOption = {
git_branch: {
default: true,
func () {
return exec('git rev-parse --abbrev-ref HEAD').toString().trim();
}
},
git_commit_hash: {
default: true,
func () {
return exec('git show -s --format=%h').toString().trim();
}
},
git_commit_fullhash: {
default: false,
func () {
return exec('git show -s --format=%H').toString().trim();
}
},
git_commit_time: {
default: false,
func () {
return exec('git show -s --format=%cI').toString().trim();
}
},
git_commit_author: {
default: false,
func () {
return exec('git show -s --format=%an').toString().trim();
}
},
git_commit_commiter: {
default: false,
func () {
return exec('git show -s --format=%cn').toString().trim();
}
},
git_commit_message: {
default: false,
func () {
return exec('git show -s --format=%b').toString().trim();
}
},
package_version: {
default: false,
func () {
return process.env.npm_package_version;
}
},
build_time: {
default: false,
func () {
return new Date().toISOString();
}
},
}
const VERSION_INFO = {}
const dataNameList = Object.keys(defaultDataOption).concat(Object.keys(dataOption))
for (let dataName of dataNameList) {
if (typeof dataOption[dataName] === 'undefined') {
if (defaultDataOption[dataName] && defaultDataOption[dataName].default) {
VERSION_INFO[dataName] = defaultDataOption[dataName].func()
}
} else if (typeof dataOption[dataName] === 'boolean') {
if (dataOption[dataName] && defaultDataOption[dataName]) {
VERSION_INFO[dataName] = defaultDataOption[dataName].func()
}
} else if (typeof dataOption[dataName] === 'function') {
VERSION_INFO[dataName] = dataOption[dataName]()
} else {
VERSION_INFO[dataName] = dataOption[dataName]
}
}
const vsrsionString = `window.${name} = ${JSON.stringify(VERSION_INFO)};\n`
files.js.map(jsFilename => {
compilation.assets[jsFilename] = new sources.ConcatSource(vsrsionString, compilation.assets[jsFilename],);
})
})
}
}
module.exports = VersionPlugin;