From 7ad533a02de514d55057e3df0877b2d67ab97c03 Mon Sep 17 00:00:00 2001 From: ryomahan Date: Tue, 26 Dec 2023 18:44:30 +0800 Subject: [PATCH 1/2] docs: perfect README.md for the UserOptions of viteNext --- README.md | 19 ++++++++++++------- README.zh_CN.md | 19 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 7b8f43d..75f27bb 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,10 @@ 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, }), ], }) @@ -150,13 +154,14 @@ 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 | +| 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 | ### InjectOptions diff --git a/README.zh_CN.md b/README.zh_CN.md index fde5518..771211c 100644 --- a/README.zh_CN.md +++ b/README.zh_CN.md @@ -84,6 +84,10 @@ export default defineConfig({ }, ], }, + /** + * 如果你使用的 vite 版本高于 5.0.0-beta.13,可以将 viteNext 设置为 true 来进行兼容 + */ + viteNext: false, }), ], }) @@ -152,13 +156,14 @@ export default defineConfig({ ### UserOptions -| 参数 | 类型 | 默认值 | 说明 | -| -------- | ------------------------ | ------------- | ---------------- | -| entry | `string` | `src/main.ts` | 入口文件 | -| template | `string` | `index.html` | 模板的相对路径 | -| inject | `InjectOptions` | - | 注入 HTML 的数据 | -| minify | `boolean|MinifyOptions` | - | 是否压缩 html | -| pages | `PageOption` | - | 多页配置 | +| 参数 | 类型 | 默认值 | 说明 | +| -------- | ------------------------ | ------------- | ------------------------------- | +| entry | `string` | `src/main.ts` | 入口文件 | +| template | `string` | `index.html` | 模板的相对路径 | +| inject | `InjectOptions` | - | 注入 HTML 的数据 | +| minify | `boolean|MinifyOptions` | - | 是否压缩 html | +| pages | `PageOption` | - | 多页配置 | +| viteNext | `boolean` | false | 用于开启是否兼容最新版本的 vite | ### InjectOptions From 821b3a4fe07c7b1e718562be6cdb9fb19573c092 Mon Sep 17 00:00:00 2001 From: ryomahan Date: Thu, 28 Dec 2023 00:34:47 +0800 Subject: [PATCH 2/2] feat: remove viteNext, compatibility with different versions of vite using new way --- README.md | 19 +++++++------------ README.zh_CN.md | 19 +++++++------------ packages/core/src/htmlPlugin.ts | 28 ++++++++++++++++------------ packages/core/src/typing.ts | 5 ----- 4 files changed, 30 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 75f27bb..7b8f43d 100644 --- a/README.md +++ b/README.md @@ -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, }), ], }) @@ -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 diff --git a/README.zh_CN.md b/README.zh_CN.md index 771211c..fde5518 100644 --- a/README.zh_CN.md +++ b/README.zh_CN.md @@ -84,10 +84,6 @@ export default defineConfig({ }, ], }, - /** - * 如果你使用的 vite 版本高于 5.0.0-beta.13,可以将 viteNext 设置为 true 来进行兼容 - */ - viteNext: false, }), ], }) @@ -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 diff --git a/packages/core/src/htmlPlugin.ts b/packages/core/src/htmlPlugin.ts index 21e12db..58b6114 100644 --- a/packages/core/src/htmlPlugin.ts +++ b/packages/core/src/htmlPlugin.ts @@ -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, @@ -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[] = [] diff --git a/packages/core/src/typing.ts b/packages/core/src/typing.ts index 9c2d17a..b50ddf6 100644 --- a/packages/core/src/typing.ts +++ b/packages/core/src/typing.ts @@ -58,9 +58,4 @@ export interface UserOptions { * @default false */ verbose?: boolean - - /** - * fit vite2+ - */ - viteNext?: boolean }