Skip to content

Commit

Permalink
feat: adding compatibility code for different versions of vite
Browse files Browse the repository at this point in the history
  • Loading branch information
ryomahan committed Dec 27, 2023
1 parent 3138c7c commit 1bb6a4d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ryomahan-vite-plugin-html",
"version": "3.2.2",
"version": "3.2.3",
"description": "A plugin for vite to Minimize index.html and use lodash.template template syntax in index.html",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
Expand Down
35 changes: 23 additions & 12 deletions packages/core/src/htmlPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ 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(): number {
let version = 0
try {
// @ts-ignore
version = Number(vite.version.split('.')[0])
} catch (_) {
version = 2
}
return version
}

export function createPlugin(userOptions: UserOptions = {}): PluginOption {
const {
entry,
Expand Down Expand Up @@ -114,18 +126,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

0 comments on commit 1bb6a4d

Please sign in to comment.