@@ -20,6 +20,10 @@ export type Config = {
20
20
//
21
21
// @default true
22
22
deleteInlinedFiles ?: boolean
23
+ // If you need to override anything specific in the recommended build config, pass a sparse
24
+ // object here. For example, use `{ base: "/" }` to override the default "./" base path for
25
+ // non-inlined public files.
26
+ overrideConfig ?: Partial < UserConfig >
23
27
}
24
28
25
29
const defaultConfig = { useRecommendedBuildConfig : true , removeViteModuleLoader : false , deleteInlinedFiles : true }
@@ -28,7 +32,7 @@ export function replaceScript(html: string, scriptFilename: string, scriptCode:
28
32
const f = scriptFilename . replaceAll ( "." , "\\." )
29
33
const reScript = new RegExp ( `<script([^>]*?) src="(?:[^"]*?/)?${ f } "([^>]*)></script>` )
30
34
const preloadMarker = / " ? _ _ V I T E _ P R E L O A D _ _ " ? / g
31
- const newCode = scriptCode . replace ( preloadMarker , "void 0" ) . replace ( / < ( \/ s c r i p t > | ! - - ) / g, ' \\x3C$1' )
35
+ const newCode = scriptCode . replace ( preloadMarker , "void 0" ) . replace ( / < ( \/ s c r i p t > | ! - - ) / g, " \\x3C$1" )
32
36
const inlined = html . replace ( reScript , ( _ , beforeSrc , afterSrc ) => `<script${ beforeSrc } ${ afterSrc } >${ newCode . trim ( ) } </script>` )
33
37
return removeViteModuleLoader ? _removeViteModuleLoader ( inlined ) : inlined
34
38
}
@@ -37,7 +41,7 @@ export function replaceCss(html: string, scriptFilename: string, scriptCode: str
37
41
const f = scriptFilename . replaceAll ( "." , "\\." )
38
42
const reStyle = new RegExp ( `<link([^>]*?) href="(?:[^"]*?/)?${ f } "([^>]*)>` )
39
43
const newCode = scriptCode . replace ( `@charset "UTF-8";` , "" )
40
- const inlined = html . replace ( reStyle , ( _ , beforeSrc , afterSrc ) => `<style${ beforeSrc } ${ afterSrc } >${ newCode . trim ( ) } </style>` ) ;
44
+ const inlined = html . replace ( reStyle , ( _ , beforeSrc , afterSrc ) => `<style${ beforeSrc } ${ afterSrc } >${ newCode . trim ( ) } </style>` )
41
45
return inlined
42
46
}
43
47
@@ -50,19 +54,53 @@ export function viteSingleFile({
50
54
removeViteModuleLoader = false ,
51
55
inlinePattern = [ ] ,
52
56
deleteInlinedFiles = true ,
57
+ overrideConfig = { } ,
53
58
} : Config = defaultConfig ) : PluginOption {
59
+ // Modifies the Vite build config to make this plugin work well.
60
+ const _useRecommendedBuildConfig = ( config : UserConfig ) => {
61
+ if ( ! config . build ) config . build = { }
62
+ // Ensures that even very large assets are inlined in your JavaScript.
63
+ config . build . assetsInlineLimit = ( ) => true
64
+ // Avoid warnings about large chunks.
65
+ config . build . chunkSizeWarningLimit = 100000000
66
+ // Emit all CSS as a single file, which `vite-plugin-singlefile` can then inline.
67
+ config . build . cssCodeSplit = false
68
+ // We need relative path to support any static files in public folder,
69
+ // which are copied to ${build.outDir} by vite.
70
+ config . base = "./"
71
+ // Make generated files in ${build.outDir}'s root, instead of default ${build.outDir}/assets.
72
+ // Then the embedded resources can be loaded by relative path.
73
+ config . build . assetsDir = ""
74
+
75
+ if ( ! config . build . rollupOptions ) config . build . rollupOptions = { }
76
+ if ( ! config . build . rollupOptions . output ) config . build . rollupOptions . output = { }
77
+
78
+ const updateOutputOptions = ( out : OutputOptions ) => {
79
+ // Ensure that as many resources as possible are inlined.
80
+ out . inlineDynamicImports = true
81
+ }
82
+
83
+ if ( Array . isArray ( config . build . rollupOptions . output ) ) {
84
+ for ( const o of config . build . rollupOptions . output ) updateOutputOptions ( o as OutputOptions )
85
+ } else {
86
+ updateOutputOptions ( config . build . rollupOptions . output as OutputOptions )
87
+ }
88
+
89
+ Object . assign ( config , overrideConfig )
90
+ }
91
+
54
92
return {
55
93
name : "vite:singlefile" ,
56
94
config : useRecommendedBuildConfig ? _useRecommendedBuildConfig : undefined ,
57
95
enforce : "post" ,
58
96
generateBundle ( _ , bundle ) {
59
- const warnNotInlined = ( filename : string ) => this . info ( `NOTE: asset not inlined: ${ filename } ` )
97
+ const warnNotInlined = ( filename : string ) => this . info ( `NOTE: asset not inlined: ${ filename } ` )
60
98
this . info ( "\n" )
61
99
const files = {
62
100
html : [ ] as string [ ] ,
63
101
css : [ ] as string [ ] ,
64
102
js : [ ] as string [ ] ,
65
- other : [ ] as string [ ]
103
+ other : [ ] as string [ ] ,
66
104
}
67
105
for ( const i of Object . keys ( bundle ) ) {
68
106
if ( isHtmlFile . test ( i ) ) {
@@ -124,34 +162,3 @@ export function viteSingleFile({
124
162
// https://github.com/richardtallent/vite-plugin-singlefile/issues/57#issuecomment-1263950209
125
163
const _removeViteModuleLoader = ( html : string ) =>
126
164
html . replace ( / ( < s c r i p t t y p e = " m o d u l e " c r o s s o r i g i n > \s * ) \( f u n c t i o n (?: p o l y f i l l ) ? \( \) \s * \{ [ \s \S ] * ?\} \) \( \) ; / , '<script type="module">' )
127
-
128
- // Modifies the Vite build config to make this plugin work well.
129
- const _useRecommendedBuildConfig = ( config : UserConfig ) => {
130
- if ( ! config . build ) config . build = { }
131
- // Ensures that even very large assets are inlined in your JavaScript.
132
- config . build . assetsInlineLimit = ( ) => true
133
- // Avoid warnings about large chunks.
134
- config . build . chunkSizeWarningLimit = 100000000
135
- // Emit all CSS as a single file, which `vite-plugin-singlefile` can then inline.
136
- config . build . cssCodeSplit = false
137
- // We need relative path to support any static files in public folder,
138
- // which are copied to ${build.outDir} by vite.
139
- config . base = './'
140
- // Make generated files in ${build.outDir}'s root, instead of default ${build.outDir}/assets.
141
- // Then the embedded resources can be loaded by relative path.
142
- config . build . assetsDir = ''
143
-
144
- if ( ! config . build . rollupOptions ) config . build . rollupOptions = { }
145
- if ( ! config . build . rollupOptions . output ) config . build . rollupOptions . output = { }
146
-
147
- const updateOutputOptions = ( out : OutputOptions ) => {
148
- // Ensure that as many resources as possible are inlined.
149
- out . inlineDynamicImports = true
150
- }
151
-
152
- if ( Array . isArray ( config . build . rollupOptions . output ) ) {
153
- for ( const o of config . build . rollupOptions . output ) updateOutputOptions ( o as OutputOptions )
154
- } else {
155
- updateOutputOptions ( config . build . rollupOptions . output as OutputOptions )
156
- }
157
- }
0 commit comments