Skip to content

Commit d0b4961

Browse files
committed
fix: πŸ› Incorrect version when version type is build_timestamp
βœ… Closes: #8
1 parent 309a438 commit d0b4961

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

β€Žpackages/core/src/index.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,22 @@ export function getTimestamp() {
6666
* @returns The version of the plugin.
6767
*/
6868
export function getVersion(versionType: VersionType = 'git_commit_hash') {
69+
const getVersionStrategies: Record<VersionType, () => string> = {
70+
pkg_version: getHostProjectPkgVersion,
71+
git_commit_hash: getGitCommitHash,
72+
build_timestamp: getTimestamp,
73+
}
6974
try {
70-
if (versionType === 'git_commit_hash')
71-
return getGitCommitHash()
72-
if (versionType === 'pkg_version')
73-
return getHostProjectPkgVersion()
75+
const strategy = getVersionStrategies[versionType]
76+
if (!strategy) {
77+
console.warn(`
78+
======================================================
79+
[plugin-web-update-notice] The version type '${versionType}' is not supported!, we will use the packaging timestamp instead.
80+
======================================================`)
81+
return getTimestamp()
82+
}
7483

75-
return getTimestamp()
84+
return strategy()
7685
}
7786
catch (err) {
7887
console.warn(`

β€Žpackages/vite-plugin/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ function injectPluginHtml(html: string, version: string, options: Options) {
5151

5252
export function webUpdateNotice(options: Options = {}): Plugin {
5353
// let viteConfig: ResolvedConfig;
54+
55+
const version = getVersion(options.versionType)
5456
return {
5557
name: 'vue-vite-web-update-notice',
5658
apply: 'build',
@@ -60,7 +62,6 @@ export function webUpdateNotice(options: Options = {}): Plugin {
6062
// viteConfig = resolvedConfig;
6163
// },
6264
generateBundle(_, bundle = {}) {
63-
const version = getVersion(options.versionType)
6465
if (!version)
6566
return
6667

@@ -94,7 +95,6 @@ export function webUpdateNotice(options: Options = {}): Plugin {
9495
transformIndexHtml: {
9596
enforce: 'post',
9697
transform(html: string) {
97-
const version = getVersion(options.versionType)
9898
if (version)
9999
return injectPluginHtml(html, version, options)
100100
return html

β€Žpackages/webpack-plugin/src/index.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ class WebUpdateNotificationPlugin {
6161
}
6262

6363
apply(compiler: Compiler) {
64+
const { hiddenDefaultNotification, versionType, indexHtmlFilePath } = this.options
65+
const version = getVersion(versionType)
66+
6467
compiler.hooks.emit.tap(pluginName, (compilation: Compilation) => {
6568
// const outputPath = compiler.outputPath
66-
const { hiddenDefaultNotification, versionType } = this.options
67-
68-
const version = getVersion(versionType)
6969

7070
const jsonFileContent = generateJSONFileContent(version)
7171
// @ts-expect-error
@@ -91,12 +91,10 @@ class WebUpdateNotificationPlugin {
9191
})
9292

9393
compiler.hooks.afterEmit.tap(pluginName, () => {
94-
const { indexHtmlFilePath } = this.options
9594
const htmlFilePath = resolve(compiler.outputPath, indexHtmlFilePath || './index.html')
9695
try {
9796
accessSync(htmlFilePath, constants.F_OK)
9897

99-
const version = getVersion(this.options.versionType)
10098
let html = readFileSync(htmlFilePath, 'utf8')
10199
html = injectPluginHtml(html, version, this.options)
102100
writeFileSync(htmlFilePath, html)

0 commit comments

Comments
Β (0)