Skip to content

Commit e86d613

Browse files
committedFeb 10, 2022
chore: support vite 2.8.0
1 parent 689324b commit e86d613

File tree

10 files changed

+230
-199
lines changed

10 files changed

+230
-199
lines changed
 

‎package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@
2727
"@commitlint/config-conventional": "^16.0.0",
2828
"@types/html-minifier-terser": "^6.1.0",
2929
"@types/jsdom": "^16.2.14",
30-
"@types/node": "^17.0.13",
31-
"@typescript-eslint/eslint-plugin": "^5.10.1",
32-
"@typescript-eslint/parser": "^5.10.1",
30+
"@types/node": "^17.0.16",
31+
"@typescript-eslint/eslint-plugin": "^5.11.0",
32+
"@typescript-eslint/parser": "^5.11.0",
3333
"commitizen": "^4.2.4",
3434
"conventional-changelog-cli": "^2.2.2",
3535
"cross-env": "^7.0.3",
3636
"eslint": "^8.8.0",
3737
"eslint-config-prettier": "^8.3.0",
3838
"eslint-plugin-html": "^6.2.0",
3939
"husky": "^7.0.4",
40-
"lint-staged": "^12.3.2",
40+
"lint-staged": "^12.3.3",
4141
"prettier": "^2.5.1",
4242
"rimraf": "^3.0.2",
43-
"tsup": "^5.11.11",
43+
"tsup": "^5.11.13",
4444
"typescript": "^4.5.5",
4545
"unbuild": "^0.6.9",
46-
"vite": "^2.7.13",
47-
"vitest": "^0.2.5"
46+
"vite": "^2.8.0",
47+
"vitest": "^0.3.0"
4848
},
4949
"lint-staged": {
5050
"*": [

‎packages/core/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
"colorette": "^2.0.16",
4747
"connect-history-api-fallback": "^1.6.0",
4848
"consola": "^2.15.3",
49-
"dotenv": "^14.3.2",
50-
"dotenv-expand": "^6.0.1",
49+
"dotenv": "^16.0.0",
50+
"dotenv-expand": "^8.0.1",
5151
"ejs": "^3.1.6",
5252
"fast-glob": "^3.2.11",
5353
"fs-extra": "^10.0.0",
@@ -62,8 +62,8 @@
6262
"@types/ejs": "^3.1.0",
6363
"@types/fs-extra": "^9.0.13",
6464
"@types/html-minifier-terser": "^6.1.0",
65-
"@types/node": "^17.0.13",
65+
"@types/node": "^17.0.16",
6666
"typescript": "^4.5.5",
67-
"vite": "^2.7.13"
67+
"vite": "^2.8.0"
6868
}
6969
}

‎packages/core/src/htmlPlugin.ts

+25-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ResolvedConfig, Plugin, TransformResult } from 'vite'
1+
import type { ResolvedConfig, PluginOption, TransformResult } from 'vite'
22
import type { InjectOptions, PageOption, Pages, UserOptions } from './typing'
33
import { render } from 'ejs'
44
import { cleanUrl, isDirEmpty, loadEnv } from './utils'
@@ -17,8 +17,13 @@ const ignoreDirs = ['.', '', '/']
1717

1818
const bodyInjectRE = /<\/body>/
1919

