Skip to content

Commit 8bca056

Browse files
committed
feat: automatically detected injectFileBase
1 parent 9763d68 commit 8bca056

File tree

7 files changed

+40
-20
lines changed

7 files changed

+40
-20
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,15 @@ export interface Options {
247247
*/
248248
hiddenDismissButton?: boolean
249249
/**
250+
* After version 1.2.0, you not need to set this option, it will be automatically detected from the base of vite config、publicPath of webpack config or publicPath of umi config
251+
*
250252
* Base public path for inject file, Valid values include:
251253
* * Absolute URL pathname, e.g. /foo/
252254
* * Full URL, e.g. https://foo.com/
253255
* * Empty string(default) or ./
256+
*
254257
* !!! Don't forget / at the end of the path
255-
*/
258+
*/
256259
injectFileBase?: string
257260
}
258261

@@ -301,7 +304,7 @@ export type LocaleData = Record<string, NotificationProps>
301304
302305
## Q&A
303306
304-
1. `TypeScript` 的智能提示, 如果你想使用 `window.pluginWebUpdateNotice_.`
307+
1. `TypeScript` intellisense, if you use `window.pluginWebUpdateNotice_.`
305308
306309
```ts
307310
// src/shim.d.ts
@@ -351,6 +354,7 @@ export type LocaleData = Record<string, NotificationProps>
351354
})
352355
```
353356

357+
> After version 1.2.0, in most case, you not need to set injectFileBase, it will be automatically detected from the base of vite config、publicPath of webpack config or publicPath of umi config
354358
3. Custom notification button event.
355359

