Skip to content

Commit

Permalink
feat: remove viteNext, compatibility with different versions of vite …
Browse files Browse the repository at this point in the history
…using new way
  • Loading branch information
ryomahan committed Dec 27, 2023
1 parent 7ad533a commit 821b3a4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 41 deletions.
19 changes: 7 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ export default defineConfig({
},
],
},
/**
* If you are using vite that version higher than 5.0.0-beta.13, you can set viteNext to true to align vite's config
*/
viteNext: false,
}),
],
})
Expand Down Expand Up @@ -154,14 +150,13 @@ export default defineConfig({

### UserOptions

| Parameter | Types | Default | Description |
| --------- | ------------------------ | ------------- | ------------------------------------------------- |
| entry | `string` | `src/main.ts` | entry file path |
| template | `string` | `index.html` | relative path to the template |
| inject | `InjectOptions` | - | Data injected into HTML |
| minify | `boolean|MinifyOptions` | - | whether to compress html |
| pages | `PageOption` | - | Multi-page configuration |
| viteNext | `boolean` | false | set to true to support the latest version of vite |
| Parameter | Types | Default | Description |
| --------- | ------------------------ | ------------- | ----------------------------- |
| entry | `string` | `src/main.ts` | entry file path |
| template | `string` | `index.html` | relative path to the template |
| inject | `InjectOptions` | - | Data injected into HTML |
| minify | `boolean|MinifyOptions` | - | whether to compress html |
| pages | `PageOption` | - | Multi-page configuration |

### InjectOptions

Expand Down
19 changes: 7 additions & 12 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ export default defineConfig({
},
],
},
/**
* 如果你使用的 vite 版本高于 5.0.0-beta.13,可以将 viteNext 设置为 true 来进行兼容
*/
viteNext: false,
}),
],
})
Expand Down Expand Up @@ -156,14 +152,13 @@ export default defineConfig({

### UserOptions

| 参数 | 类型 | 默认值 | 说明 |
| -------- | ------------------------ | ------------- | ------------------------------- |
| entry | `string` | `src/main.ts` | 入口文件 |
| template | `string` | `index.html` | 模板的相对路径 |
| inject | `InjectOptions` | - | 注入 HTML 的数据 |
| minify | `boolean|MinifyOptions` | - | 是否压缩 html |
| pages | `PageOption` | - | 多页配置 |
| viteNext | `boolean` | false | 用于开启是否兼容最新版本的 vite |
| 参数 | 类型 | 默认值 | 说明 |
| -------- | ------------------------ | ------------- | ---------------- |
| entry | `string` | `src/main.ts` | 入口文件 |
| template | `string` | `index.html` | 模板的相对路径 |
| inject | `InjectOptions` | - | 注入 HTML 的数据 |
| minify | `boolean|MinifyOptions` | - | 是否压缩 html |
| pages | `PageOption` | - | 多页配置 |

### InjectOptions

Expand Down
28 changes: 16 additions & 12 deletions packages/core/src/htmlPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ import fg from 'fast-glob'
import consola from 'consola'
import { dim } from 'colorette'
import history from 'connect-history-api-fallback'
import * as vite from 'vite'

const DEFAULT_TEMPLATE = 'index.html'
const ignoreDirs = ['.', '', '/']

const bodyInjectRE = /<\/body>/

function getViteMajorVersion() {
return vite?.version ? Number(vite.version.split('.')[0]) : 2
}

export function createPlugin(userOptions: UserOptions = {}): PluginOption {
const {
entry,
Expand Down Expand Up @@ -114,18 +119,17 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
)
},

transformIndexHtml: userOptions.viteNext
?
{
// @ts-ignore
order: 'pre',
handler: transformIndexHtmlHandler,
}
:
{
enforce: 'pre',
transform: transformIndexHtmlHandler,
},
transformIndexHtml:
getViteMajorVersion() >= 5
? {
// @ts-ignore
order: 'pre',
handler: transformIndexHtmlHandler,
}
: {
enforce: 'pre',
transform: transformIndexHtmlHandler,
},
async closeBundle() {
const outputDirs: string[] = []

Expand Down
5 changes: 0 additions & 5 deletions packages/core/src/typing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,4 @@ export interface UserOptions {
* @default false
*/
verbose?: boolean

/**
* fit vite2+
*/
viteNext?: boolean
}

0 comments on commit 821b3a4

Please # to comment.