20-
export function createPlugin(userOptions: UserOptions = {}): Plugin {
21-
const { entry, template = DEFAULT_TEMPLATE, pages = [] } = userOptions
20+
export function createPlugin(userOptions: UserOptions = {}): PluginOption {
21+
const {
22+
entry,
23+
template = DEFAULT_TEMPLATE,
24+
pages = [],
25+
verbose = false,
26+
} = userOptions
2227

2328
let viteConfig: ResolvedConfig
2429
let env: Record<string, any> = {}
@@ -31,13 +36,16 @@ export function createPlugin(userOptions: UserOptions = {}): Plugin {
3136
env = loadEnv(viteConfig.mode, viteConfig.root, '')
3237
},
3338
config(conf) {
34-
return mergeConfig(conf, {
35-
build: {
36-
rollupOptions: {
37-
input: createInput(userOptions, conf as unknown as ResolvedConfig),
39+
const input = createInput(userOptions, conf as unknown as ResolvedConfig)
40+
if (input) {
41+
return {
42+
build: {
43+
rollupOptions: {
44+
input,
45+
},
3846
},
39-
},
40-
})
47+
}
48+
}
4149
},
4250

4351
configureServer(server) {
@@ -68,6 +76,7 @@ export function createPlugin(userOptions: UserOptions = {}): Plugin {
6876
viteConfig,
6977
env,
7078
entry: page.entry || entry,
79+
verbose,
7180
})
7281
html = await server.transformIndexHtml?.(url, html, req.originalUrl)
7382
res.end(html)
@@ -88,6 +97,7 @@ export function createPlugin(userOptions: UserOptions = {}): Plugin {
8897
viteConfig,
8998
env,
9099
entry: page.entry || entry,
100+
verbose,
91101
}).then((resultHtml) => {
92102
return {
93103
code: resultHtml,
@@ -180,9 +190,10 @@ export async function renderHtml(
180190
viteConfig: ResolvedConfig
181191
env: Record<string, any>
182192
entry?: string
193+
verbose?: boolean
183194
},
184195
) {
185-
const { injectOptions, viteConfig, env, entry } = config
196+
const { injectOptions, viteConfig, env, entry, verbose } = config
186197
const { data, ejsOptions } = injectOptions
187198

188199
const ejsData: Record<string, any> = {
@@ -194,7 +205,7 @@ export async function renderHtml(
194205
let result = await render(html, ejsData, ejsOptions)
195206

196207
if (entry) {
197-
result = removeEntryScript(result)
208+
result = removeEntryScript(result, verbose)
198209
result = result.replace(
199210
bodyInjectRE,
200211
`<script type="module" src="${normalizePath(
@@ -224,7 +235,7 @@ function isMpa(viteConfig: ResolvedConfig) {
224235
return typeof input !== 'string' && Object.keys(input || {}).length > 1
225236
}
226237

227-
export function removeEntryScript(html: string) {
238+
export function removeEntryScript(html: string, verbose = false) {
228239
if (!html) {
229240
return html
230241
}
@@ -236,7 +247,8 @@ export function removeEntryScript(html: string) {
236247
removedNode.push(item.toString())
237248
item.parentNode.removeChild(item)
238249
})
239-
removedNode.length &&
250+
verbose &&
251+
removedNode.length &&
240252
consola.warn(`vite-plugin-html: Since you have already configured entry, ${dim(
241253
removedNode.toString(),
242254
)} is deleted. You may also delete it from the index.html.

‎packages/core/src/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import type { Plugin } from 'vite'
1+
import type { PluginOption } from 'vite'
22
import type { UserOptions } from './typing'
33
import { createPlugin } from './htmlPlugin'
44
import { createMinifyHtmlPlugin } from './minifyHtml'
55
import consola from 'consola'
66

77
consola.wrapConsole()
88

9-
export function createHtmlPlugin(userOptions: UserOptions = {}): Plugin[] {
9+
export function createHtmlPlugin(
10+
userOptions: UserOptions = {},
11+
): PluginOption[] {
1012
return [createPlugin(userOptions), createMinifyHtmlPlugin(userOptions)]
1113
}

‎packages/core/src/minifyHtml.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Plugin } from 'vite'
1+
import type { PluginOption } from 'vite'
22
import type { UserOptions } from './typing'
33
import type { Options as MinifyOptions } from 'html-minifier-terser'
44
import { minify as minifyFn } from 'html-minifier-terser'
@@ -36,7 +36,7 @@ export async function minifyHtml(
3636

3737
export function createMinifyHtmlPlugin({
3838
minify = true,
39-
}: UserOptions = {}): Plugin {
39+
}: UserOptions = {}): PluginOption {
4040
return {
4141
name: 'vite:minify-html',
4242
// apply: 'build',

‎packages/core/src/typing.ts

+6
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ export interface UserOptions {
4949
* @description inject options
5050
*/
5151
inject?: InjectOptions
52+
53+
/**
54+
* output warning log
55+
* @default false
56+
*/
57+
verbose?: boolean
5258
}

‎packages/playground/basic/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
},
88
"dependencies": {
99
"vite-plugin-html": "workspace:*",
10-
"vue": "^3.2.29",
10+
"vue": "^3.2.30",
1111
"vue-router": "^4.0.12"
1212
},
1313
"devDependencies": {
14-
"@vitejs/plugin-vue": "^2.1.0",
15-
"@vue/compiler-sfc": "^3.2.29",
16-
"vite": "^2.7.13"
14+
"@vitejs/plugin-vue": "^2.2.0",
15+
"@vue/compiler-sfc": "^3.2.30",
16+
"vite": "^2.8.0"
1717
}
1818
}

‎packages/playground/mpa/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
},
99
"dependencies": {
1010
"vite-plugin-html": "workspace:*",
11-
"vue": "^3.2.29"
11+
"vue": "^3.2.30"
1212
},
1313
"devDependencies": {
14-
"@vitejs/plugin-vue": "^2.1.0",
15-
"@vue/compiler-sfc": "^3.2.29",
16-
"vite": "^2.7.13"
14+
"@vitejs/plugin-vue": "^2.2.0",
15+
"@vue/compiler-sfc": "^3.2.30",
16+
"vite": "^2.8.0"
1717
}
1818
}

‎packages/playground/mpa/public/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99

1010
<body>
1111
<div id="app"></div>
12+
<div>index app</div>
1213
</body>
1314
</html>

‎pnpm-lock.yaml

+173-163
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.