356360
```ts

README.zh-CN.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,15 @@ export interface Options {
245245
*/
246246
hiddenDismissButton?: boolean
247247
/**
248+
* After version 1.2.0, you not need to set this option, it will be automatically detected from the base of vite config、publicPath of webpack config or publicPath of umi config
249+
*
248250
* Base public path for inject file, Valid values include:
249251
* * Absolute URL pathname, e.g. /foo/
250252
* * Full URL, e.g. https://foo.com/
251253
* * Empty string(default) or ./
254+
*
252255
* !!! Don't forget / at the end of the path
253-
*/
256+
*/
254257
injectFileBase?: string
255258
}
256259

@@ -303,11 +306,11 @@ export type LocaleData = Record<string, NotificationProps>
303306

304307
```ts
305308
// vite.config.ts
306-
309+
307310
const prod = process.env.NODE_ENV === 'production'
308-
311+
309312
const cdnServerUrl = 'https://foo.com/'
310-
313+
311314
export default defineConfig({
312315
base: prod ? cdnServerUrl : '/',
313316
plugins: [
@@ -323,11 +326,11 @@ export type LocaleData = Record<string, NotificationProps>
323326

324327
```ts
325328
// vite.config.ts
326-
329+
327330
const prod = process.env.NODE_ENV === 'production'
328-
331+
329332
const base = '/folder/' // https://example.com/folder/
330-
333+
331334
export default defineConfig({
332335
base,
333336
plugins: [
@@ -339,6 +342,8 @@ export type LocaleData = Record<string, NotificationProps>
339342
})
340343
```
341344

345+
> After version 1.2.0, you not need to set this option, it will be automatically detected from the base of vite config、publicPath of webpack config or publicPath of umi config
346+
342347
3. 自定义 `notification` 的刷新和忽略按钮事件。
343348

344349
```ts
@@ -355,7 +360,7 @@ export type LocaleData = Record<string, NotificationProps>
355360

356361
```html
357362
<!-- notification html content -->
358-
363+
359364
<div class="plugin-web-update-notice-anchor">
360365
<div class="plugin-web-update-notice">
361366
<div class="plugin-web-update-notice-content" data-cy="notification-content">

example/vue-cli/vue.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ const { WebUpdateNotificationPlugin } = require('@plugin-web-update-notification
22
const { defineConfig } = require('@vue/cli-service')
33

44
module.exports = defineConfig({
5+
publicPath: '/test/',
56
transpileDependencies: true,
67
configureWebpack: {
78
plugins: [
89
new WebUpdateNotificationPlugin({
910
logVersion: true,
10-
injectFileBase: './',
1111
locale: 'en_US',
1212
}),
1313
],

packages/core/src/type.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@ export interface Options {
4949
*/
5050
hiddenDismissButton?: boolean
5151
/**
52+
* After version 1.2.0, you not need to set this option, it will be automatically detected from the base of vite config、publicPath of webpack config or publicPath of umi config
53+
*
5254
* Base public path for inject file, Valid values include:
5355
* * Absolute URL pathname, e.g. /foo/
5456
* * Full URL, e.g. https://foo.com/
5557
* * Empty string(default) or ./
58+
*
5659
* !!! Don't forget / at the end of the path
57-
*/
60+
*/
5861
injectFileBase?: string
5962
}
6063

packages/umi-plugin/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export default (api: IApi) => {
5252
},
5353
})
5454
const webUpdateNotificationOptions = (api.userConfig?.webUpdateNotification || {}) as Options
55+
if (webUpdateNotificationOptions.injectFileBase === undefined)
56+
webUpdateNotificationOptions.injectFileBase = api.userConfig.publicPath || '/'
57+
5558
const { versionType, logVersion, customNotificationHTML, hiddenDefaultNotification, injectFileBase = '', customVersion } = webUpdateNotificationOptions
5659

5760
let version = ''

packages/vite-plugin/src/index.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFileSync } from 'fs'
22
import { resolve } from 'path'
3-
import type { Plugin } from 'vite'
3+
import type { Plugin, ResolvedConfig } from 'vite'
44
import type { Options } from '@plugin-web-update-notification/core'
55
import {
66
DIRECTORY_NAME,
@@ -49,7 +49,7 @@ function injectPluginHtml(html: string, version: string, options: Options) {
4949
}
5050

5151
export function webUpdateNotice(options: Options = {}): Plugin {
52-
// let viteConfig: ResolvedConfig;
52+
let viteConfig: ResolvedConfig
5353

5454
const { versionType, customVersion } = options
5555
let version = ''
@@ -62,10 +62,12 @@ export function webUpdateNotice(options: Options = {}): Plugin {
6262
name: 'vue-vite-web-update-notice',
6363
apply: 'build',
6464
enforce: 'post',
65-
// configResolved(resolvedConfig: ResolvedConfig) {
66-
// // 存储最终解析的配置
67-
// viteConfig = resolvedConfig;
68-
// },
65+
configResolved(resolvedConfig: ResolvedConfig) {
66+
// 存储最终解析的配置
67+
viteConfig = resolvedConfig
68+
if (options.injectFileBase === undefined)
69+
options.injectFileBase = viteConfig.base
70+
},
6971
generateBundle(_, bundle = {}) {
7072
if (!version)
7173
return

packages/webpack-plugin/src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type PluginOptions = Options & {
2121
* @returns The html of the page with the injected script and css.
2222
*/
2323
function injectPluginHtml(html: string, version: string, options: Options) {
24-
const { logVersion, customNotificationHTML, hiddenDefaultNotification, injectFileBase = '' } = options
24+
const { logVersion, customNotificationHTML, hiddenDefaultNotification, injectFileBase = '/' } = options
2525

2626
const logHtml = logVersion ? `<script>console.log('version: %c${version}', 'color: #1890ff');</script>` : ''
2727
const versionScript = `<script>window.pluginWebUpdateNotice_version = '${version}';</script>`
@@ -60,6 +60,10 @@ class WebUpdateNotificationPlugin {
6060
}
6161

6262
apply(compiler: Compiler) {
63+
const { publicPath } = compiler.options.output
64+
if (this.options.injectFileBase === undefined)
65+
this.options.injectFileBase = typeof publicPath === 'string' ? publicPath : '/'
66+
6367
const { hiddenDefaultNotification, versionType, indexHtmlFilePath, customVersion } = this.options
6468
let version = ''
6569
if (versionType === 'custom')
@@ -69,7 +73,6 @@ class WebUpdateNotificationPlugin {
6973

7074
compiler.hooks.emit.tap(pluginName, (compilation: Compilation) => {
7175
// const outputPath = compiler.outputPath
72-
7376
const jsonFileContent = generateJSONFileContent(version)
7477
// @ts-expect-error
7578
compilation.assets[`${DIRECTORY_NAME}/${JSON_FILE_NAME}.json`] = {

0 commit comments

Comments
 (0)