-
Notifications
You must be signed in to change notification settings - Fork 314
/
webpack.plugins.js
147 lines (126 loc) · 3.76 KB
/
webpack.plugins.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
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
import fs from "fs"
import crypto from "crypto"
import path from "path"
import { validate } from "schema-utils"
import { access as accessCps } from "fs"
import { execFile as execFileCps } from "child_process"
import { promisify } from "util"
class SRIPlugin {
static defaultOptions = {
algorithm: "sha512",
sourceFile: "assets.json"
}
constructor(options = {}) {
this.options = { ...SRIPlugin.defaultOptions, ...options }
validate(
{
type: "object",
properties: {
sourceFile: { type: "string" },
outputFile: { type: "string" },
algorithm: { type: "string" }
}
},
options,
{
name: "SRI Plugin",
baseDataPath: "options"
}
)
}
apply(compiler) {
compiler.hooks.done.tap("SRIPlugin", () => {
const data = JSON.parse(fs.readFileSync(this.options.sourceFile, "utf8"))
const outputFile = this.options.outputFile || this.options.sourceFile
const { algorithm } = this.options
const calculateSRI = (file) => {
const fileContent = fs.readFileSync(path.join(".", "static", file))
const hash = crypto.createHash(algorithm).update(fileContent).digest("base64")
return `${algorithm}-${hash}`
}
Object.keys(data).forEach((key) => {
data[key].integrity = calculateSRI(data[key].src)
})
fs.writeFileSync(outputFile, JSON.stringify(data, null, 2), { encoding: "utf8", flag: "w" })
})
}
}
class GitVersionPlugin {
static defaultOptions = {
outputFile: "VERSION"
}
constructor(options = {}) {
this.options = { ...GitVersionPlugin.defaultOptions, ...options }
validate(
{
type: "object",
properties: {
outputFile: { type: "string" }
}
},
options,
{
baseDataPath: "options",
name: "GitVersion Plugin"
}
)
}
apply(compiler) {
const { webpack, hooks, context } = compiler
const { Compilation } = webpack
hooks.beforeCompile.tapPromise("GitVersionPlugin", async () => {
const access = promisify(accessCps)
try {
await access(".git")
this.dependsOnGit = true
} catch {
this.dependsOnGit = false
}
})
hooks.compilation.tap("GitVersionPlugin", (compilation) => {
if (this.dependsOnGit) {
compilation.fileDependencies.add(path.join(context, ".git/logs/HEAD"))
compilation.contextDependencies.add(path.join(context, ".git/refs/tags"))
}
compilation.hooks.processAssets.tapPromise(
{
name: "GitVersionPlugin",
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
},
async (assets) => {
try {
const v = await this.version()
assets[this.options.outputFile] = {
source: () => `${v}\n`,
size: () => v.length + 1
}
} catch {
assets[this.options.outputFile] = {
source: () => "",
size: () => 0
}
}
}
)
})
}
async version() {
const execFile = promisify(execFileCps)
try {
const { stdout: describe } = await execFile("git", ["describe", "--long", "--tags"])
const [, tag, offset] = describe.trim().match(/^(.*)-(\d+)-g[0-9a-f]+$/)
return parseInt(offset) === 0 ? tag : this.getBranchAndHash()
} catch {
return this.getBranchAndHash()
}
}
async getBranchAndHash() {
const execFile = promisify(execFileCps)
const [{ stdout: branch }, { stdout: hash }] = await Promise.all([
execFile("git", ["rev-parse", "--abbrev-ref", "HEAD"]),
execFile("git", ["rev-parse", "HEAD"])
])
return `${branch.trim()}@${hash.substring(0, 7)}`
}
}
export { SRIPlugin, GitVersionPlugin }