Skip to content

Commit dc0bcd4

Browse files
committed
Convert source to TypeScript 🔐
There is a missing type definition for the assetPath hook.
1 parent 84ba6c7 commit dc0bcd4

11 files changed

Lines changed: 220 additions & 144 deletions

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@
3737
}
3838
},
3939
"prettier": {
40-
"printWidth": 80,
41-
"semi": true,
40+
"printWidth": 120,
41+
"semi": false,
4242
"singleQuote": true,
43-
"trailingComma": "es5"
43+
"trailingComma": "all"
4444
},
4545
"devDependencies": {
4646
"@types/jest": "^24.0.18",
47+
"@types/webpack": "^4.39.2",
4748
"husky": "^3.0.8",
4849
"tsdx": "^0.9.3",
4950
"tslib": "^1.10.0",

src/build-file.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/build-file.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import runGitCommand from './helpers/run-git-command'
2+
import { Compiler } from 'webpack'
3+
4+
interface BuildFileOptions {
5+
compiler: Compiler
6+
gitWorkTree?: string
7+
command: string
8+
replacePattern: RegExp
9+
asset: string
10+
}
11+
12+
export default function buildFile({ compiler, gitWorkTree, command, replacePattern, asset }: BuildFileOptions) {
13+
let data: string | undefined = ''
14+
15+
compiler.hooks.compilation.tap('GitRevisionWebpackPlugin', compilation => {
16+
compilation.hooks.optimizeTree.tapAsync('optimize-tree', (_, __, callback) => {
17+
runGitCommand(gitWorkTree, command, function(err, res) {
18+
if (err) {
19+
return callback(err)
20+
}
21+
data = res
22+
23+
callback()
24+
})
25+
})
26+
27+
// we are missing the type definition for assetPath
28+
// TODO: create a PR to DefinitelyTyped
29+
const mainTemplate = compilation.mainTemplate as any
30+
31+
// TODO remove `any` once we get the type definitions
32+
mainTemplate.hooks.assetPath.tap('GitRevisionWebpackPlugin', (assetPath: any, chunkData: any) => {
33+
const path = typeof assetPath === 'function' ? assetPath(chunkData) : assetPath
34+
35+
if (!data) return path
36+
return path.replace(replacePattern, data)
37+
})
38+
})
39+
40+
compiler.hooks.emit.tap('GitRevisionWebpackPlugin', compilation => {
41+
compilation.assets[asset] = {
42+
source: function() {
43+
return data
44+
},
45+
size: function() {
46+
return data ? data.length : 0
47+
},
48+
}
49+
})
50+
}

src/helpers/remove-empty-lines.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/helpers/remove-empty-lines.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function removeEmptyLines(string: string) {
2+
return string.replace(/[\s\r\n]+$/, '')
3+
}

src/helpers/run-git-command.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/helpers/run-git-command.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { exec } from 'child_process'
2+
import { execSync } from 'child_process'
3+
import path from 'path'
4+
import removeEmptyLines from './remove-empty-lines'
5+
6+
interface Cb {
7+
(err: Error | null, output?: string): void
8+
}
9+
10+
export default function(gitWorkTree: string | undefined, command: string, callback?: Cb) {
11+
const gitCommand = gitWorkTree
12+
? ['git', '--git-dir=' + path.join(gitWorkTree, '.git'), '--work-tree=' + gitWorkTree, command].join(' ')
13+
: ['git', command].join(' ')
14+
15+
if (callback) {
16+
exec(gitCommand, function(err, stdout) {
17+
if (err) {
18+
return callback(err)
19+
}
20+
callback(null, removeEmptyLines(stdout))
21+
})
22+
23+
return null
24+
} else {
25+
return removeEmptyLines(`${execSync(gitCommand)}`)
26+
}
27+
}

src/index.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/index.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import buildFile from './build-file'
2+
import runGitCommand from './helpers/run-git-command'
3+
import { Compiler } from 'webpack'
4+
5+
const COMMITHASH_COMMAND = 'rev-parse HEAD'
6+
const VERSION_COMMAND = 'describe --always'
7+
const BRANCH_COMMAND = 'rev-parse --abbrev-ref HEAD'
8+
const LASTCOMMITDATETIME_COMMAND = 'log -1 --format=%cI'
9+
10+
interface GitRevisionPluginOptions {
11+
gitWorkTree?: string
12+
commithashCommand?: string
13+
versionCommand?: string
14+
branch?: boolean
15+
branchCommand?: string
16+
lastCommitDateTimeCommand?: string
17+
lightweightTags?: boolean
18+
}
19+
20+
export default class GitRevisionPlugin {
21+
gitWorkTree?: string
22+
commithashCommand: string
23+
versionCommand: string
24+
createBranchFile: boolean
25+
branchCommand: string
26+
lastCommitDateTimeCommand: string
27+
28+
constructor(options: GitRevisionPluginOptions = {}) {
29+
this.gitWorkTree = options.gitWorkTree
30+
this.commithashCommand = options.commithashCommand || COMMITHASH_COMMAND
31+
this.versionCommand = options.versionCommand || VERSION_COMMAND + (options.lightweightTags ? ' --tags' : '')
32+
this.createBranchFile = options.branch || false
33+
this.branchCommand = options.branchCommand || BRANCH_COMMAND
34+
this.lastCommitDateTimeCommand = options.lastCommitDateTimeCommand || LASTCOMMITDATETIME_COMMAND
35+
36+
if (options.versionCommand && options.lightweightTags) {
37+
throw new Error("lightweightTags can't be used together versionCommand")
38+
}
39+
}
40+
41+
commithash() {
42+
return runGitCommand(this.gitWorkTree, this.commithashCommand)
43+
}
44+
45+
version() {
46+
return runGitCommand(this.gitWorkTree, this.versionCommand)
47+
}
48+
49+
branch() {
50+
return runGitCommand(this.gitWorkTree, this.branchCommand)
51+
}
52+
53+
lastcommitdatetime() {
54+
return runGitCommand(this.gitWorkTree, this.lastCommitDateTimeCommand)
55+
}
56+
57+
apply(compiler: Compiler) {
58+
buildFile({
59+
compiler: compiler,
60+
gitWorkTree: this.gitWorkTree,
61+
command: this.commithashCommand,
62+
replacePattern: /\[git-revision-hash\]/gi,
63+
asset: 'COMMITHASH',
64+
})
65+
66+
buildFile({
67+
compiler: compiler,
68+
gitWorkTree: this.gitWorkTree,
69+
command: this.versionCommand,
70+
replacePattern: /\[git-revision-version\]/gi,
71+
asset: 'VERSION',
72+
})
73+
74+
buildFile({
75+
compiler: compiler,
76+
gitWorkTree: this.gitWorkTree,
77+
command: this.lastCommitDateTimeCommand,
78+
replacePattern: /\[git-revision-last-commit-datetime\]/gi,
79+
asset: 'LASTCOMMITDATETIME',
80+
})
81+
82+
if (this.createBranchFile) {
83+
buildFile({
84+
compiler: compiler,
85+
gitWorkTree: this.gitWorkTree,
86+
command: this.branchCommand,
87+
replacePattern: /\[git-revision-branch\]/gi,
88+
asset: 'BRANCH',
89+
})
90+
}
91+
}
92+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"compilerOptions": {
44
"target": "es5",
55
"module": "esnext",
6-
"lib": ["dom", "esnext"],
6+
"lib": ["esnext"],
77
"importHelpers": true,
88
"declaration": true,
99
"sourceMap": true,

0 commit comments

Comments
 (0)