1
- import type { ResolvedConfig , Plugin , TransformResult } from 'vite'
1
+ import type { ResolvedConfig , PluginOption , TransformResult } from 'vite'
2
2
import type { InjectOptions , PageOption , Pages , UserOptions } from './typing'
3
3
import { render } from 'ejs'
4
4
import { cleanUrl , isDirEmpty , loadEnv } from './utils'
@@ -17,8 +17,13 @@ const ignoreDirs = ['.', '', '/']
17
17
18
18
const bodyInjectRE = / < \/ b o d y > /
19
19
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
22
27
23
28
let viteConfig : ResolvedConfig
24
29
let env : Record < string , any > = { }
@@ -31,13 +36,16 @@ export function createPlugin(userOptions: UserOptions = {}): Plugin {
31
36
env = loadEnv ( viteConfig . mode , viteConfig . root , '' )
32
37
} ,
33
38
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
+ } ,
38
46
} ,
39
- } ,
40
- } )
47
+ }
48
+ }
41
49
} ,
42
50
43
51
configureServer ( server ) {
@@ -68,6 +76,7 @@ export function createPlugin(userOptions: UserOptions = {}): Plugin {
68
76
viteConfig,
69
77
env,
70
78
entry : page . entry || entry ,
79
+ verbose,
71
80
} )
72
81
html = await server . transformIndexHtml ?.( url , html , req . originalUrl )
73
82
res . end ( html )
@@ -88,6 +97,7 @@ export function createPlugin(userOptions: UserOptions = {}): Plugin {
88
97
viteConfig,
89
98
env,
90
99
entry : page . entry || entry ,
100
+ verbose,
91
101
} ) . then ( ( resultHtml ) => {
92
102
return {
93
103
code : resultHtml ,
@@ -180,9 +190,10 @@ export async function renderHtml(
180
190
viteConfig : ResolvedConfig
181
191
env : Record < string , any >
182
192
entry ?: string
193
+ verbose ?: boolean
183
194
} ,
184
195
) {
185
- const { injectOptions, viteConfig, env, entry } = config
196
+ const { injectOptions, viteConfig, env, entry, verbose } = config
186
197
const { data, ejsOptions } = injectOptions
187
198
188
199
const ejsData : Record < string , any > = {
@@ -194,7 +205,7 @@ export async function renderHtml(
194
205
let result = await render ( html , ejsData , ejsOptions )
195
206
196
207
if ( entry ) {
197
- result = removeEntryScript ( result )
208
+ result = removeEntryScript ( result , verbose )
198
209
result = result . replace (
199
210
bodyInjectRE ,
200
211
`<script type="module" src="${ normalizePath (
@@ -224,7 +235,7 @@ function isMpa(viteConfig: ResolvedConfig) {
224
235
return typeof input !== 'string' && Object . keys ( input || { } ) . length > 1
225
236
}
226
237
227
- export function removeEntryScript ( html : string ) {
238
+ export function removeEntryScript ( html : string , verbose = false ) {
228
239
if ( ! html ) {
229
240
return html
230
241
}
@@ -236,7 +247,8 @@ export function removeEntryScript(html: string) {
236
247
removedNode . push ( item . toString ( ) )
237
248
item . parentNode . removeChild ( item )
238
249
} )
239
- removedNode . length &&
250
+ verbose &&
251
+ removedNode . length &&
240
252
consola . warn ( `vite-plugin-html: Since you have already configured entry, ${ dim (
241
253
removedNode . toString ( ) ,
242
254
) } is deleted. You may also delete it from the index.html.
0 commit comments